wifi legrand

Dependencies:   MODSERIAL mbed

Committer:
TeamLegrand
Date:
Mon Apr 30 12:21:53 2018 +0000
Revision:
0:23cef74eac23
wifi serveur cabanis

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TeamLegrand 0:23cef74eac23 1 #include "mbed.h"
TeamLegrand 0:23cef74eac23 2 #include <iostream>
TeamLegrand 0:23cef74eac23 3 #include <string>
TeamLegrand 0:23cef74eac23 4 #include <fstream>
TeamLegrand 0:23cef74eac23 5 #include "MODSERIAL.h"
TeamLegrand 0:23cef74eac23 6
TeamLegrand 0:23cef74eac23 7 MODSERIAL pc(USBTX, USBRX);
TeamLegrand 0:23cef74eac23 8 MODSERIAL esp(p28, p27); // tx, rx
TeamLegrand 0:23cef74eac23 9 DigitalOut reset(p26);
TeamLegrand 0:23cef74eac23 10 Timer t;
TeamLegrand 0:23cef74eac23 11 DigitalOut led(p7);
TeamLegrand 0:23cef74eac23 12 DigitalOut vib(p5);
TeamLegrand 0:23cef74eac23 13 DigitalOut buz(p6);
TeamLegrand 0:23cef74eac23 14
TeamLegrand 0:23cef74eac23 15 int count,ended,timeout;
TeamLegrand 0:23cef74eac23 16 char buf[1024];
TeamLegrand 0:23cef74eac23 17 char snd[255];
TeamLegrand 0:23cef74eac23 18
TeamLegrand 0:23cef74eac23 19 char ssid[32] = "IOP-LEGRAND";
TeamLegrand 0:23cef74eac23 20 char pwd [32] = "1Up-8445";
TeamLegrand 0:23cef74eac23 21
TeamLegrand 0:23cef74eac23 22 char reponse_esp[5];
TeamLegrand 0:23cef74eac23 23 char reception_esp[3] = "OK";
TeamLegrand 0:23cef74eac23 24 char test[5] = "test";
TeamLegrand 0:23cef74eac23 25
TeamLegrand 0:23cef74eac23 26 char ledon[5] = "DELA";
TeamLegrand 0:23cef74eac23 27 char ledoff[5] = "DELE";
TeamLegrand 0:23cef74eac23 28
TeamLegrand 0:23cef74eac23 29 int longueur_recue = 0;
TeamLegrand 0:23cef74eac23 30 int nouvelle_reception = 0;
TeamLegrand 0:23cef74eac23 31 char recept[5] = {0};
TeamLegrand 0:23cef74eac23 32
TeamLegrand 0:23cef74eac23 33
TeamLegrand 0:23cef74eac23 34
TeamLegrand 0:23cef74eac23 35 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),WIFI(), Donnee();
TeamLegrand 0:23cef74eac23 36
TeamLegrand 0:23cef74eac23 37 using namespace std;
TeamLegrand 0:23cef74eac23 38
TeamLegrand 0:23cef74eac23 39 int main()
TeamLegrand 0:23cef74eac23 40 {
TeamLegrand 0:23cef74eac23 41 reset=0; //hardware reset for 8266
TeamLegrand 0:23cef74eac23 42 pc.baud(9600); // set what you want here depending on your terminal program speed
TeamLegrand 0:23cef74eac23 43 pc.printf("\f\n\r-------------Demarrage du module WIFI-------------\n\r");
TeamLegrand 0:23cef74eac23 44 wait(0.5);
TeamLegrand 0:23cef74eac23 45 reset=1;
TeamLegrand 0:23cef74eac23 46 timeout=2;
TeamLegrand 0:23cef74eac23 47 getreply();
TeamLegrand 0:23cef74eac23 48
TeamLegrand 0:23cef74eac23 49 esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time.
TeamLegrand 0:23cef74eac23 50
TeamLegrand 0:23cef74eac23 51 ESPconfig(); //****************** include Config to set the ESP8266 configuration ***********************
TeamLegrand 0:23cef74eac23 52
TeamLegrand 0:23cef74eac23 53 pc.printf("\f\n\r- Module pret -\n\r");
TeamLegrand 0:23cef74eac23 54
TeamLegrand 0:23cef74eac23 55 while(1){
TeamLegrand 0:23cef74eac23 56 if(esp.readable()){
TeamLegrand 0:23cef74eac23 57 wait(1);
TeamLegrand 0:23cef74eac23 58 }
TeamLegrand 0:23cef74eac23 59 while(esp.readable())
TeamLegrand 0:23cef74eac23 60 {
TeamLegrand 0:23cef74eac23 61 recept[longueur_recue]=esp.getc();
TeamLegrand 0:23cef74eac23 62 longueur_recue++;
TeamLegrand 0:23cef74eac23 63 nouvelle_reception = 1;
TeamLegrand 0:23cef74eac23 64 }
TeamLegrand 0:23cef74eac23 65
TeamLegrand 0:23cef74eac23 66 /*for(int i=0; i<5; i++){
TeamLegrand 0:23cef74eac23 67 pc.printf("%c",recept);
TeamLegrand 0:23cef74eac23 68 }*/
TeamLegrand 0:23cef74eac23 69
TeamLegrand 0:23cef74eac23 70 //char c = esp.getc();
TeamLegrand 0:23cef74eac23 71 //pc.printf("%c",recept);
TeamLegrand 0:23cef74eac23 72
TeamLegrand 0:23cef74eac23 73 /*if(reception_esp == test){
TeamLegrand 0:23cef74eac23 74 strcpy(snd, "AT+CIPSEND=0,4");
TeamLegrand 0:23cef74eac23 75 strcat(snd, reponse_esp);
TeamLegrand 0:23cef74eac23 76 SendCMD();
TeamLegrand 0:23cef74eac23 77 timeout=10;
TeamLegrand 0:23cef74eac23 78 getreply();
TeamLegrand 0:23cef74eac23 79 pc.printf(buf);
TeamLegrand 0:23cef74eac23 80 }*/
TeamLegrand 0:23cef74eac23 81
TeamLegrand 0:23cef74eac23 82 // VIBREUR
TeamLegrand 0:23cef74eac23 83 /*if(c == 'a'){
TeamLegrand 0:23cef74eac23 84 vib=1;
TeamLegrand 0:23cef74eac23 85 }
TeamLegrand 0:23cef74eac23 86 if(c == 'b'){
TeamLegrand 0:23cef74eac23 87 vib=0;
TeamLegrand 0:23cef74eac23 88 } */
TeamLegrand 0:23cef74eac23 89
TeamLegrand 0:23cef74eac23 90 // LED
TeamLegrand 0:23cef74eac23 91 if(recept == ledon){
TeamLegrand 0:23cef74eac23 92 led=1;
TeamLegrand 0:23cef74eac23 93 pc.printf("> Les leds sont ON -\n\r");
TeamLegrand 0:23cef74eac23 94 }
TeamLegrand 0:23cef74eac23 95 if(recept == ledoff){
TeamLegrand 0:23cef74eac23 96 led=0;
TeamLegrand 0:23cef74eac23 97 pc.printf("> Les leds sont OFF -\n\r");
TeamLegrand 0:23cef74eac23 98 }
TeamLegrand 0:23cef74eac23 99
TeamLegrand 0:23cef74eac23 100 // BUZZER
TeamLegrand 0:23cef74eac23 101 /*if(c == 'e'){
TeamLegrand 0:23cef74eac23 102 buz=1;
TeamLegrand 0:23cef74eac23 103 }
TeamLegrand 0:23cef74eac23 104 if(c == 'f'){
TeamLegrand 0:23cef74eac23 105 buz=0;
TeamLegrand 0:23cef74eac23 106 }
TeamLegrand 0:23cef74eac23 107 // TOTAL
TeamLegrand 0:23cef74eac23 108 if(c =='g'){
TeamLegrand 0:23cef74eac23 109 buz=1;
TeamLegrand 0:23cef74eac23 110 led=1;
TeamLegrand 0:23cef74eac23 111 vib=1;
TeamLegrand 0:23cef74eac23 112 }
TeamLegrand 0:23cef74eac23 113 if(c =='h'){
TeamLegrand 0:23cef74eac23 114 buz=0;
TeamLegrand 0:23cef74eac23 115 led=0;
TeamLegrand 0:23cef74eac23 116 vib=0;
TeamLegrand 0:23cef74eac23 117 }*/
TeamLegrand 0:23cef74eac23 118 }
TeamLegrand 0:23cef74eac23 119
TeamLegrand 0:23cef74eac23 120 }
TeamLegrand 0:23cef74eac23 121
TeamLegrand 0:23cef74eac23 122
TeamLegrand 0:23cef74eac23 123 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
TeamLegrand 0:23cef74eac23 124 void ESPconfig()
TeamLegrand 0:23cef74eac23 125 {
TeamLegrand 0:23cef74eac23 126 wait(5);
TeamLegrand 0:23cef74eac23 127 strcpy(snd,"AT\r\n");
TeamLegrand 0:23cef74eac23 128 SendCMD();
TeamLegrand 0:23cef74eac23 129 timeout=1;
TeamLegrand 0:23cef74eac23 130 getreply();
TeamLegrand 0:23cef74eac23 131 wait(1);
TeamLegrand 0:23cef74eac23 132 pc.printf("---------- Redemarrage du module ----------\r\n");
TeamLegrand 0:23cef74eac23 133 strcpy(snd,"AT+RST\r\n");
TeamLegrand 0:23cef74eac23 134 SendCMD();
TeamLegrand 0:23cef74eac23 135 timeout=5;
TeamLegrand 0:23cef74eac23 136 getreply();
TeamLegrand 0:23cef74eac23 137 pc.printf(buf);
TeamLegrand 0:23cef74eac23 138
TeamLegrand 0:23cef74eac23 139 wait(3);
TeamLegrand 0:23cef74eac23 140
TeamLegrand 0:23cef74eac23 141 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
TeamLegrand 0:23cef74eac23 142 pc.printf("\n---------- Mode Wifi ----------\r\n");
TeamLegrand 0:23cef74eac23 143 strcpy(snd, "AT+CWMODE=3\r\n");
TeamLegrand 0:23cef74eac23 144 SendCMD();
TeamLegrand 0:23cef74eac23 145 timeout=4;
TeamLegrand 0:23cef74eac23 146 getreply();
TeamLegrand 0:23cef74eac23 147 pc.printf(buf);
TeamLegrand 0:23cef74eac23 148
TeamLegrand 0:23cef74eac23 149 wait(2);
TeamLegrand 0:23cef74eac23 150
TeamLegrand 0:23cef74eac23 151 // set CIPMUX to 0=Single,1=Multi
TeamLegrand 0:23cef74eac23 152 pc.printf("\n---------- Choix de connexion multiple ou unique ----------\r\n");
TeamLegrand 0:23cef74eac23 153 strcpy(snd, "AT+CIPMUX=1\r\n");
TeamLegrand 0:23cef74eac23 154 SendCMD();
TeamLegrand 0:23cef74eac23 155 timeout=4;
TeamLegrand 0:23cef74eac23 156 getreply();
TeamLegrand 0:23cef74eac23 157 pc.printf(buf);
TeamLegrand 0:23cef74eac23 158
TeamLegrand 0:23cef74eac23 159 wait(2);
TeamLegrand 0:23cef74eac23 160
TeamLegrand 0:23cef74eac23 161 pc.printf("\n---------- Connexion au point d acces ----------\r\n");
TeamLegrand 0:23cef74eac23 162 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
TeamLegrand 0:23cef74eac23 163 strcpy(snd, "AT+CWJAP=\"");
TeamLegrand 0:23cef74eac23 164 strcat(snd, ssid);
TeamLegrand 0:23cef74eac23 165 strcat(snd, "\",\"");
TeamLegrand 0:23cef74eac23 166 strcat(snd, pwd);
TeamLegrand 0:23cef74eac23 167 strcat(snd, "\"\r\n");
TeamLegrand 0:23cef74eac23 168 SendCMD();
TeamLegrand 0:23cef74eac23 169 timeout=10;
TeamLegrand 0:23cef74eac23 170 getreply();
TeamLegrand 0:23cef74eac23 171 pc.printf(buf);
TeamLegrand 0:23cef74eac23 172
TeamLegrand 0:23cef74eac23 173 wait(5);
TeamLegrand 0:23cef74eac23 174
TeamLegrand 0:23cef74eac23 175 pc.printf("\n---------- Adresse IP local ----------\r\n");
TeamLegrand 0:23cef74eac23 176 strcpy(snd, "AT+CIFSR\r\n");
TeamLegrand 0:23cef74eac23 177 SendCMD();
TeamLegrand 0:23cef74eac23 178 timeout=3;
TeamLegrand 0:23cef74eac23 179 getreply();
TeamLegrand 0:23cef74eac23 180 pc.printf(buf);
TeamLegrand 0:23cef74eac23 181
TeamLegrand 0:23cef74eac23 182 wait(5);
TeamLegrand 0:23cef74eac23 183
TeamLegrand 0:23cef74eac23 184 pc.printf("\n---------- TCP Client ----------\r\n");
TeamLegrand 0:23cef74eac23 185 strcpy(snd, "AT+CIPSERVER=1\r\n");
TeamLegrand 0:23cef74eac23 186 SendCMD();
TeamLegrand 0:23cef74eac23 187 timeout=5;
TeamLegrand 0:23cef74eac23 188 getreply();
TeamLegrand 0:23cef74eac23 189 pc.printf(buf);
TeamLegrand 0:23cef74eac23 190
TeamLegrand 0:23cef74eac23 191 wait(5);
TeamLegrand 0:23cef74eac23 192
TeamLegrand 0:23cef74eac23 193 /* pc.printf("\n---------- DATA SEND ----------\r\n");
TeamLegrand 0:23cef74eac23 194 strcpy(snd, "AT+CIPSEND=0,4\r\n");
TeamLegrand 0:23cef74eac23 195 SendCMD();
TeamLegrand 0:23cef74eac23 196 timeout=5;
TeamLegrand 0:23cef74eac23 197 getreply();
TeamLegrand 0:23cef74eac23 198 pc.printf(buf);*/
TeamLegrand 0:23cef74eac23 199 }
TeamLegrand 0:23cef74eac23 200
TeamLegrand 0:23cef74eac23 201 void SendCMD()
TeamLegrand 0:23cef74eac23 202 {
TeamLegrand 0:23cef74eac23 203 esp.printf("%s", snd);
TeamLegrand 0:23cef74eac23 204 }
TeamLegrand 0:23cef74eac23 205
TeamLegrand 0:23cef74eac23 206
TeamLegrand 0:23cef74eac23 207
TeamLegrand 0:23cef74eac23 208 void getreply()
TeamLegrand 0:23cef74eac23 209 {
TeamLegrand 0:23cef74eac23 210 memset(buf, '\0', sizeof(buf));
TeamLegrand 0:23cef74eac23 211 t.start();
TeamLegrand 0:23cef74eac23 212 ended=0;
TeamLegrand 0:23cef74eac23 213 count=0;
TeamLegrand 0:23cef74eac23 214 while(!ended) {
TeamLegrand 0:23cef74eac23 215 if(esp.readable()) {
TeamLegrand 0:23cef74eac23 216 buf[count] = esp.getc();
TeamLegrand 0:23cef74eac23 217 count++;
TeamLegrand 0:23cef74eac23 218 }
TeamLegrand 0:23cef74eac23 219 if(t.read() > timeout) {
TeamLegrand 0:23cef74eac23 220 ended = 1;
TeamLegrand 0:23cef74eac23 221 t.stop();
TeamLegrand 0:23cef74eac23 222 t.reset();
TeamLegrand 0:23cef74eac23 223 }
TeamLegrand 0:23cef74eac23 224 }
TeamLegrand 0:23cef74eac23 225 }