TeamLegrand / Mbed 2 deprecated wifiserveur

Dependencies:   MODSERIAL mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <iostream>
00003 #include <string>
00004 #include <fstream>
00005 #include "MODSERIAL.h"
00006 
00007 MODSERIAL pc(USBTX, USBRX);
00008 MODSERIAL esp(p28, p27); // tx, rx
00009 DigitalOut reset(p26);
00010 Timer t;
00011 DigitalOut led(p7);
00012 DigitalOut vib(p5);
00013 DigitalOut buz(p6);
00014 
00015 int  count,ended,timeout;
00016 char buf[1024];
00017 char snd[255];
00018 
00019 char ssid[32] = "IOP-LEGRAND";     
00020 char pwd [32] = "1Up-8445";
00021 
00022 char reponse_esp[5];
00023 char reception_esp[3] = "OK";
00024 char test[5] = "test";
00025 
00026 char ledon[5] = "DELA";
00027 char ledoff[5] = "DELE";
00028 
00029 int longueur_recue = 0;
00030 int nouvelle_reception = 0;
00031 char recept[5] = {0};
00032 
00033 
00034 
00035 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),WIFI(), Donnee();
00036 
00037 using namespace std;
00038 
00039 int main()
00040 {
00041     reset=0; //hardware reset for 8266
00042     pc.baud(9600);  // set what you want here depending on your terminal program speed
00043     pc.printf("\f\n\r-------------Demarrage du module WIFI-------------\n\r");
00044     wait(0.5);
00045     reset=1;
00046     timeout=2;
00047     getreply();
00048 
00049     esp.baud(115200);   // change this to the new ESP8266 baudrate if it is changed at any time.
00050      
00051     ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
00052 
00053     pc.printf("\f\n\r- Module pret -\n\r");
00054 
00055     while(1){
00056         if(esp.readable()){
00057             wait(1);
00058         }
00059             while(esp.readable())
00060             {
00061             recept[longueur_recue]=esp.getc();
00062             longueur_recue++;
00063             nouvelle_reception = 1;
00064             }
00065             
00066         /*for(int i=0; i<5; i++){
00067             pc.printf("%c",recept);
00068         }*/
00069         
00070         //char c = esp.getc();
00071         //pc.printf("%c",recept);
00072     
00073         /*if(reception_esp == test){
00074         strcpy(snd, "AT+CIPSEND=0,4");
00075         strcat(snd, reponse_esp);
00076         SendCMD();
00077         timeout=10;
00078         getreply();
00079         pc.printf(buf);
00080         }*/
00081 
00082         // VIBREUR
00083         /*if(c == 'a'){
00084             vib=1;
00085             }
00086         if(c == 'b'){
00087             vib=0;
00088             } */
00089             
00090              // LED  
00091             if(recept == ledon){
00092                  led=1;
00093                  pc.printf("> Les leds sont ON -\n\r");
00094              }
00095             if(recept == ledoff){
00096                 led=0;
00097                 pc.printf("> Les leds sont OFF -\n\r");
00098              } 
00099             
00100         // BUZZER    
00101         /*if(c == 'e'){
00102             buz=1;
00103             }
00104         if(c == 'f'){
00105             buz=0;
00106             } 
00107         // TOTAL
00108         if(c =='g'){
00109             buz=1;
00110             led=1;
00111             vib=1;
00112             }
00113         if(c =='h'){
00114             buz=0;
00115             led=0;
00116             vib=0;
00117             }*/
00118     }
00119         
00120 }
00121 
00122 
00123 //  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
00124 void ESPconfig()
00125 {
00126     wait(5);
00127     strcpy(snd,"AT\r\n");
00128     SendCMD();
00129     timeout=1;
00130     getreply();
00131     wait(1);
00132     pc.printf("---------- Redemarrage du module ----------\r\n");
00133     strcpy(snd,"AT+RST\r\n");
00134     SendCMD();
00135     timeout=5;
00136     getreply();
00137     pc.printf(buf);
00138 
00139     wait(3);
00140 
00141     // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
00142     pc.printf("\n---------- Mode Wifi ----------\r\n");
00143     strcpy(snd, "AT+CWMODE=3\r\n");
00144     SendCMD();
00145     timeout=4;
00146     getreply();
00147     pc.printf(buf);
00148 
00149     wait(2);
00150 
00151     // set CIPMUX to 0=Single,1=Multi
00152     pc.printf("\n---------- Choix de connexion multiple ou unique ----------\r\n");
00153     strcpy(snd, "AT+CIPMUX=1\r\n");
00154     SendCMD();
00155     timeout=4;
00156     getreply();
00157     pc.printf(buf);
00158     
00159     wait(2);
00160 
00161     pc.printf("\n---------- Connexion au point d acces ----------\r\n");
00162     pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
00163     strcpy(snd, "AT+CWJAP=\"");
00164     strcat(snd, ssid);
00165     strcat(snd, "\",\"");
00166     strcat(snd, pwd);
00167     strcat(snd, "\"\r\n");
00168     SendCMD();
00169     timeout=10;
00170     getreply();
00171     pc.printf(buf);
00172 
00173     wait(5);
00174 
00175     pc.printf("\n---------- Adresse IP local ----------\r\n");
00176     strcpy(snd, "AT+CIFSR\r\n");
00177     SendCMD();
00178     timeout=3;
00179     getreply();
00180     pc.printf(buf);
00181 
00182     wait(5);
00183 
00184     pc.printf("\n---------- TCP Client ----------\r\n");
00185     strcpy(snd, "AT+CIPSERVER=1\r\n");
00186     SendCMD();
00187     timeout=5;
00188     getreply();
00189     pc.printf(buf);   
00190 
00191     wait(5);
00192 
00193    /* pc.printf("\n---------- DATA SEND ----------\r\n");
00194     strcpy(snd, "AT+CIPSEND=0,4\r\n");
00195     SendCMD();
00196     timeout=5;
00197     getreply();
00198     pc.printf(buf);*/    
00199 }
00200 
00201 void SendCMD()
00202 {
00203     esp.printf("%s", snd);
00204 }
00205 
00206 
00207 
00208 void getreply()
00209 {
00210     memset(buf, '\0', sizeof(buf));
00211     t.start();
00212     ended=0;
00213     count=0;
00214     while(!ended) {
00215         if(esp.readable()) {
00216             buf[count] = esp.getc();
00217             count++;
00218         }
00219         if(t.read() > timeout) {
00220             ended = 1;
00221             t.stop();
00222             t.reset();
00223         }
00224     }
00225 }