real time wifi

Dependencies:   RemoteIR TextLCD

Committer:
faker_71
Date:
Thu Sep 03 00:30:32 2020 +0000
Revision:
4:a365d6acec8f
Parent:
3:4c5c556c97a3
Child:
5:4026062a6fdf
0902

Who changed what in which revision?

UserRevisionLine numberNew contents of line
faker_71 0:2e9a86765692 1 #include "mbed.h"
faker_71 0:2e9a86765692 2 #include "ReceiverIR.h"
faker_71 0:2e9a86765692 3 #include "rtos.h"
faker_71 0:2e9a86765692 4 #include <stdint.h>
faker_71 0:2e9a86765692 5 #include "platform/mbed_thread.h"
faker_71 0:2e9a86765692 6 #include "TextLCD.h"
faker_71 0:2e9a86765692 7 RawSerial pc(USBTX, USBRX);
faker_71 0:2e9a86765692 8 //Serial esp(p28, p27); // tx, rx
faker_71 0:2e9a86765692 9
faker_71 0:2e9a86765692 10 RawSerial esp(p13, p14); // tx, rx
faker_71 0:2e9a86765692 11 // Standard Mbed LED definitions
faker_71 0:2e9a86765692 12 DigitalOut led1(LED1);
faker_71 0:2e9a86765692 13 DigitalOut led2(LED2);
faker_71 0:2e9a86765692 14 DigitalOut led3(LED3);
faker_71 0:2e9a86765692 15 DigitalOut led4(LED4);
faker_71 4:a365d6acec8f 16 DigitalOut pred(p16);
faker_71 4:a365d6acec8f 17 DigitalOut pblue(p19);
faker_71 0:2e9a86765692 18
faker_71 0:2e9a86765692 19 Timer t1;
faker_71 0:2e9a86765692 20 Timer t2;
faker_71 0:2e9a86765692 21
faker_71 0:2e9a86765692 22 struct tm t;
faker_71 0:2e9a86765692 23
faker_71 0:2e9a86765692 24
faker_71 0:2e9a86765692 25 int bufflen, DataRX, count_wifi, getcount, replycount, servreq, timeout;
faker_71 0:2e9a86765692 26 int bufl, ipdLen, linkID, weberror, webcounter,click_flag;
faker_71 0:2e9a86765692 27 float temperature, AdcIn, Ht;
faker_71 0:2e9a86765692 28 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
faker_71 0:2e9a86765692 29 char Vcc[10];
faker_71 0:2e9a86765692 30 char webcount[8];
faker_71 0:2e9a86765692 31 char type[16];
faker_71 0:2e9a86765692 32 char type1[16];
faker_71 0:2e9a86765692 33 char channel[2];
faker_71 0:2e9a86765692 34 char cmdbuff[32];
faker_71 0:2e9a86765692 35 char replybuff[1024];
faker_71 4:a365d6acec8f 36 bool click_log = false;
faker_71 0:2e9a86765692 37 char webdata[1024]; // This may need to be bigger depending on WEB browser used
faker_71 0:2e9a86765692 38 char webbuff[4096*4]; // Currently using 1986 characters, Increase this if more web page data added
faker_71 0:2e9a86765692 39
faker_71 0:2e9a86765692 40
faker_71 2:1bf376317a43 41 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),sendpage2(),SendWEB(),sendcheck(),touchuan();
faker_71 0:2e9a86765692 42 void wifi(void const *argument);
faker_71 0:2e9a86765692 43
faker_71 0:2e9a86765692 44 Thread wifi_thread(wifi, NULL, osPriorityNormal); // decodeIRをスレッド化 :+3
faker_71 2:1bf376317a43 45 int count_test=1; //test
faker_71 2:1bf376317a43 46 char battery_ch[8];
faker_71 0:2e9a86765692 47 int port =80; // set server port
faker_71 0:2e9a86765692 48 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
faker_71 0:2e9a86765692 49
faker_71 0:2e9a86765692 50 // Serial Interrupt read ESP data
faker_71 0:2e9a86765692 51 void callback()
faker_71 0:2e9a86765692 52 {
faker_71 0:2e9a86765692 53 //pc.printf("\n\r------------ callback is being called --------------\n\r");
faker_71 4:a365d6acec8f 54 led3=1;
faker_71 4:a365d6acec8f 55
faker_71 0:2e9a86765692 56 while (esp.readable()) {
faker_71 4:a365d6acec8f 57
faker_71 0:2e9a86765692 58 webbuff[count_wifi] = esp.getc();
faker_71 0:2e9a86765692 59 count_wifi++;
faker_71 0:2e9a86765692 60 }
faker_71 4:a365d6acec8f 61
faker_71 0:2e9a86765692 62 if(strlen(webbuff)>bufflen) {
faker_71 0:2e9a86765692 63 pc.printf("\f\n\r------------ webbuff over bufflen --------------\n\r");
faker_71 0:2e9a86765692 64 DataRX=1;
faker_71 0:2e9a86765692 65 led3=0;
faker_71 0:2e9a86765692 66 }
faker_71 0:2e9a86765692 67 }
faker_71 0:2e9a86765692 68
faker_71 0:2e9a86765692 69 void wifi(void const *argument){
faker_71 0:2e9a86765692 70 pc.baud(115200);
faker_71 0:2e9a86765692 71
faker_71 0:2e9a86765692 72 pc.printf("\f\n\r------------ ESP8266 Hardware Reset psq --------------\n\r");
faker_71 0:2e9a86765692 73 wait(0.5);
faker_71 0:2e9a86765692 74 led1=1,led2=0,led3=0;
faker_71 0:2e9a86765692 75 timeout=6000;
faker_71 0:2e9a86765692 76 getcount=500;
faker_71 0:2e9a86765692 77 getreply();
faker_71 0:2e9a86765692 78 esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
faker_71 0:2e9a86765692 79 startserver();
faker_71 0:2e9a86765692 80
faker_71 0:2e9a86765692 81 while(1) {
faker_71 0:2e9a86765692 82 if(DataRX==1) {
faker_71 0:2e9a86765692 83 pc.printf("\f\n\r------------ main while > if --------------\n\r");
faker_71 4:a365d6acec8f 84 // click_flag = 1;
faker_71 4:a365d6acec8f 85 click_log=false;
faker_71 0:2e9a86765692 86 ReadWebData();
faker_71 4:a365d6acec8f 87 // pc.printf("\f\n\r------------ click_flag=%d --------------\n\r",click_flag);
faker_71 0:2e9a86765692 88 //if ((servreq == 1 && weberror == 0) && click_flag == 1) {
faker_71 4:a365d6acec8f 89 if (servreq == 1 && weberror == 0 && click_log == false) {
faker_71 0:2e9a86765692 90 pc.printf("\f\n\r------------ befor send page --------------\n\r");
faker_71 0:2e9a86765692 91 sendpage();
faker_71 0:2e9a86765692 92 }
faker_71 4:a365d6acec8f 93 // pc.printf("\f\n\r------------ send_check begin --------------\n\r");
faker_71 0:2e9a86765692 94
faker_71 0:2e9a86765692 95 //sendcheck();
faker_71 4:a365d6acec8f 96 pc.printf("\f\n\r------------ befor attach callback--------------\n\r");
faker_71 4:a365d6acec8f 97 // this is neccessary 0902
faker_71 4:a365d6acec8f 98 memset(webbuff, '\0', sizeof(webbuff));
faker_71 4:a365d6acec8f 99
faker_71 0:2e9a86765692 100 esp.attach(&callback);
faker_71 0:2e9a86765692 101 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);
faker_71 0:2e9a86765692 102 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
faker_71 0:2e9a86765692 103 pc.printf(" Web Characters sent : %d\n\n", bufl);
faker_71 0:2e9a86765692 104 pc.printf(" -------------------------------------\n\n");
faker_71 0:2e9a86765692 105 servreq=0;
faker_71 0:2e9a86765692 106 }
faker_71 0:2e9a86765692 107 }
faker_71 0:2e9a86765692 108 }
faker_71 0:2e9a86765692 109 int main()
faker_71 0:2e9a86765692 110 {
faker_71 0:2e9a86765692 111 while(1){
faker_71 0:2e9a86765692 112 Thread::wait(100);
faker_71 0:2e9a86765692 113 }
faker_71 0:2e9a86765692 114 }
faker_71 0:2e9a86765692 115 // Static WEB page
faker_71 0:2e9a86765692 116 void sendpage()
faker_71 0:2e9a86765692 117 {
faker_71 0:2e9a86765692 118 // WEB page data
faker_71 0:2e9a86765692 119
faker_71 0:2e9a86765692 120 strcpy(webbuff, "<!DOCTYPE html>");
faker_71 0:2e9a86765692 121 strcat(webbuff, "<html><head><title>RobotCar</title><meta name='viewport' content='width=device-width'/>");
faker_71 0:2e9a86765692 122 strcat(webbuff, "</head><body><center><p><strong>Robot Car Remot Controller");
faker_71 0:2e9a86765692 123 strcat(webbuff, "</strong></p><td style='vertical-align:top;'><strong>Battery level ");
faker_71 0:2e9a86765692 124 strcat(webbuff, "<input type=\"text\" id=\"leftms\" size=4 value=250>%</strong>");
faker_71 0:2e9a86765692 125 strcat(webbuff, "</td></p><tr ><strong>Now speed : </strong></tr><nobr id=\"speprint\">ready");
faker_71 0:2e9a86765692 126 strcat(webbuff, "</nobr><br><tr ><strong>Now action : </strong></tr>");
faker_71 0:2e9a86765692 127 strcat(webbuff, "<nobr id=\"funprint\">ready</nobr><table><tr><td></td><td>");
faker_71 0:2e9a86765692 128
faker_71 0:2e9a86765692 129 strcat(webbuff, "<button id='gobtn' type='button' style='width:100px;height:60px' value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
faker_71 0:2e9a86765692 130 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
faker_71 0:2e9a86765692 131 strcat(webbuff, "<button id='leftbtn' type='button' style='width:100px;height:60px' value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
faker_71 0:2e9a86765692 132 strcat(webbuff, "</button></td><td>");
faker_71 0:2e9a86765692 133
faker_71 0:2e9a86765692 134 strcat(webbuff, "<button id='stopbtn' type='button' style='width:100px;height:60px' value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
faker_71 0:2e9a86765692 135 strcat(webbuff, "</button></td><td>");
faker_71 0:2e9a86765692 136 strcat(webbuff, "<button id='rightbtn' type='button' style='width:100px;height:60px' value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
faker_71 0:2e9a86765692 137 strcat(webbuff, "</button></td></tr><td></td><td>");
faker_71 0:2e9a86765692 138 strcat(webbuff, "<button id='backbtn' type='button' style='width:100px;height:60px' value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
faker_71 0:2e9a86765692 139 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
faker_71 0:2e9a86765692 140
faker_71 0:2e9a86765692 141
faker_71 0:2e9a86765692 142
faker_71 0:2e9a86765692 143 //begin
faker_71 0:2e9a86765692 144 strcat(webbuff, "<strong>Addition functions:</strong><table>");
faker_71 0:2e9a86765692 145 strcat(webbuff, "<td>");//ok
faker_71 0:2e9a86765692 146 strcat(webbuff, "<button id='avoidbtn' type='button' style='width:100px;height:60px' value=\"AVOIDANCE\" onClick='send_mes_fun(this.id,this.value)' >");
faker_71 0:2e9a86765692 147 strcat(webbuff, "AVOIDANCE</button></td><td>");
faker_71 0:2e9a86765692 148 strcat(webbuff, "<button id='tracebtn' type='button' style='width:100px;height:60px' value=\"LINETRACE\" onClick='send_mes_fun(this.id,this.value)' >LINE TRACE");
faker_71 0:2e9a86765692 149 strcat(webbuff, "</button></td></table><table><td>");
faker_71 0:2e9a86765692 150 strcat(webbuff, "</td><strong>Speed level:</strong>");
faker_71 0:2e9a86765692 151 strcat(webbuff, "</table><table><td>");
faker_71 0:2e9a86765692 152 strcat(webbuff, "<button id='sp1btn' type='button' style='width:100px;height:60px' value=\"SLOW\" onClick='send_mes_spe(this.id,this.value)' >SLOW");
faker_71 0:2e9a86765692 153 strcat(webbuff, "</button></td><td>");
faker_71 0:2e9a86765692 154 strcat(webbuff, "<button id='sp2btn' type='button' style='width:100px;height:60px' value=\"FAST\" onClick='send_mes_spe(this.id,this.value)' >FAST");
faker_71 0:2e9a86765692 155 strcat(webbuff, "</button>");
faker_71 0:2e9a86765692 156 strcat(webbuff, "</td>");
faker_71 0:2e9a86765692 157 strcat(webbuff, "<td>");
faker_71 0:2e9a86765692 158 strcat(webbuff, "<button id='sp3btn' type='button' style='width:100px;height:60px' value=\"VERYFAST\" onClick='send_mes_spe(this.id,this.value)' >FLY");
faker_71 0:2e9a86765692 159 strcat(webbuff, "</button>");
faker_71 0:2e9a86765692 160 strcat(webbuff, "</td></table>");
faker_71 0:2e9a86765692 161 strcat(webbuff, "<table>");
faker_71 0:2e9a86765692 162 strcat(webbuff, "<td>");
faker_71 0:2e9a86765692 163 strcat(webbuff, "</td>");
faker_71 0:2e9a86765692 164 strcat(webbuff, "</table>"); //end
faker_71 0:2e9a86765692 165
faker_71 0:2e9a86765692 166
faker_71 0:2e9a86765692 167
faker_71 0:2e9a86765692 168 strcat(webbuff, "</center>");
faker_71 0:2e9a86765692 169 strcat(webbuff, "</body>");
faker_71 0:2e9a86765692 170 strcat(webbuff, "</html>");
faker_71 0:2e9a86765692 171 strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
faker_71 0:2e9a86765692 172 strcat(webbuff, "function send_mes(btnmes,btnval){");
faker_71 4:a365d6acec8f 173
faker_71 4:a365d6acec8f 174 strcat(webbuff, "try {xhr.abort();console.log(1);}");
faker_71 4:a365d6acec8f 175 strcat(webbuff, "catch(err) {console.log(0);}");
faker_71 4:a365d6acec8f 176
faker_71 0:2e9a86765692 177 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
faker_71 0:2e9a86765692 178 strcat(webbuff, "htmlacs(url);");
faker_71 0:2e9a86765692 179 strcat(webbuff, "console.log(url);");
faker_71 0:2e9a86765692 180 strcat(webbuff, "}");
faker_71 2:1bf376317a43 181
faker_71 0:2e9a86765692 182 strcat(webbuff, "function htmlacs(url) {");
faker_71 0:2e9a86765692 183 strcat(webbuff, "var xhr = new XMLHttpRequest();");
faker_71 2:1bf376317a43 184 strcat(webbuff, "xhr.open(\"GET\", url ,true);");
faker_71 2:1bf376317a43 185 strcat(webbuff, "xhr.send();");
faker_71 0:2e9a86765692 186 strcat(webbuff, "}");
faker_71 2:1bf376317a43 187
faker_71 4:a365d6acec8f 188 //0902 beging
faker_71 4:a365d6acec8f 189 /*
faker_71 2:1bf376317a43 190 //0824 battery update auto
faker_71 2:1bf376317a43 191 strcat(webbuff, "function battery_update() {");
faker_71 2:1bf376317a43 192 strcat(webbuff, "var url1 = \"http://\" + window.location.hostname+ \"/cargo?a=responseBattery\";");
faker_71 2:1bf376317a43 193 strcat(webbuff, "var xhr1 = new XMLHttpRequest();");
faker_71 2:1bf376317a43 194 strcat(webbuff, "xhr1.open(\"GET\", url1);");
faker_71 2:1bf376317a43 195 //0820
faker_71 2:1bf376317a43 196 strcat(webbuff, "xhr1.onreadystatechange = function(){");
faker_71 2:1bf376317a43 197 //strcat(webbuff, "console.log(\"onready function is being reload!\");");
faker_71 2:1bf376317a43 198 strcat(webbuff, "if(this.readyState == 4 && this.status == 200){");
faker_71 2:1bf376317a43 199 //strcat(webbuff, "console.log(\"state is being reload!\");");
faker_71 2:1bf376317a43 200 strcat(webbuff, "var res1 = xhr1.responseText;");
faker_71 2:1bf376317a43 201 strcat(webbuff, "document.getElementById('leftms').value=res1;}};");
faker_71 2:1bf376317a43 202 //strcat(webbuff, "console.log(res);}};");
faker_71 2:1bf376317a43 203 //0820
faker_71 2:1bf376317a43 204
faker_71 2:1bf376317a43 205 strcat(webbuff, "xhr1.send();");
faker_71 2:1bf376317a43 206 strcat(webbuff, "}");
faker_71 2:1bf376317a43 207 strcat(webbuff, "setInterval(battery_update,10000);");
faker_71 4:a365d6acec8f 208 */
faker_71 4:a365d6acec8f 209 //0902 end
faker_71 2:1bf376317a43 210 //0824 battery update auto
faker_71 2:1bf376317a43 211 /*
faker_71 0:2e9a86765692 212 strcat(webbuff, "function send_mes_spe(btnmes,btnval){");
faker_71 0:2e9a86765692 213 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
faker_71 0:2e9a86765692 214 strcat(webbuff, "htmlacs(url);");
faker_71 0:2e9a86765692 215 strcat(webbuff, "if(document.getElementById(\"speprint\")){");
faker_71 0:2e9a86765692 216 strcat(webbuff, "document.getElementById(\"speprint\").innerHTML=btnval;");
faker_71 0:2e9a86765692 217 strcat(webbuff, "}");
faker_71 0:2e9a86765692 218 strcat(webbuff, "}");
faker_71 0:2e9a86765692 219 strcat(webbuff, "function send_mes_fun(btnmes,btnval){");
faker_71 0:2e9a86765692 220 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
faker_71 0:2e9a86765692 221 strcat(webbuff, "htmlacs(url);");
faker_71 0:2e9a86765692 222 strcat(webbuff, "if(document.getElementById(\"funprint\")){");
faker_71 0:2e9a86765692 223 strcat(webbuff, "document.getElementById(\"funprint\").innerHTML=btnval;");
faker_71 0:2e9a86765692 224 strcat(webbuff, "}");
faker_71 0:2e9a86765692 225 strcat(webbuff, "}");
faker_71 2:1bf376317a43 226 */
faker_71 0:2e9a86765692 227 strcat(webbuff, "</script>");
faker_71 0:2e9a86765692 228 // end of WEB page data
faker_71 0:2e9a86765692 229 bufl = strlen(webbuff); // get total page buffer length
faker_71 0:2e9a86765692 230 //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
faker_71 0:2e9a86765692 231
faker_71 0:2e9a86765692 232 sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
faker_71 0:2e9a86765692 233 timeout=500;
faker_71 0:2e9a86765692 234 getcount=40;
faker_71 0:2e9a86765692 235 SendCMD();
faker_71 0:2e9a86765692 236 getreply();
faker_71 0:2e9a86765692 237 pc.printf(replybuff);
faker_71 0:2e9a86765692 238 pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl));
faker_71 0:2e9a86765692 239
faker_71 0:2e9a86765692 240 pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
faker_71 0:2e9a86765692 241
faker_71 0:2e9a86765692 242 //pastthrough mode
faker_71 0:2e9a86765692 243 SendWEB(); // send web page
faker_71 0:2e9a86765692 244 pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n");
faker_71 0:2e9a86765692 245
faker_71 0:2e9a86765692 246 memset(webbuff, '\0', sizeof(webbuff));
faker_71 0:2e9a86765692 247 sendcheck();
faker_71 0:2e9a86765692 248 }
faker_71 0:2e9a86765692 249
faker_71 2:1bf376317a43 250
faker_71 2:1bf376317a43 251 void sendpage2()
faker_71 0:2e9a86765692 252 {
faker_71 2:1bf376317a43 253 // WEB page data
faker_71 4:a365d6acec8f 254 pred=!pred;
faker_71 4:a365d6acec8f 255 /*
faker_71 2:1bf376317a43 256 //strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
faker_71 2:1bf376317a43 257 //strcat(webbuff, "document.getElementById('leftms').value=\"100\";");
faker_71 2:1bf376317a43 258 count_test++;
faker_71 2:1bf376317a43 259 sprintf(battery_ch,"%d",count_test);
faker_71 2:1bf376317a43 260 strcpy(webbuff, battery_ch);
faker_71 2:1bf376317a43 261
faker_71 2:1bf376317a43 262 //strcpy(webbuff, "document.getElementById('leftms').value=\"100\";");
faker_71 2:1bf376317a43 263
faker_71 2:1bf376317a43 264 //strcat(webbuff, "</script>");
faker_71 2:1bf376317a43 265 // end of WEB page data
faker_71 2:1bf376317a43 266 bufl = strlen(webbuff); // get total page buffer length
faker_71 2:1bf376317a43 267 //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
faker_71 2:1bf376317a43 268
faker_71 2:1bf376317a43 269 sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
faker_71 2:1bf376317a43 270 timeout=500;
faker_71 2:1bf376317a43 271 getcount=40;
faker_71 2:1bf376317a43 272 SendCMD();
faker_71 2:1bf376317a43 273 getreply();
faker_71 2:1bf376317a43 274 pc.printf(replybuff);
faker_71 2:1bf376317a43 275 pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl));
faker_71 2:1bf376317a43 276
faker_71 2:1bf376317a43 277 pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
faker_71 2:1bf376317a43 278
faker_71 2:1bf376317a43 279 //pastthrough mode
faker_71 2:1bf376317a43 280 SendWEB(); // send web page
faker_71 2:1bf376317a43 281 pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n");
faker_71 2:1bf376317a43 282
faker_71 2:1bf376317a43 283 memset(webbuff, '\0', sizeof(webbuff));
faker_71 2:1bf376317a43 284 sendcheck();
faker_71 4:a365d6acec8f 285
faker_71 4:a365d6acec8f 286 */
faker_71 0:2e9a86765692 287 }
faker_71 0:2e9a86765692 288
faker_71 0:2e9a86765692 289
faker_71 0:2e9a86765692 290 // Reads and processes GET and POST web data
faker_71 0:2e9a86765692 291 void ReadWebData()
faker_71 0:2e9a86765692 292 {
faker_71 4:a365d6acec8f 293 pc.printf("+++++++++++++++++Read Web Data begin+++++++++++++++++++++\r\n");
faker_71 4:a365d6acec8f 294
faker_71 4:a365d6acec8f 295 //0902 is this wait neccessary
faker_71 4:a365d6acec8f 296 // wait_ms(200);
faker_71 4:a365d6acec8f 297
faker_71 0:2e9a86765692 298 esp.attach(NULL);
faker_71 0:2e9a86765692 299 count_wifi=0;
faker_71 0:2e9a86765692 300 DataRX=0;
faker_71 0:2e9a86765692 301 weberror=0;
faker_71 0:2e9a86765692 302 memset(webdata, '\0', sizeof(webdata));
faker_71 0:2e9a86765692 303 int x = strcspn (webbuff,"+");
faker_71 0:2e9a86765692 304 if(x) {
faker_71 0:2e9a86765692 305 strcpy(webdata, webbuff + x);
faker_71 0:2e9a86765692 306 weberror=0;
faker_71 0:2e9a86765692 307 int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
faker_71 0:2e9a86765692 308 //int i=0;
faker_71 4:a365d6acec8f 309 pc.printf("\r\n+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n");
faker_71 0:2e9a86765692 310 pc.printf("%s",webdata);
faker_71 4:a365d6acec8f 311 pc.printf("\r\n+++++++++++++++++succed rec end+++++++++++++++++++++\r\n");
faker_71 4:a365d6acec8f 312 // click or login
faker_71 4:a365d6acec8f 313 if( strstr(webdata, "cargo") != NULL ) {
faker_71 4:a365d6acec8f 314 click_log=true;
faker_71 4:a365d6acec8f 315 pc.printf("\r\n++this is click++\r\n");
faker_71 4:a365d6acec8f 316
faker_71 4:a365d6acec8f 317 }
faker_71 2:1bf376317a43 318 if( strstr(webdata, "responseBattery") != NULL ) {
faker_71 0:2e9a86765692 319 click_flag = 0;
faker_71 0:2e9a86765692 320 led4=!led4;
faker_71 2:1bf376317a43 321 pc.printf("\r\n++++++++++++++mode = LEFT++++++++++++++\r\n");
faker_71 2:1bf376317a43 322 sendpage2();
faker_71 2:1bf376317a43 323 /*
faker_71 2:1bf376317a43 324 strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
faker_71 2:1bf376317a43 325 strcat(webbuff, "document.getElementById('leftms').value=\"96\";");
faker_71 2:1bf376317a43 326 strcat(webbuff, "let request = new XMLHttpRequest();");
faker_71 2:1bf376317a43 327 strcat(webbuff, "request.open('GET', url);request.responseType = 'text';");
faker_71 2:1bf376317a43 328 strcat(webbuff, "request.onload = function() { poemDisplay.textContent = request.response;};");
faker_71 2:1bf376317a43 329
faker_71 2:1bf376317a43 330 strcat(webbuff, "request.send();");
faker_71 2:1bf376317a43 331 strcat(webbuff, "</script>");
faker_71 2:1bf376317a43 332 bufl = strlen(webbuff); // get total page buffer length
faker_71 2:1bf376317a43 333 sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
faker_71 2:1bf376317a43 334 timeout=500;
faker_71 2:1bf376317a43 335 getcount=40;
faker_71 2:1bf376317a43 336 SendCMD();
faker_71 2:1bf376317a43 337 getreply();
faker_71 2:1bf376317a43 338 SendWEB();
faker_71 2:1bf376317a43 339 memset(webbuff, '\0', sizeof(webbuff));
faker_71 2:1bf376317a43 340 sendcheck();
faker_71 2:1bf376317a43 341 pc.printf("\r\n++++++++++++++mode = LEFT++++++++++++++\r\n");
faker_71 2:1bf376317a43 342 */
faker_71 2:1bf376317a43 343
faker_71 2:1bf376317a43 344 /* mode = LEFT; // 左折モード
faker_71 0:2e9a86765692 345 run = LEFT; // 左折
faker_71 2:1bf376317a43 346 display(); */
faker_71 2:1bf376317a43 347
faker_71 0:2e9a86765692 348 }
faker_71 3:4c5c556c97a3 349 if( strstr(webdata, "LEFT") != NULL ) {
faker_71 4:a365d6acec8f 350 sendpage2();
faker_71 3:4c5c556c97a3 351 led4=!led4;
faker_71 4:a365d6acec8f 352 // pc.printf("mode = RIGHT\r\n");
faker_71 3:4c5c556c97a3 353
faker_71 3:4c5c556c97a3 354 }
faker_71 4:a365d6acec8f 355
faker_71 0:2e9a86765692 356 if( strstr(webdata, "RIGHT") != NULL ) {
faker_71 4:a365d6acec8f 357 sendpage2();
faker_71 0:2e9a86765692 358 led4=!led4;
faker_71 4:a365d6acec8f 359
faker_71 0:2e9a86765692 360 }
faker_71 0:2e9a86765692 361
faker_71 4:a365d6acec8f 362 /*
faker_71 0:2e9a86765692 363 if( strstr(webdata, "GO") != NULL ) {
faker_71 0:2e9a86765692 364 led4=!led4;
faker_71 0:2e9a86765692 365 pc.printf("mode = ADVANCE\r\n");
faker_71 0:2e9a86765692 366 mode = ADVANCE; // 前進モード
faker_71 0:2e9a86765692 367 run = ADVANCE; // 前進
faker_71 0:2e9a86765692 368 display(); // ディスプレイ表示
faker_71 0:2e9a86765692 369 }
faker_71 0:2e9a86765692 370
faker_71 0:2e9a86765692 371 if( strstr(webdata, "BACK") != NULL ) {
faker_71 0:2e9a86765692 372 led4=!led4;
faker_71 0:2e9a86765692 373 pc.printf("mode = BACK\r\n");
faker_71 0:2e9a86765692 374 mode = BACK; // 後退モード
faker_71 0:2e9a86765692 375 run = BACK; // 後退
faker_71 0:2e9a86765692 376 display(); // ディスプレイ表示
faker_71 0:2e9a86765692 377 }
faker_71 0:2e9a86765692 378
faker_71 0:2e9a86765692 379 if( strstr(webdata, "STOP") != NULL ) {
faker_71 0:2e9a86765692 380 led4=!led4;
faker_71 0:2e9a86765692 381 pc.printf("mode = STOP\r\n");
faker_71 0:2e9a86765692 382 mode = STOP; // 停止モード
faker_71 0:2e9a86765692 383 run = STOP; // 停止
faker_71 0:2e9a86765692 384 display(); // ディスプレイ表示
faker_71 0:2e9a86765692 385 }
faker_71 0:2e9a86765692 386
faker_71 0:2e9a86765692 387 if( strstr(webdata, "AVOIDANCE") != NULL ) {
faker_71 0:2e9a86765692 388 led4=!led4;
faker_71 0:2e9a86765692 389 pc.printf("mode = AVOIDANCE\r\n");
faker_71 0:2e9a86765692 390 mode=AVOIDANCE; // 障害物回避モード
faker_71 0:2e9a86765692 391 run = ADVANCE; // 前進
faker_71 0:2e9a86765692 392 display();
faker_71 0:2e9a86765692 393 }
faker_71 0:2e9a86765692 394
faker_71 0:2e9a86765692 395 if( strstr(webdata, "LINETRACE") != NULL ) {
faker_71 0:2e9a86765692 396 led4=!led4;
faker_71 0:2e9a86765692 397 pc.printf("mode = LINE_TRACE\r\n");
faker_71 0:2e9a86765692 398 mode=LINE_TRACE; // ライントレースモード
faker_71 0:2e9a86765692 399 display();
faker_71 0:2e9a86765692 400 }
faker_71 0:2e9a86765692 401
faker_71 0:2e9a86765692 402 if( strstr(webdata, "SLOW") != NULL ) {
faker_71 0:2e9a86765692 403 led4=!led4;
faker_71 0:2e9a86765692 404 pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n");
faker_71 0:2e9a86765692 405 }
faker_71 0:2e9a86765692 406
faker_71 0:2e9a86765692 407 if( strstr(webdata, "FAST") != NULL ) {
faker_71 0:2e9a86765692 408 led4=!led4;
faker_71 0:2e9a86765692 409 pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n");
faker_71 0:2e9a86765692 410 }
faker_71 0:2e9a86765692 411
faker_71 0:2e9a86765692 412 if( strstr(webdata, "VERYFAST") != NULL ) {
faker_71 0:2e9a86765692 413 led4=!led4;
faker_71 0:2e9a86765692 414 pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n");
faker_71 0:2e9a86765692 415 }*/
faker_71 0:2e9a86765692 416 sprintf(channel, "%d",linkID);
faker_71 0:2e9a86765692 417 if (strstr(webdata, "GET") != NULL) {
faker_71 0:2e9a86765692 418 servreq=1;
faker_71 0:2e9a86765692 419 pc.printf("\r\n+++++++++++++++++GET+++++++++++++++++++++\r\n");
faker_71 0:2e9a86765692 420
faker_71 0:2e9a86765692 421 }
faker_71 0:2e9a86765692 422 if (strstr(webdata, "POST") != NULL) {
faker_71 0:2e9a86765692 423 servreq=1;
faker_71 0:2e9a86765692 424 pc.printf("\r\n+++++++++++++++++POST+++++++++++++++++++++\r\n");
faker_71 0:2e9a86765692 425 }
faker_71 0:2e9a86765692 426 webcounter++;
faker_71 0:2e9a86765692 427 sprintf(webcount, "%d",webcounter);
faker_71 0:2e9a86765692 428 } else {
faker_71 0:2e9a86765692 429 pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n");
faker_71 0:2e9a86765692 430 memset(webbuff, '\0', sizeof(webbuff));
faker_71 0:2e9a86765692 431 esp.attach(&callback);
faker_71 0:2e9a86765692 432 weberror=1;
faker_71 0:2e9a86765692 433 }
faker_71 4:a365d6acec8f 434
faker_71 4:a365d6acec8f 435 pc.printf("+++++++++++++++++Read Web Data end+++++++++++++++++++++\r\n");
faker_71 0:2e9a86765692 436 }
faker_71 2:1bf376317a43 437 // Large WEB buffer data send
faker_71 2:1bf376317a43 438 void SendWEB()
faker_71 2:1bf376317a43 439 {
faker_71 2:1bf376317a43 440 int i=0;
faker_71 2:1bf376317a43 441 if(esp.writeable()) {
faker_71 2:1bf376317a43 442 while(webbuff[i]!='\0') {
faker_71 2:1bf376317a43 443 esp.putc(webbuff[i]);
faker_71 2:1bf376317a43 444
faker_71 2:1bf376317a43 445 //****
faker_71 2:1bf376317a43 446 //output at command when 2000
faker_71 2:1bf376317a43 447 if(((i%2047)==0) && (i>0)){
faker_71 2:1bf376317a43 448 //wait_ms(10);
faker_71 2:1bf376317a43 449 sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048)); // send IPD link channel and buffer character length.
faker_71 2:1bf376317a43 450 pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048));
faker_71 2:1bf376317a43 451 timeout=600;
faker_71 2:1bf376317a43 452 getcount=50;
faker_71 2:1bf376317a43 453 SendCMD();
faker_71 2:1bf376317a43 454 getreply();
faker_71 2:1bf376317a43 455 pc.printf(replybuff);
faker_71 2:1bf376317a43 456 pc.printf("\r\n+++++++++++++++++++\r\n");
faker_71 2:1bf376317a43 457 }
faker_71 2:1bf376317a43 458 //****
faker_71 2:1bf376317a43 459 i++;
faker_71 2:1bf376317a43 460 pc.printf("%c",webbuff[i]);
faker_71 2:1bf376317a43 461 }
faker_71 2:1bf376317a43 462 }
faker_71 2:1bf376317a43 463 pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i);
faker_71 2:1bf376317a43 464 }
faker_71 2:1bf376317a43 465
faker_71 2:1bf376317a43 466
faker_71 2:1bf376317a43 467
faker_71 2:1bf376317a43 468 void sendcheck()
faker_71 2:1bf376317a43 469 {
faker_71 2:1bf376317a43 470 weberror=1;
faker_71 2:1bf376317a43 471 timeout=500;
faker_71 2:1bf376317a43 472 getcount=24;
faker_71 2:1bf376317a43 473 t2.reset();
faker_71 2:1bf376317a43 474 t2.start();
faker_71 2:1bf376317a43 475
faker_71 2:1bf376317a43 476 /*
faker_71 2:1bf376317a43 477 while(weberror==1 && t2.read() <5) {
faker_71 2:1bf376317a43 478 getreply();
faker_71 2:1bf376317a43 479 if (strstr(replybuff, "SEND OK") != NULL) {
faker_71 2:1bf376317a43 480 weberror=0; // wait for valid SEND OK
faker_71 2:1bf376317a43 481 }
faker_71 2:1bf376317a43 482 }
faker_71 2:1bf376317a43 483 */
faker_71 2:1bf376317a43 484 if(weberror==1) { // restart connection
faker_71 2:1bf376317a43 485 strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
faker_71 2:1bf376317a43 486 timeout=500;
faker_71 2:1bf376317a43 487 getcount=10;
faker_71 2:1bf376317a43 488 SendCMD();
faker_71 2:1bf376317a43 489 getreply();
faker_71 2:1bf376317a43 490 pc.printf(replybuff);
faker_71 2:1bf376317a43 491 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
faker_71 2:1bf376317a43 492 timeout=500;
faker_71 2:1bf376317a43 493 getcount=10;
faker_71 2:1bf376317a43 494 SendCMD();
faker_71 2:1bf376317a43 495 getreply();
faker_71 2:1bf376317a43 496 pc.printf(replybuff);
faker_71 2:1bf376317a43 497 } else {
faker_71 2:1bf376317a43 498 sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
faker_71 2:1bf376317a43 499 SendCMD();
faker_71 2:1bf376317a43 500 getreply();
faker_71 2:1bf376317a43 501 pc.printf(replybuff);
faker_71 2:1bf376317a43 502 }
faker_71 2:1bf376317a43 503 t2.reset();
faker_71 2:1bf376317a43 504 }
faker_71 2:1bf376317a43 505
faker_71 0:2e9a86765692 506 // Starts and restarts webserver if errors detected.
faker_71 0:2e9a86765692 507 void startserver()
faker_71 0:2e9a86765692 508 {
faker_71 0:2e9a86765692 509 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
faker_71 0:2e9a86765692 510 strcpy(cmdbuff,"AT+RST\r\n");
faker_71 0:2e9a86765692 511 timeout=8000;
faker_71 0:2e9a86765692 512 getcount=1000;
faker_71 0:2e9a86765692 513 SendCMD();
faker_71 0:2e9a86765692 514 getreply();
faker_71 0:2e9a86765692 515 pc.printf(replybuff);
faker_71 0:2e9a86765692 516 pc.printf("%d",count_wifi);
faker_71 0:2e9a86765692 517 if (strstr(replybuff, "OK") != NULL) {
faker_71 0:2e9a86765692 518 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
faker_71 0:2e9a86765692 519 strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections.
faker_71 0:2e9a86765692 520 timeout=500;
faker_71 0:2e9a86765692 521 getcount=20;
faker_71 0:2e9a86765692 522 SendCMD();
faker_71 0:2e9a86765692 523 getreply();
faker_71 0:2e9a86765692 524 pc.printf(replybuff);
faker_71 0:2e9a86765692 525 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
faker_71 0:2e9a86765692 526 timeout=500;
faker_71 0:2e9a86765692 527 getcount=20;
faker_71 0:2e9a86765692 528 SendCMD();
faker_71 0:2e9a86765692 529 getreply();
faker_71 0:2e9a86765692 530 pc.printf(replybuff);
faker_71 0:2e9a86765692 531 wait(1);
faker_71 0:2e9a86765692 532 sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
faker_71 0:2e9a86765692 533 timeout=500;
faker_71 0:2e9a86765692 534 getcount=50;
faker_71 0:2e9a86765692 535 SendCMD();
faker_71 0:2e9a86765692 536 getreply();
faker_71 0:2e9a86765692 537 pc.printf(replybuff);
faker_71 1:a45cf46c1adc 538 //wait(5);
faker_71 1:a45cf46c1adc 539 wait(1);
faker_71 0:2e9a86765692 540 pc.printf("\n Getting Server IP \r\n");
faker_71 0:2e9a86765692 541 strcpy(cmdbuff, "AT+CIFSR\r\n");
faker_71 0:2e9a86765692 542 timeout=2500;
faker_71 0:2e9a86765692 543 getcount=200;
faker_71 0:2e9a86765692 544 while(weberror==0) {
faker_71 0:2e9a86765692 545 SendCMD();
faker_71 0:2e9a86765692 546 getreply();
faker_71 0:2e9a86765692 547 if (strstr(replybuff, "0.0.0.0") == NULL) {
faker_71 0:2e9a86765692 548 weberror=1; // wait for valid IP
faker_71 0:2e9a86765692 549 }
faker_71 0:2e9a86765692 550 }
faker_71 0:2e9a86765692 551 pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
faker_71 0:2e9a86765692 552 pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
faker_71 0:2e9a86765692 553 replybuff[strlen(replybuff)-1] = '\0';
faker_71 0:2e9a86765692 554 //char* IP = replybuff + 5;
faker_71 0:2e9a86765692 555 sprintf(webdata,"%s", replybuff);
faker_71 0:2e9a86765692 556 pc.printf(webdata);
faker_71 0:2e9a86765692 557 led2=1;
faker_71 0:2e9a86765692 558 bufflen=200;
faker_71 0:2e9a86765692 559 //bufflen=100;
faker_71 0:2e9a86765692 560 count_wifi=0;
faker_71 0:2e9a86765692 561 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
faker_71 0:2e9a86765692 562 esp.attach(&callback);
faker_71 0:2e9a86765692 563 } else {
faker_71 0:2e9a86765692 564 pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
faker_71 0:2e9a86765692 565 led1=1;
faker_71 0:2e9a86765692 566 led2=1;
faker_71 0:2e9a86765692 567 led3=1;
faker_71 0:2e9a86765692 568 led4=1;
faker_71 0:2e9a86765692 569 while(1) {
faker_71 0:2e9a86765692 570 led1=!led1;
faker_71 0:2e9a86765692 571 led2=!led2;
faker_71 0:2e9a86765692 572 led3=!led3;
faker_71 0:2e9a86765692 573 led4=!led4;
faker_71 0:2e9a86765692 574 wait(1);
faker_71 0:2e9a86765692 575 }
faker_71 0:2e9a86765692 576 }
faker_71 0:2e9a86765692 577 t2.reset();
faker_71 0:2e9a86765692 578 t2.start();
faker_71 0:2e9a86765692 579 }
faker_71 0:2e9a86765692 580 // ESP Command data send
faker_71 0:2e9a86765692 581 void SendCMD()
faker_71 0:2e9a86765692 582 {
faker_71 0:2e9a86765692 583 esp.printf("%s", cmdbuff);
faker_71 0:2e9a86765692 584 }
faker_71 0:2e9a86765692 585 // Get Command and ESP status replies
faker_71 0:2e9a86765692 586 void getreply()
faker_71 0:2e9a86765692 587 {
faker_71 0:2e9a86765692 588 memset(replybuff, '\0', sizeof(replybuff));
faker_71 0:2e9a86765692 589 t1.reset();
faker_71 0:2e9a86765692 590 t1.start();
faker_71 0:2e9a86765692 591 replycount=0;
faker_71 0:2e9a86765692 592 while(t1.read_ms()< timeout && replycount < getcount) {
faker_71 0:2e9a86765692 593 if(esp.readable()) {
faker_71 0:2e9a86765692 594 replybuff[replycount] = esp.getc();
faker_71 0:2e9a86765692 595 replycount++;
faker_71 0:2e9a86765692 596 }
faker_71 0:2e9a86765692 597 }
faker_71 0:2e9a86765692 598 t1.stop();
faker_71 0:2e9a86765692 599 }