mbed_robotcar / Mbed 2 deprecated wifi-demo2-0730

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // ESP8266 Static page WEB server to control Mbed
00002 
00003 #include "mbed.h"
00004 //#include "DS18B20.h"
00005 
00006 Serial pc(USBTX, USBRX);
00007 Serial esp(p28, p27); // tx, rx
00008 //Serial esp(p9, p10); // tx, rx
00009 
00010 //DS18B20 thermom(A0, DS18B20::RES_12_BIT);
00011 
00012 // Standard Mbed LED definitions
00013 DigitalOut  led1(LED1);      // (PTB18)
00014 DigitalOut  led2(LED2);    // (PTB19)
00015 DigitalOut  led3(LED3);     // (PTD1)
00016 
00017 // Digital Out and In pins, can be configured to any suitable pin depending on Platform
00018 DigitalOut  Out1(p6);
00019 DigitalOut  Out2(p7);
00020 DigitalOut  Out3(p8);
00021 DigitalOut  reset(p26);
00022 
00023 
00024 DigitalOut  ledR(p17);
00025 DigitalOut  ledG(p16);
00026 
00027 DigitalIn  In1(p9);
00028 DigitalIn  In2(p10);
00029 DigitalIn  In3(p11);
00030 
00031 PwmOut speaker(p21);
00032 AnalogIn   Ain1(p18);
00033 AnalogIn   Ain2(p19);
00034 
00035 Timer t1;
00036 Timer t2;
00037 
00038 struct tm t;
00039 
00040 int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
00041 int bufl, ipdLen, linkID, weberror, webcounter;
00042 float temperature, AdcIn, Ht;
00043 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
00044 char Vcc[10];
00045 char Temp[10];
00046 char temp[10];
00047 char webcount[8];
00048 char lasthit[30];
00049 char timebuf[30];
00050 char type[16];
00051 char type1[16];
00052 char channel[2];
00053 char cmdbuff[32];
00054 char replybuff[1024];
00055 
00056 char webdata[1024]; // This may need to be bigger depending on WEB browser used
00057 char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
00058 
00059 //char webdata[4096]; // This may need to be bigger depending on WEB browser used
00060 //char webbuff[4096*4];     // Currently using 1986 characters, Increase this if more web page data added
00061 
00062 
00063 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(),touchuan();
00064 void gettime(),gettemp(),getbattery(),setRTC(),beep();
00065 
00066 // manual set RTC values
00067 int minute      =00;    // 0-59
00068 int hour        =12;    // 2-23
00069 int dayofmonth  =26;    // 1-31
00070 int month       =8;     // 1-12
00071 int year        =15;    // last 2 digits
00072 
00073 int port        =80;  // set server port
00074 int SERVtimeout =5;    // set server timeout in seconds in case link breaks.
00075 
00076 // Serial Interrupt read ESP data
00077 void callback()
00078 {
00079     //pc.printf("\n\r------------ callback is being called --------------\n\r");
00080     led3=1;
00081     while (esp.readable()) {
00082         webbuff[count] = esp.getc();
00083         count++;
00084     }
00085     if(strlen(webbuff)>bufflen) {
00086         pc.printf("\f\n\r------------ webbuff over bufflen --------------\n\r");
00087         DataRX=1;
00088         led3=0;
00089     }
00090 }
00091 
00092 int main()
00093 {
00094     reset=0;
00095     pc.baud(115200);
00096 
00097     pc.printf("\f\n\r------------ ESP8266 Hardware Reset psq --------------\n\r");
00098     wait(0.5);
00099     reset=1;
00100     led1=1,led2=0,led3=0;
00101     timeout=6000;
00102     getcount=500;
00103     getreply();
00104     esp.baud(115200);   // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
00105     if (time(NULL) < 1420070400) {
00106         setRTC();
00107     }
00108     beep();
00109     startserver();
00110     
00111     while(1) {
00112         if(DataRX==1) {
00113             pc.printf("\f\n\r------------ main while > if --------------\n\r");
00114         
00115             ReadWebData();
00116             beep();
00117             if (servreq == 1 && weberror == 0) {
00118                 pc.printf("\f\n\r------------ befor send page --------------\n\r");        
00119                 sendpage();
00120             }
00121             esp.attach(&callback);
00122             pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type);
00123             pc.printf("\n\n  HTTP Packet: \n\n%s\n", webdata);
00124             pc.printf("  Web Characters sent : %d\n\n", bufl);
00125             pc.printf("  -------------------------------------\n\n");
00126             strcpy(lasthit, timebuf);
00127             servreq=0;
00128         }
00129     }
00130 }
00131 // Static WEB page
00132 void sendpage()
00133 {
00134     gettemp();
00135     getbattery();
00136     gettime();
00137 
00138 // WEB page data
00139 
00140 strcpy(webbuff, "<!DOCTYPE html>");
00141 strcat(webbuff, "<html><head><title>RobotCar</title><meta name='viewport' content='width=device-width'/>");    
00142 strcat(webbuff, "</head><body><center><p><strong>Robot Car Remot Controller");
00143 strcat(webbuff, "</strong></p><td style='vertical-align:top;'><strong>Battery level ");
00144 strcat(webbuff, "<input type=\"text\" id=\"leftms\" size=4 value=250>%</strong>");
00145 strcat(webbuff, "</td></p><tr ><strong>Now speed : </strong></tr><nobr id=\"speprint\">ready");
00146 strcat(webbuff, "</nobr><br><tr ><strong>Now action : </strong></tr>");
00147 strcat(webbuff, "<nobr id=\"funprint\">ready</nobr><table><tr><td></td><td>");
00148 
00149 strcat(webbuff, "<button id='gobtn' type='button' style='width:100px;height:60px'  value=\"GO\"  onClick='send_mes(this.id,this.value)'>GO");
00150 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
00151 strcat(webbuff, "<button id='leftbtn' type='button' style='width:100px;height:60px' value=\"LEFT\"  onClick='send_mes(this.id,this.value)' >LEFT");
00152 strcat(webbuff, "</button></td><td>");
00153 /*
00154 strcat(webbuff, "<button id='stopbtn' type='button' style='width:100px;height:60px' value=\"STOP\"  onClick='send_mes(this.id,this.value)' >STOP");
00155 strcat(webbuff, "</button></td><td>");
00156 strcat(webbuff, "<button id='rightbtn' type='button' style='width:100px;height:60px' value=\"RIGHT\"  onClick='send_mes(this.id,this.value)' >RIGHT");
00157 strcat(webbuff, "</button></td></tr><td></td><td>");
00158 strcat(webbuff, "<button id='backbtn' type='button' style='width:100px;height:60px' value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
00159 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
00160 
00161 
00162 
00163 //begin
00164 strcat(webbuff, "<strong>Addition functions:</strong><table>");
00165 strcat(webbuff, "<td>");//ok
00166 strcat(webbuff, "<button id='avoidbtn' type='button' style='width:100px;height:60px' value=\"AVOIDANCE\"  onClick='send_mes_fun(this.id,this.value)' >");
00167 strcat(webbuff, "AVOIDANCE</button></td><td>");
00168 strcat(webbuff, "<button id='tracebtn' type='button' style='width:100px;height:60px' value=\"LINE TRACE\"  onClick='send_mes_fun(this.id,this.value)' >LINE TRACE");
00169 strcat(webbuff, "</button></td><td>");
00170 strcat(webbuff, "<button id='defbtn' type='button' style='width:100px;height:60px' value=\"DEFAULT\"  onClick='send_mes_fun(this.id,this.value)' >DEFAULT");
00171 strcat(webbuff, "</button></td></table><table><td>");
00172 strcat(webbuff, "</td><strong>Speed level:</strong>");
00173 strcat(webbuff, "</table><td>");
00174 strcat(webbuff, "<button id='sp1btn' type='button' style='width:100px;height:60px' value=\"SLOW\"  onClick='send_mes_spe(this.id,this.value)' >SLOW");
00175 strcat(webbuff, "</button></td><td>");
00176 strcat(webbuff, "<button id='sp2btn' type='button' style='width:100px;height:60px' value=\"FAST\"  onClick='send_mes_spe(this.id,this.value)' >FAST");
00177 strcat(webbuff, "</button>");
00178 strcat(webbuff, "</td>");    
00179 strcat(webbuff, "<td>");
00180 strcat(webbuff, "<button id='sp3btn' type='button' style='width:100px;height:60px' value=\"FLY\"  onClick='send_mes_spe(this.id,this.value)' >FLY");
00181 strcat(webbuff, "</button>");
00182 strcat(webbuff, "</td>");    
00183 strcat(webbuff, "<table>");        
00184 strcat(webbuff, "<td>");
00185 strcat(webbuff, "</td>");    
00186 strcat(webbuff, "</table>");    //end
00187 
00188 */
00189 
00190 strcat(webbuff, "</center>");
00191 strcat(webbuff, "</body>");
00192 strcat(webbuff, "</html>");
00193 strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
00194 strcat(webbuff, "function send_mes(btnmes,btnval){");
00195 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
00196 strcat(webbuff, "htmlacs(url);");
00197 strcat(webbuff, "console.log(url);");
00198 strcat(webbuff, "}");
00199 strcat(webbuff, "function htmlacs(url) {");
00200 strcat(webbuff, "var xhr = new XMLHttpRequest();");
00201 strcat(webbuff, "xhr.open(\"GET\", url);");
00202 strcat(webbuff, "xhr.send(\"\");");
00203 strcat(webbuff, "}");
00204 strcat(webbuff, "function send_mes_spe(btnmes,btnval){");
00205 strcat(webbuff, "console.log(btnmes);");
00206 strcat(webbuff, "if(document.getElementById(\"speprint\")){");
00207 strcat(webbuff, "document.getElementById(\"speprint\").innerHTML=btnval;");
00208 strcat(webbuff, "}");
00209 strcat(webbuff, "}");
00210 strcat(webbuff, "function send_mes_fun(btnmes,btnval){");
00211 strcat(webbuff, "console.log(btnmes);");
00212 strcat(webbuff, "if(document.getElementById(\"funprint\")){");
00213 strcat(webbuff, "document.getElementById(\"funprint\").innerHTML=btnval;");
00214 strcat(webbuff, "}");
00215 strcat(webbuff, "}");
00216 strcat(webbuff, "</script>");
00217 // end of WEB page data
00218     bufl = strlen(webbuff); // get total page buffer length
00219     //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
00220     
00221     sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
00222     timeout=500;
00223     getcount=40;
00224     SendCMD();
00225     getreply();
00226     pc.printf(replybuff);
00227     pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, 2048);
00228     
00229     pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
00230     
00231     //pastthrough mode
00232     SendWEB();  // send web page
00233    
00234     memset(webbuff, '\0', sizeof(webbuff));
00235     sendcheck();
00236 }
00237 
00238 // Large WEB buffer data send
00239 void SendWEB()
00240 {
00241     int i=0;
00242     if(esp.writeable()) {
00243         while(webbuff[i]!='\0') {
00244             esp.putc(webbuff[i]);
00245                  /*
00246           //****
00247           //output at command when 2000 
00248             if(i==1499){    
00249                     wait_ms(10);
00250                     sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, 1500); // send IPD link channel and buffer character length.
00251                     pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, 1500);
00252                     timeout=600;
00253                     getcount=50;
00254                     SendCMD();
00255                     getreply();
00256                     pc.printf(replybuff);
00257                     pc.printf("\r\n+++++++++++++++++++\r\n");
00258                     
00259                }    
00260                */
00261     //****  
00262             i++;     
00263             pc.printf("%c",webbuff[i]);
00264         }
00265     }
00266     
00267     
00268     //****
00269     /*
00270     sprintf(cmdbuff,"AT+CIPCLOSE\r\n"); // send IPD link channel and buffer character length.        
00271     SendCMD();
00272     */
00273     //*****
00274     
00275      pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i);
00276 }
00277 
00278 
00279 //  wait for ESP "SEND OK" reply, then close IP to load web page
00280 
00281 //touchuan mode
00282 void touchuan(){
00283     strcpy(cmdbuff, "AT+CIPMUX=0\r\n");
00284     pc.printf("\n++++++++++ AT+CIPMUX=0\r\n+++++++++");
00285     timeout=500;
00286     getcount=40;
00287     SendCMD();
00288     getreply();
00289     pc.printf(replybuff);
00290     strcpy(cmdbuff, "AT+CIPSTART=\"TCP\",\"192.168.4.2\",8080\r\n"); // send IPD link channel and buffer character length.
00291     pc.printf("\n++++++++++ AT+CIPSTART=0\r\n+++++++++");
00292     timeout=500;
00293     getcount=40;
00294     SendCMD();
00295     getreply();
00296     pc.printf(replybuff);
00297     strcpy(cmdbuff, "AT+CIPMODE=1\r\n"); // send IPD link channel and buffer character length.
00298     pc.printf("\n++++++++++ AT+CIPMODE=1\r\n+++++++++");
00299     timeout=500;
00300     getcount=40;
00301     SendCMD();
00302     getreply();
00303     pc.printf(replybuff);
00304     strcpy(cmdbuff, "AT+CIPSEND\r\n"); // send IPD link channel and buffer character length.
00305     pc.printf("\n++++++++++ AT+CIPSEND\r\n+++++++++");
00306     timeout=500;
00307     getcount=40;
00308     SendCMD();
00309     getreply();
00310     pc.printf(replybuff);
00311 }
00312     
00313 void sendcheck()
00314 {
00315     weberror=1;
00316     timeout=500;
00317     getcount=24;
00318     t2.reset();
00319     t2.start();
00320     while(weberror==1 && t2.read() <5) {
00321         getreply();
00322         if (strstr(replybuff, "SEND OK") != NULL) {
00323             weberror=0;   // wait for valid SEND OK
00324         }
00325     }
00326     if(weberror==1) { // restart connection
00327         strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
00328         timeout=500;
00329         getcount=10;
00330         SendCMD();
00331         getreply();
00332         pc.printf(replybuff);
00333         sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
00334         timeout=500;
00335         getcount=10;
00336         SendCMD();
00337         getreply();
00338         pc.printf(replybuff);
00339     } else {
00340         sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
00341         SendCMD();
00342         getreply();
00343         pc.printf(replybuff);
00344     }
00345     t2.reset();
00346 }
00347 
00348 // Reads and processes GET and POST web data
00349 void ReadWebData()
00350 {
00351     wait_ms(200);
00352     esp.attach(NULL);
00353     count=0;
00354     DataRX=0;
00355     weberror=0;
00356     memset(webdata, '\0', sizeof(webdata));
00357     int x = strcspn (webbuff,"+");
00358     if(x) {
00359         strcpy(webdata, webbuff + x);
00360         weberror=0;
00361         int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
00362         //int i=0;
00363         pc.printf("+++++++++++++++++succed+++++++++++++++++++++");
00364         pc.printf("%s",webdata);
00365         
00366      
00367         
00368         if( strstr(webdata, "LEFT") != NULL ) {
00369             //ledR=!ledR;
00370              led1=!led1;
00371              pc.printf("+++++++++++++++++ledR+++++++++++++++++++++\r\n");
00372        
00373         }
00374         
00375         if( strstr(webdata, "RIGHT") != NULL ) {
00376             ledG=!ledG;
00377            pc.printf("+++++++++++++++++ledG+++++++++++++++++++++\r\n");
00378         }
00379         pc.printf("+++++++++++++++++succed+++++++++++++++++++++");
00380         
00381         
00382         if( strstr(webdata, "sp3btn=0") != NULL ) {
00383             led1=!led1;
00384            
00385         }
00386         if( strstr(webdata, "led1=1") != NULL ) {
00387             led1=1;
00388         }
00389         if( strstr(webdata, "led1=0") != NULL ) {
00390             led1=0;
00391         }
00392         if( strstr(webdata, "Out1=1") != NULL ) {
00393             Out1=1;
00394         }
00395         if( strstr(webdata, "Out1=0") != NULL ) {
00396             Out1=0;
00397         }
00398         if( strstr(webdata, "Out2=1") != NULL ) {
00399             Out2=1;
00400         }
00401         if( strstr(webdata, "Out2=0") != NULL ) {
00402             Out2=0;
00403         }
00404         if( strstr(webdata, "Out3=1") != NULL ) {
00405             Out3=1;
00406         }
00407         if( strstr(webdata, "Out3=0") != NULL ) {
00408             Out3=0;
00409         }
00410         sprintf(channel, "%d",linkID);
00411         if (strstr(webdata, "GET") != NULL) {
00412             servreq=1;
00413         }
00414         if (strstr(webdata, "POST") != NULL) {
00415             servreq=1;
00416         }
00417         webcounter++;
00418         sprintf(webcount, "%d",webcounter);
00419     } else {
00420         memset(webbuff, '\0', sizeof(webbuff));
00421         esp.attach(&callback);
00422         weberror=1;
00423     }
00424 }
00425 // Starts and restarts webserver if errors detected.
00426 void startserver()
00427 {
00428     gettemp();
00429     gettime();
00430     pc.printf("\n\n RTC time   %s\r\n\n",timebuf);
00431     pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
00432     strcpy(cmdbuff,"AT+RST\r\n");
00433     timeout=8000;
00434     getcount=1000;
00435     SendCMD();
00436     getreply();
00437     pc.printf(replybuff);
00438     pc.printf("%d",count);
00439     if (strstr(replybuff, "OK") != NULL) {
00440         pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
00441         strcpy(cmdbuff, "AT+CIPMUX=1\r\n");  // set multiple connections.
00442         timeout=500;
00443         getcount=20;
00444         SendCMD();
00445         getreply();
00446         pc.printf(replybuff);
00447         sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
00448         timeout=500;
00449         getcount=20;
00450         SendCMD();
00451         getreply();
00452         pc.printf(replybuff);
00453         wait(1);
00454         sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
00455         timeout=500;
00456         getcount=50;
00457         SendCMD();
00458         getreply();
00459         pc.printf(replybuff);
00460         wait(5);
00461         pc.printf("\n Getting Server IP \r\n");
00462         strcpy(cmdbuff, "AT+CIFSR\r\n");
00463         timeout=2500;
00464         getcount=200;
00465         while(weberror==0) {
00466             SendCMD();
00467             getreply();
00468             if (strstr(replybuff, "0.0.0.0") == NULL) {
00469                 weberror=1;   // wait for valid IP
00470             }
00471         }
00472         pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
00473         pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
00474         replybuff[strlen(replybuff)-1] = '\0';
00475         //char* IP = replybuff + 5;
00476         sprintf(webdata,"%s", replybuff);
00477         pc.printf(webdata);
00478         led2=1;
00479         bufflen=200;
00480         count=0;
00481         pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
00482         esp.attach(&callback);
00483     } else {
00484         pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
00485         while(1) {}
00486     }
00487     t2.reset();
00488     t2.start();
00489     beep();
00490 }
00491 // ESP Command data send
00492 void SendCMD()
00493 {
00494     esp.printf("%s", cmdbuff);
00495 }
00496 // Get Command and ESP status replies
00497 void getreply()
00498 {
00499     memset(replybuff, '\0', sizeof(replybuff));
00500     t1.reset();
00501     t1.start();
00502     replycount=0;
00503     while(t1.read_ms()< timeout && replycount < getcount) {
00504         if(esp.readable()) {
00505             replybuff[replycount] = esp.getc();
00506             replycount++;
00507         }
00508     }
00509     t1.stop();
00510 }
00511 // Analog in example
00512 void getbattery()
00513 {
00514     AdcIn=Ain1.read();
00515     Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
00516     sprintf(Vcc,"%2.3f",Ht);
00517 }
00518 // Temperature example
00519 void gettemp()
00520 {
00521  
00522     AdcIn=Ain2.read();
00523     Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy  
00524     sprintf(Temp,"%2.3f",Ht);
00525 }
00526 // Get RTC time
00527 void gettime()
00528 {
00529     time_t seconds = time(NULL);
00530     strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
00531 }
00532 
00533 void beep()
00534 {
00535     speaker.period(1.0/2000); // 2000hz period
00536     speaker = 0.5; //50% duty cycle - max volume
00537     wait_ms(60);
00538     speaker=0.0; // turn off audio
00539 }
00540 
00541 void setRTC()
00542 {
00543     t.tm_sec = (0);             // 0-59
00544     t.tm_min = (minute);        // 0-59
00545     t.tm_hour = (hour);         // 0-23
00546     t.tm_mday = (dayofmonth);   // 1-31
00547     t.tm_mon = (month-1);       // 0-11  "0" = Jan, -1 added for Mbed RCT clock format
00548     t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
00549     set_time(mktime(&t));       // set RTC clock
00550 }