In the words of Tim Gun... make it work

Dependencies:   mbed

Fork of ESP8266-configuaration-baudrate by Paul Staron

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(USBTX, USBRX);
00004 Serial esp(p13, p14); // tx, rx 
00005 
00006 Timer t;
00007 
00008 int  count,ended,timeout;
00009 char buf[1024];
00010 char snd[255];
00011 
00012 char ssid[32] = "hp-setup-1";     // enter router ssid inside the quotes
00013 char pwd [32] = "WelcomeHal"; // enter router password inside the quotes
00014 
00015 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
00016 
00017 
00018 int main() {
00019     
00020     pc.baud(115200);  // set what you want here depending on your terminal program speed
00021     esp.baud(9600);   // change this to the new ESP8266 baudrate if it is changed at any time.    
00022     
00023     ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
00024 
00025     ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
00026     
00027    
00028    // contiuosly get AP list and IP
00029     while(1){
00030         pc.printf("\n---------- Listing Acces Points ----------\r\n");
00031         strcpy(snd, "AT+CWLAP\r\n");
00032         SendCMD();
00033         timeout=3;
00034         getreply();   
00035         pc.printf(buf);
00036 
00037         pc.printf("\n---------- Get IP's ----------\r\n"); 
00038         strcpy(snd, "AT+CIFSR\r\n");
00039         SendCMD();
00040         timeout=2;
00041         getreply(); 
00042         pc.printf(buf);
00043     }  
00044   
00045      
00046 }
00047 
00048 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
00049 void ESPsetbaudrate()
00050 {
00051     strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
00052     SendCMD();    
00053     esp.baud(115200);
00054 }    
00055 
00056 //  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
00057 void ESPconfig()
00058 {
00059 
00060     pc.printf("---------- Starting ESP Config ----------\r\n\n");
00061     wait(2);
00062     pc.printf("---------- Reset & get Firmware ----------\r\n");
00063     strcpy(snd,"AT+RST\r\n");
00064     SendCMD();
00065     timeout=2;
00066     getreply();
00067     pc.printf(buf);
00068     
00069     wait(1);   
00070    
00071     pc.printf("\n---------- Get Version ----------\r\n");
00072     strcpy(snd,"AT+GMR\r\n");
00073     SendCMD();
00074     timeout=1;
00075     getreply();
00076     pc.printf(buf);    
00077     
00078     wait(1);
00079         
00080     // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
00081     pc.printf("\n---------- Setting Mode ----------\r\n");
00082     strcpy(snd, "AT+CWMODE=1\r\n");
00083     SendCMD();
00084     timeout=1;
00085     getreply();
00086     pc.printf(buf);
00087     
00088     wait(1);
00089     
00090     // set CIPMUX to 0=Single,1=Multi 
00091     pc.printf("\n---------- Setting Connection Mode ----------\r\n");
00092     strcpy(snd, "AT+CIPMUX=1\r\n"); 
00093     SendCMD();
00094     timeout=1;
00095     getreply();
00096     pc.printf(buf);
00097     
00098     wait(1);
00099     /*
00100     pc.printf("\n---------- Listing Acces Points ----------\r\n");
00101     strcpy(snd, "AT+CWLAP\r\n");
00102     SendCMD();
00103     timeout=3;
00104     getreply();   
00105     pc.printf(buf);
00106     
00107     wait(1);    
00108     */
00109     pc.printf("\n---------- Connecting to AP ----------\r\n");
00110     pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
00111     strcpy(snd, "AT+CWJAP=\"");
00112     strcat(snd, ssid);
00113     strcat(snd, "\",\"");
00114     strcat(snd, pwd);
00115     strcat(snd, "\"\r\n");   
00116     SendCMD();
00117     timeout=8;
00118     getreply();    
00119     pc.printf(buf);
00120     
00121     wait(5);
00122     
00123     pc.printf("\n---------- Get IP's ----------\r\n"); 
00124     strcpy(snd, "AT+CIFSR\r\n");
00125     SendCMD();
00126     timeout=2;
00127     getreply(); 
00128     pc.printf(buf);
00129     
00130     wait(1);
00131     
00132     pc.printf("\n---------- Get Connection Status ----------\r\n"); 
00133     strcpy(snd, "AT+CIPSTATUS\r\n");
00134     SendCMD();
00135     timeout=2;
00136     getreply(); 
00137     pc.printf(buf);   
00138     
00139     pc.printf("\n\n\n  If you get a valid IP, ESP8266 has been set up.\r\n");
00140     pc.printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
00141 } 
00142 
00143 void SendCMD()
00144 {    
00145     esp.printf("%s", snd);    
00146 } 
00147 
00148 void getreply()
00149 {    
00150     memset(buf, '\0', sizeof(buf));
00151     t.start();
00152     ended=0;count=0;
00153     while(!ended) {
00154         if(esp.readable()) {
00155             buf[count] = esp.getc();count++;
00156             }
00157         if(t.read() > timeout) {
00158                 ended = 1;t.stop();t.reset();
00159             }
00160         }   
00161 }