real time wifi
Dependencies: RemoteIR TextLCD
main.cpp@5:4026062a6fdf, 2020-09-03 (annotated)
- Committer:
- faker_71
- Date:
- Thu Sep 03 04:43:03 2020 +0000
- Revision:
- 5:4026062a6fdf
- Parent:
- 4:a365d6acec8f
0903 wifi
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:4026062a6fdf | 82 | |
faker_71 | 0:2e9a86765692 | 83 | if(DataRX==1) { |
faker_71 | 0:2e9a86765692 | 84 | pc.printf("\f\n\r------------ main while > if --------------\n\r"); |
faker_71 | 4:a365d6acec8f | 85 | // click_flag = 1; |
faker_71 | 4:a365d6acec8f | 86 | click_log=false; |
faker_71 | 0:2e9a86765692 | 87 | ReadWebData(); |
faker_71 | 4:a365d6acec8f | 88 | // pc.printf("\f\n\r------------ click_flag=%d --------------\n\r",click_flag); |
faker_71 | 0:2e9a86765692 | 89 | //if ((servreq == 1 && weberror == 0) && click_flag == 1) { |
faker_71 | 4:a365d6acec8f | 90 | if (servreq == 1 && weberror == 0 && click_log == false) { |
faker_71 | 0:2e9a86765692 | 91 | pc.printf("\f\n\r------------ befor send page --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 92 | sendpage(); |
faker_71 | 0:2e9a86765692 | 93 | } |
faker_71 | 4:a365d6acec8f | 94 | // pc.printf("\f\n\r------------ send_check begin --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 95 | |
faker_71 | 0:2e9a86765692 | 96 | //sendcheck(); |
faker_71 | 4:a365d6acec8f | 97 | pc.printf("\f\n\r------------ befor attach callback--------------\n\r"); |
faker_71 | 4:a365d6acec8f | 98 | // this is neccessary 0902 |
faker_71 | 4:a365d6acec8f | 99 | memset(webbuff, '\0', sizeof(webbuff)); |
faker_71 | 4:a365d6acec8f | 100 | |
faker_71 | 0:2e9a86765692 | 101 | esp.attach(&callback); |
faker_71 | 0:2e9a86765692 | 102 | 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 | 103 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
faker_71 | 0:2e9a86765692 | 104 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
faker_71 | 0:2e9a86765692 | 105 | pc.printf(" -------------------------------------\n\n"); |
faker_71 | 0:2e9a86765692 | 106 | servreq=0; |
faker_71 | 0:2e9a86765692 | 107 | } |
faker_71 | 0:2e9a86765692 | 108 | } |
faker_71 | 0:2e9a86765692 | 109 | } |
faker_71 | 0:2e9a86765692 | 110 | int main() |
faker_71 | 0:2e9a86765692 | 111 | { |
faker_71 | 0:2e9a86765692 | 112 | while(1){ |
faker_71 | 0:2e9a86765692 | 113 | Thread::wait(100); |
faker_71 | 0:2e9a86765692 | 114 | } |
faker_71 | 0:2e9a86765692 | 115 | } |
faker_71 | 0:2e9a86765692 | 116 | // Static WEB page |
faker_71 | 0:2e9a86765692 | 117 | void sendpage() |
faker_71 | 0:2e9a86765692 | 118 | { |
faker_71 | 0:2e9a86765692 | 119 | // WEB page data |
faker_71 | 0:2e9a86765692 | 120 | |
faker_71 | 0:2e9a86765692 | 121 | strcpy(webbuff, "<!DOCTYPE html>"); |
faker_71 | 0:2e9a86765692 | 122 | strcat(webbuff, "<html><head><title>RobotCar</title><meta name='viewport' content='width=device-width'/>"); |
faker_71 | 0:2e9a86765692 | 123 | strcat(webbuff, "</head><body><center><p><strong>Robot Car Remot Controller"); |
faker_71 | 0:2e9a86765692 | 124 | strcat(webbuff, "</strong></p><td style='vertical-align:top;'><strong>Battery level "); |
faker_71 | 0:2e9a86765692 | 125 | strcat(webbuff, "<input type=\"text\" id=\"leftms\" size=4 value=250>%</strong>"); |
faker_71 | 0:2e9a86765692 | 126 | strcat(webbuff, "</td></p><tr ><strong>Now speed : </strong></tr><nobr id=\"speprint\">ready"); |
faker_71 | 0:2e9a86765692 | 127 | strcat(webbuff, "</nobr><br><tr ><strong>Now action : </strong></tr>"); |
faker_71 | 0:2e9a86765692 | 128 | strcat(webbuff, "<nobr id=\"funprint\">ready</nobr><table><tr><td></td><td>"); |
faker_71 | 0:2e9a86765692 | 129 | |
faker_71 | 0:2e9a86765692 | 130 | 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 | 131 | strcat(webbuff, "</button></td><td></td></tr><tr><td>"); |
faker_71 | 0:2e9a86765692 | 132 | 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 | 133 | strcat(webbuff, "</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 134 | |
faker_71 | 0:2e9a86765692 | 135 | 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 | 136 | strcat(webbuff, "</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 137 | 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 | 138 | strcat(webbuff, "</button></td></tr><td></td><td>"); |
faker_71 | 0:2e9a86765692 | 139 | 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 | 140 | strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>"); |
faker_71 | 0:2e9a86765692 | 141 | |
faker_71 | 0:2e9a86765692 | 142 | |
faker_71 | 0:2e9a86765692 | 143 | |
faker_71 | 0:2e9a86765692 | 144 | //begin |
faker_71 | 0:2e9a86765692 | 145 | strcat(webbuff, "<strong>Addition functions:</strong><table>"); |
faker_71 | 0:2e9a86765692 | 146 | strcat(webbuff, "<td>");//ok |
faker_71 | 0:2e9a86765692 | 147 | 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 | 148 | strcat(webbuff, "AVOIDANCE</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 149 | 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 | 150 | strcat(webbuff, "</button></td></table><table><td>"); |
faker_71 | 0:2e9a86765692 | 151 | strcat(webbuff, "</td><strong>Speed level:</strong>"); |
faker_71 | 0:2e9a86765692 | 152 | strcat(webbuff, "</table><table><td>"); |
faker_71 | 0:2e9a86765692 | 153 | 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 | 154 | strcat(webbuff, "</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 155 | 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 | 156 | strcat(webbuff, "</button>"); |
faker_71 | 0:2e9a86765692 | 157 | strcat(webbuff, "</td>"); |
faker_71 | 0:2e9a86765692 | 158 | strcat(webbuff, "<td>"); |
faker_71 | 0:2e9a86765692 | 159 | 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 | 160 | strcat(webbuff, "</button>"); |
faker_71 | 0:2e9a86765692 | 161 | strcat(webbuff, "</td></table>"); |
faker_71 | 0:2e9a86765692 | 162 | strcat(webbuff, "<table>"); |
faker_71 | 0:2e9a86765692 | 163 | strcat(webbuff, "<td>"); |
faker_71 | 0:2e9a86765692 | 164 | strcat(webbuff, "</td>"); |
faker_71 | 0:2e9a86765692 | 165 | strcat(webbuff, "</table>"); //end |
faker_71 | 0:2e9a86765692 | 166 | |
faker_71 | 0:2e9a86765692 | 167 | |
faker_71 | 0:2e9a86765692 | 168 | |
faker_71 | 0:2e9a86765692 | 169 | strcat(webbuff, "</center>"); |
faker_71 | 0:2e9a86765692 | 170 | strcat(webbuff, "</body>"); |
faker_71 | 0:2e9a86765692 | 171 | strcat(webbuff, "</html>"); |
faker_71 | 0:2e9a86765692 | 172 | strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">"); |
faker_71 | 0:2e9a86765692 | 173 | strcat(webbuff, "function send_mes(btnmes,btnval){"); |
faker_71 | 5:4026062a6fdf | 174 | /* |
faker_71 | 4:a365d6acec8f | 175 | strcat(webbuff, "try {xhr.abort();console.log(1);}"); |
faker_71 | 4:a365d6acec8f | 176 | strcat(webbuff, "catch(err) {console.log(0);}"); |
faker_71 | 5:4026062a6fdf | 177 | */ |
faker_71 | 0:2e9a86765692 | 178 | strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;"); |
faker_71 | 0:2e9a86765692 | 179 | strcat(webbuff, "htmlacs(url);"); |
faker_71 | 0:2e9a86765692 | 180 | strcat(webbuff, "console.log(url);"); |
faker_71 | 0:2e9a86765692 | 181 | strcat(webbuff, "}"); |
faker_71 | 2:1bf376317a43 | 182 | |
faker_71 | 0:2e9a86765692 | 183 | strcat(webbuff, "function htmlacs(url) {"); |
faker_71 | 0:2e9a86765692 | 184 | strcat(webbuff, "var xhr = new XMLHttpRequest();"); |
faker_71 | 2:1bf376317a43 | 185 | strcat(webbuff, "xhr.open(\"GET\", url ,true);"); |
faker_71 | 5:4026062a6fdf | 186 | /* |
faker_71 | 5:4026062a6fdf | 187 | //begin 0903 stop xml when state=2 |
faker_71 | 5:4026062a6fdf | 188 | strcat(webbuff, "xhr.onreadystatechange = function(){console.log(111);"); |
faker_71 | 5:4026062a6fdf | 189 | //strcat(webbuff, "xhr.onload = function(){console.log(111);"); |
faker_71 | 5:4026062a6fdf | 190 | |
faker_71 | 5:4026062a6fdf | 191 | //strcat(webbuff, "console.log(\"onready function is being reload!\");"); |
faker_71 | 5:4026062a6fdf | 192 | strcat(webbuff, "if(this.readyState == 1 ){"); |
faker_71 | 5:4026062a6fdf | 193 | strcat(webbuff, "console.log(222);this.abort();}};"); |
faker_71 | 5:4026062a6fdf | 194 | //strcat(webbuff, "console.log(res);}};"); |
faker_71 | 5:4026062a6fdf | 195 | |
faker_71 | 5:4026062a6fdf | 196 | //end 0903 stop xml when state=2 |
faker_71 | 5:4026062a6fdf | 197 | */ |
faker_71 | 5:4026062a6fdf | 198 | |
faker_71 | 2:1bf376317a43 | 199 | strcat(webbuff, "xhr.send();"); |
faker_71 | 5:4026062a6fdf | 200 | //strcat(webbuff, "console.log(xhr.readyState);"); |
faker_71 | 5:4026062a6fdf | 201 | //start abort after send |
faker_71 | 5:4026062a6fdf | 202 | strcat(webbuff, "setTimeout(function(){xhr.abort();},100);"); |
faker_71 | 5:4026062a6fdf | 203 | |
faker_71 | 5:4026062a6fdf | 204 | //strcat(webbuff, "setTimeout(xhr.abort(),\"100\");"); |
faker_71 | 5:4026062a6fdf | 205 | // strcat(webbuff, "xhr.abort();"); |
faker_71 | 5:4026062a6fdf | 206 | //end abort after send |
faker_71 | 5:4026062a6fdf | 207 | /* |
faker_71 | 5:4026062a6fdf | 208 | //begin 0903 esp onload kakkuninn |
faker_71 | 5:4026062a6fdf | 209 | strcat(webbuff, "xhr.onload = function() {concole.log(\"onload\");"); |
faker_71 | 5:4026062a6fdf | 210 | strcat(webbuff, "if (xhr.status != 200) { "); |
faker_71 | 5:4026062a6fdf | 211 | strcat(webbuff, "console.log(200);} else {"); |
faker_71 | 5:4026062a6fdf | 212 | strcat(webbuff, "console.log(xhr.responseText);"); |
faker_71 | 5:4026062a6fdf | 213 | strcat(webbuff, "Button_Update(xhr.responseText,\"0\");}};"); |
faker_71 | 5:4026062a6fdf | 214 | |
faker_71 | 5:4026062a6fdf | 215 | //end 0903 esp onload kakkuninn |
faker_71 | 5:4026062a6fdf | 216 | */ |
faker_71 | 0:2e9a86765692 | 217 | strcat(webbuff, "}"); |
faker_71 | 2:1bf376317a43 | 218 | |
faker_71 | 5:4026062a6fdf | 219 | //0903 begin abort function |
faker_71 | 5:4026062a6fdf | 220 | /* |
faker_71 | 5:4026062a6fdf | 221 | strcat(webbuff, "function abort_click(){"); |
faker_71 | 5:4026062a6fdf | 222 | strcat(webbuff, "xhr.abort();"); |
faker_71 | 5:4026062a6fdf | 223 | strcat(webbuff, "}"); |
faker_71 | 5:4026062a6fdf | 224 | */ |
faker_71 | 5:4026062a6fdf | 225 | |
faker_71 | 5:4026062a6fdf | 226 | //0903 end abort function |
faker_71 | 5:4026062a6fdf | 227 | |
faker_71 | 4:a365d6acec8f | 228 | //0902 beging |
faker_71 | 4:a365d6acec8f | 229 | /* |
faker_71 | 2:1bf376317a43 | 230 | //0824 battery update auto |
faker_71 | 2:1bf376317a43 | 231 | strcat(webbuff, "function battery_update() {"); |
faker_71 | 2:1bf376317a43 | 232 | strcat(webbuff, "var url1 = \"http://\" + window.location.hostname+ \"/cargo?a=responseBattery\";"); |
faker_71 | 2:1bf376317a43 | 233 | strcat(webbuff, "var xhr1 = new XMLHttpRequest();"); |
faker_71 | 2:1bf376317a43 | 234 | strcat(webbuff, "xhr1.open(\"GET\", url1);"); |
faker_71 | 2:1bf376317a43 | 235 | //0820 |
faker_71 | 2:1bf376317a43 | 236 | strcat(webbuff, "xhr1.onreadystatechange = function(){"); |
faker_71 | 2:1bf376317a43 | 237 | //strcat(webbuff, "console.log(\"onready function is being reload!\");"); |
faker_71 | 2:1bf376317a43 | 238 | strcat(webbuff, "if(this.readyState == 4 && this.status == 200){"); |
faker_71 | 2:1bf376317a43 | 239 | //strcat(webbuff, "console.log(\"state is being reload!\");"); |
faker_71 | 2:1bf376317a43 | 240 | strcat(webbuff, "var res1 = xhr1.responseText;"); |
faker_71 | 2:1bf376317a43 | 241 | strcat(webbuff, "document.getElementById('leftms').value=res1;}};"); |
faker_71 | 2:1bf376317a43 | 242 | //strcat(webbuff, "console.log(res);}};"); |
faker_71 | 2:1bf376317a43 | 243 | //0820 |
faker_71 | 2:1bf376317a43 | 244 | |
faker_71 | 2:1bf376317a43 | 245 | strcat(webbuff, "xhr1.send();"); |
faker_71 | 2:1bf376317a43 | 246 | strcat(webbuff, "}"); |
faker_71 | 2:1bf376317a43 | 247 | strcat(webbuff, "setInterval(battery_update,10000);"); |
faker_71 | 4:a365d6acec8f | 248 | */ |
faker_71 | 4:a365d6acec8f | 249 | //0902 end |
faker_71 | 2:1bf376317a43 | 250 | //0824 battery update auto |
faker_71 | 2:1bf376317a43 | 251 | /* |
faker_71 | 0:2e9a86765692 | 252 | strcat(webbuff, "function send_mes_spe(btnmes,btnval){"); |
faker_71 | 0:2e9a86765692 | 253 | strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;"); |
faker_71 | 0:2e9a86765692 | 254 | strcat(webbuff, "htmlacs(url);"); |
faker_71 | 0:2e9a86765692 | 255 | strcat(webbuff, "if(document.getElementById(\"speprint\")){"); |
faker_71 | 0:2e9a86765692 | 256 | strcat(webbuff, "document.getElementById(\"speprint\").innerHTML=btnval;"); |
faker_71 | 0:2e9a86765692 | 257 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 258 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 259 | strcat(webbuff, "function send_mes_fun(btnmes,btnval){"); |
faker_71 | 0:2e9a86765692 | 260 | strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;"); |
faker_71 | 0:2e9a86765692 | 261 | strcat(webbuff, "htmlacs(url);"); |
faker_71 | 0:2e9a86765692 | 262 | strcat(webbuff, "if(document.getElementById(\"funprint\")){"); |
faker_71 | 0:2e9a86765692 | 263 | strcat(webbuff, "document.getElementById(\"funprint\").innerHTML=btnval;"); |
faker_71 | 0:2e9a86765692 | 264 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 265 | strcat(webbuff, "}"); |
faker_71 | 2:1bf376317a43 | 266 | */ |
faker_71 | 0:2e9a86765692 | 267 | strcat(webbuff, "</script>"); |
faker_71 | 0:2e9a86765692 | 268 | // end of WEB page data |
faker_71 | 0:2e9a86765692 | 269 | bufl = strlen(webbuff); // get total page buffer length |
faker_71 | 0:2e9a86765692 | 270 | //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
faker_71 | 0:2e9a86765692 | 271 | |
faker_71 | 0:2e9a86765692 | 272 | 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 | 273 | timeout=500; |
faker_71 | 0:2e9a86765692 | 274 | getcount=40; |
faker_71 | 0:2e9a86765692 | 275 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 276 | getreply(); |
faker_71 | 0:2e9a86765692 | 277 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 278 | pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl)); |
faker_71 | 0:2e9a86765692 | 279 | |
faker_71 | 0:2e9a86765692 | 280 | pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl); |
faker_71 | 0:2e9a86765692 | 281 | |
faker_71 | 0:2e9a86765692 | 282 | //pastthrough mode |
faker_71 | 0:2e9a86765692 | 283 | SendWEB(); // send web page |
faker_71 | 0:2e9a86765692 | 284 | pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 285 | |
faker_71 | 0:2e9a86765692 | 286 | memset(webbuff, '\0', sizeof(webbuff)); |
faker_71 | 0:2e9a86765692 | 287 | sendcheck(); |
faker_71 | 0:2e9a86765692 | 288 | } |
faker_71 | 0:2e9a86765692 | 289 | |
faker_71 | 2:1bf376317a43 | 290 | |
faker_71 | 2:1bf376317a43 | 291 | void sendpage2() |
faker_71 | 0:2e9a86765692 | 292 | { |
faker_71 | 2:1bf376317a43 | 293 | // WEB page data |
faker_71 | 4:a365d6acec8f | 294 | pred=!pred; |
faker_71 | 4:a365d6acec8f | 295 | /* |
faker_71 | 2:1bf376317a43 | 296 | //strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">"); |
faker_71 | 2:1bf376317a43 | 297 | //strcat(webbuff, "document.getElementById('leftms').value=\"100\";"); |
faker_71 | 2:1bf376317a43 | 298 | count_test++; |
faker_71 | 2:1bf376317a43 | 299 | sprintf(battery_ch,"%d",count_test); |
faker_71 | 2:1bf376317a43 | 300 | strcpy(webbuff, battery_ch); |
faker_71 | 2:1bf376317a43 | 301 | |
faker_71 | 2:1bf376317a43 | 302 | //strcpy(webbuff, "document.getElementById('leftms').value=\"100\";"); |
faker_71 | 2:1bf376317a43 | 303 | |
faker_71 | 2:1bf376317a43 | 304 | //strcat(webbuff, "</script>"); |
faker_71 | 2:1bf376317a43 | 305 | // end of WEB page data |
faker_71 | 2:1bf376317a43 | 306 | bufl = strlen(webbuff); // get total page buffer length |
faker_71 | 2:1bf376317a43 | 307 | //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
faker_71 | 2:1bf376317a43 | 308 | |
faker_71 | 2:1bf376317a43 | 309 | 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 | 310 | timeout=500; |
faker_71 | 2:1bf376317a43 | 311 | getcount=40; |
faker_71 | 2:1bf376317a43 | 312 | SendCMD(); |
faker_71 | 2:1bf376317a43 | 313 | getreply(); |
faker_71 | 2:1bf376317a43 | 314 | pc.printf(replybuff); |
faker_71 | 2:1bf376317a43 | 315 | pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl)); |
faker_71 | 2:1bf376317a43 | 316 | |
faker_71 | 2:1bf376317a43 | 317 | pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl); |
faker_71 | 2:1bf376317a43 | 318 | |
faker_71 | 2:1bf376317a43 | 319 | //pastthrough mode |
faker_71 | 2:1bf376317a43 | 320 | SendWEB(); // send web page |
faker_71 | 2:1bf376317a43 | 321 | pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n"); |
faker_71 | 2:1bf376317a43 | 322 | |
faker_71 | 2:1bf376317a43 | 323 | memset(webbuff, '\0', sizeof(webbuff)); |
faker_71 | 2:1bf376317a43 | 324 | sendcheck(); |
faker_71 | 4:a365d6acec8f | 325 | |
faker_71 | 4:a365d6acec8f | 326 | */ |
faker_71 | 0:2e9a86765692 | 327 | } |
faker_71 | 0:2e9a86765692 | 328 | |
faker_71 | 0:2e9a86765692 | 329 | |
faker_71 | 0:2e9a86765692 | 330 | // Reads and processes GET and POST web data |
faker_71 | 0:2e9a86765692 | 331 | void ReadWebData() |
faker_71 | 0:2e9a86765692 | 332 | { |
faker_71 | 4:a365d6acec8f | 333 | pc.printf("+++++++++++++++++Read Web Data begin+++++++++++++++++++++\r\n"); |
faker_71 | 4:a365d6acec8f | 334 | |
faker_71 | 4:a365d6acec8f | 335 | //0902 is this wait neccessary |
faker_71 | 4:a365d6acec8f | 336 | // wait_ms(200); |
faker_71 | 4:a365d6acec8f | 337 | |
faker_71 | 0:2e9a86765692 | 338 | esp.attach(NULL); |
faker_71 | 0:2e9a86765692 | 339 | count_wifi=0; |
faker_71 | 0:2e9a86765692 | 340 | DataRX=0; |
faker_71 | 0:2e9a86765692 | 341 | weberror=0; |
faker_71 | 0:2e9a86765692 | 342 | memset(webdata, '\0', sizeof(webdata)); |
faker_71 | 0:2e9a86765692 | 343 | int x = strcspn (webbuff,"+"); |
faker_71 | 0:2e9a86765692 | 344 | if(x) { |
faker_71 | 0:2e9a86765692 | 345 | strcpy(webdata, webbuff + x); |
faker_71 | 0:2e9a86765692 | 346 | weberror=0; |
faker_71 | 0:2e9a86765692 | 347 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
faker_71 | 0:2e9a86765692 | 348 | //int i=0; |
faker_71 | 4:a365d6acec8f | 349 | pc.printf("\r\n+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 350 | pc.printf("%s",webdata); |
faker_71 | 4:a365d6acec8f | 351 | pc.printf("\r\n+++++++++++++++++succed rec end+++++++++++++++++++++\r\n"); |
faker_71 | 4:a365d6acec8f | 352 | // click or login |
faker_71 | 4:a365d6acec8f | 353 | if( strstr(webdata, "cargo") != NULL ) { |
faker_71 | 4:a365d6acec8f | 354 | click_log=true; |
faker_71 | 4:a365d6acec8f | 355 | pc.printf("\r\n++this is click++\r\n"); |
faker_71 | 4:a365d6acec8f | 356 | |
faker_71 | 4:a365d6acec8f | 357 | } |
faker_71 | 2:1bf376317a43 | 358 | if( strstr(webdata, "responseBattery") != NULL ) { |
faker_71 | 0:2e9a86765692 | 359 | click_flag = 0; |
faker_71 | 0:2e9a86765692 | 360 | led4=!led4; |
faker_71 | 2:1bf376317a43 | 361 | pc.printf("\r\n++++++++++++++mode = LEFT++++++++++++++\r\n"); |
faker_71 | 2:1bf376317a43 | 362 | sendpage2(); |
faker_71 | 2:1bf376317a43 | 363 | /* |
faker_71 | 2:1bf376317a43 | 364 | strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">"); |
faker_71 | 2:1bf376317a43 | 365 | strcat(webbuff, "document.getElementById('leftms').value=\"96\";"); |
faker_71 | 2:1bf376317a43 | 366 | strcat(webbuff, "let request = new XMLHttpRequest();"); |
faker_71 | 2:1bf376317a43 | 367 | strcat(webbuff, "request.open('GET', url);request.responseType = 'text';"); |
faker_71 | 2:1bf376317a43 | 368 | strcat(webbuff, "request.onload = function() { poemDisplay.textContent = request.response;};"); |
faker_71 | 2:1bf376317a43 | 369 | |
faker_71 | 2:1bf376317a43 | 370 | strcat(webbuff, "request.send();"); |
faker_71 | 2:1bf376317a43 | 371 | strcat(webbuff, "</script>"); |
faker_71 | 2:1bf376317a43 | 372 | bufl = strlen(webbuff); // get total page buffer length |
faker_71 | 2:1bf376317a43 | 373 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
faker_71 | 2:1bf376317a43 | 374 | timeout=500; |
faker_71 | 2:1bf376317a43 | 375 | getcount=40; |
faker_71 | 2:1bf376317a43 | 376 | SendCMD(); |
faker_71 | 2:1bf376317a43 | 377 | getreply(); |
faker_71 | 2:1bf376317a43 | 378 | SendWEB(); |
faker_71 | 2:1bf376317a43 | 379 | memset(webbuff, '\0', sizeof(webbuff)); |
faker_71 | 2:1bf376317a43 | 380 | sendcheck(); |
faker_71 | 2:1bf376317a43 | 381 | pc.printf("\r\n++++++++++++++mode = LEFT++++++++++++++\r\n"); |
faker_71 | 2:1bf376317a43 | 382 | */ |
faker_71 | 2:1bf376317a43 | 383 | |
faker_71 | 2:1bf376317a43 | 384 | /* mode = LEFT; // 左折モード |
faker_71 | 0:2e9a86765692 | 385 | run = LEFT; // 左折 |
faker_71 | 2:1bf376317a43 | 386 | display(); */ |
faker_71 | 2:1bf376317a43 | 387 | |
faker_71 | 0:2e9a86765692 | 388 | } |
faker_71 | 3:4c5c556c97a3 | 389 | if( strstr(webdata, "LEFT") != NULL ) { |
faker_71 | 4:a365d6acec8f | 390 | sendpage2(); |
faker_71 | 3:4c5c556c97a3 | 391 | led4=!led4; |
faker_71 | 4:a365d6acec8f | 392 | // pc.printf("mode = RIGHT\r\n"); |
faker_71 | 3:4c5c556c97a3 | 393 | |
faker_71 | 3:4c5c556c97a3 | 394 | } |
faker_71 | 4:a365d6acec8f | 395 | |
faker_71 | 0:2e9a86765692 | 396 | if( strstr(webdata, "RIGHT") != NULL ) { |
faker_71 | 4:a365d6acec8f | 397 | sendpage2(); |
faker_71 | 0:2e9a86765692 | 398 | led4=!led4; |
faker_71 | 4:a365d6acec8f | 399 | |
faker_71 | 0:2e9a86765692 | 400 | } |
faker_71 | 0:2e9a86765692 | 401 | |
faker_71 | 4:a365d6acec8f | 402 | /* |
faker_71 | 0:2e9a86765692 | 403 | if( strstr(webdata, "GO") != NULL ) { |
faker_71 | 0:2e9a86765692 | 404 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 405 | pc.printf("mode = ADVANCE\r\n"); |
faker_71 | 0:2e9a86765692 | 406 | mode = ADVANCE; // 前進モード |
faker_71 | 0:2e9a86765692 | 407 | run = ADVANCE; // 前進 |
faker_71 | 0:2e9a86765692 | 408 | display(); // ディスプレイ表示 |
faker_71 | 0:2e9a86765692 | 409 | } |
faker_71 | 0:2e9a86765692 | 410 | |
faker_71 | 0:2e9a86765692 | 411 | if( strstr(webdata, "BACK") != NULL ) { |
faker_71 | 0:2e9a86765692 | 412 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 413 | pc.printf("mode = BACK\r\n"); |
faker_71 | 0:2e9a86765692 | 414 | mode = BACK; // 後退モード |
faker_71 | 0:2e9a86765692 | 415 | run = BACK; // 後退 |
faker_71 | 0:2e9a86765692 | 416 | display(); // ディスプレイ表示 |
faker_71 | 0:2e9a86765692 | 417 | } |
faker_71 | 0:2e9a86765692 | 418 | |
faker_71 | 0:2e9a86765692 | 419 | if( strstr(webdata, "STOP") != NULL ) { |
faker_71 | 0:2e9a86765692 | 420 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 421 | pc.printf("mode = STOP\r\n"); |
faker_71 | 0:2e9a86765692 | 422 | mode = STOP; // 停止モード |
faker_71 | 0:2e9a86765692 | 423 | run = STOP; // 停止 |
faker_71 | 0:2e9a86765692 | 424 | display(); // ディスプレイ表示 |
faker_71 | 0:2e9a86765692 | 425 | } |
faker_71 | 0:2e9a86765692 | 426 | |
faker_71 | 0:2e9a86765692 | 427 | if( strstr(webdata, "AVOIDANCE") != NULL ) { |
faker_71 | 0:2e9a86765692 | 428 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 429 | pc.printf("mode = AVOIDANCE\r\n"); |
faker_71 | 0:2e9a86765692 | 430 | mode=AVOIDANCE; // 障害物回避モード |
faker_71 | 0:2e9a86765692 | 431 | run = ADVANCE; // 前進 |
faker_71 | 0:2e9a86765692 | 432 | display(); |
faker_71 | 0:2e9a86765692 | 433 | } |
faker_71 | 0:2e9a86765692 | 434 | |
faker_71 | 0:2e9a86765692 | 435 | if( strstr(webdata, "LINETRACE") != NULL ) { |
faker_71 | 0:2e9a86765692 | 436 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 437 | pc.printf("mode = LINE_TRACE\r\n"); |
faker_71 | 0:2e9a86765692 | 438 | mode=LINE_TRACE; // ライントレースモード |
faker_71 | 0:2e9a86765692 | 439 | display(); |
faker_71 | 0:2e9a86765692 | 440 | } |
faker_71 | 0:2e9a86765692 | 441 | |
faker_71 | 0:2e9a86765692 | 442 | if( strstr(webdata, "SLOW") != NULL ) { |
faker_71 | 0:2e9a86765692 | 443 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 444 | pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 445 | } |
faker_71 | 0:2e9a86765692 | 446 | |
faker_71 | 0:2e9a86765692 | 447 | if( strstr(webdata, "FAST") != NULL ) { |
faker_71 | 0:2e9a86765692 | 448 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 449 | pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 450 | } |
faker_71 | 0:2e9a86765692 | 451 | |
faker_71 | 0:2e9a86765692 | 452 | if( strstr(webdata, "VERYFAST") != NULL ) { |
faker_71 | 0:2e9a86765692 | 453 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 454 | pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 455 | }*/ |
faker_71 | 0:2e9a86765692 | 456 | sprintf(channel, "%d",linkID); |
faker_71 | 0:2e9a86765692 | 457 | if (strstr(webdata, "GET") != NULL) { |
faker_71 | 0:2e9a86765692 | 458 | servreq=1; |
faker_71 | 0:2e9a86765692 | 459 | pc.printf("\r\n+++++++++++++++++GET+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 460 | |
faker_71 | 0:2e9a86765692 | 461 | } |
faker_71 | 0:2e9a86765692 | 462 | if (strstr(webdata, "POST") != NULL) { |
faker_71 | 0:2e9a86765692 | 463 | servreq=1; |
faker_71 | 0:2e9a86765692 | 464 | pc.printf("\r\n+++++++++++++++++POST+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 465 | } |
faker_71 | 0:2e9a86765692 | 466 | webcounter++; |
faker_71 | 0:2e9a86765692 | 467 | sprintf(webcount, "%d",webcounter); |
faker_71 | 0:2e9a86765692 | 468 | } else { |
faker_71 | 0:2e9a86765692 | 469 | pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 470 | memset(webbuff, '\0', sizeof(webbuff)); |
faker_71 | 0:2e9a86765692 | 471 | esp.attach(&callback); |
faker_71 | 0:2e9a86765692 | 472 | weberror=1; |
faker_71 | 0:2e9a86765692 | 473 | } |
faker_71 | 4:a365d6acec8f | 474 | |
faker_71 | 4:a365d6acec8f | 475 | pc.printf("+++++++++++++++++Read Web Data end+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 476 | } |
faker_71 | 2:1bf376317a43 | 477 | // Large WEB buffer data send |
faker_71 | 2:1bf376317a43 | 478 | void SendWEB() |
faker_71 | 2:1bf376317a43 | 479 | { |
faker_71 | 2:1bf376317a43 | 480 | int i=0; |
faker_71 | 2:1bf376317a43 | 481 | if(esp.writeable()) { |
faker_71 | 2:1bf376317a43 | 482 | while(webbuff[i]!='\0') { |
faker_71 | 2:1bf376317a43 | 483 | esp.putc(webbuff[i]); |
faker_71 | 2:1bf376317a43 | 484 | |
faker_71 | 2:1bf376317a43 | 485 | //**** |
faker_71 | 2:1bf376317a43 | 486 | //output at command when 2000 |
faker_71 | 2:1bf376317a43 | 487 | if(((i%2047)==0) && (i>0)){ |
faker_71 | 2:1bf376317a43 | 488 | //wait_ms(10); |
faker_71 | 2:1bf376317a43 | 489 | 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 | 490 | pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048)); |
faker_71 | 2:1bf376317a43 | 491 | timeout=600; |
faker_71 | 2:1bf376317a43 | 492 | getcount=50; |
faker_71 | 2:1bf376317a43 | 493 | SendCMD(); |
faker_71 | 2:1bf376317a43 | 494 | getreply(); |
faker_71 | 2:1bf376317a43 | 495 | pc.printf(replybuff); |
faker_71 | 2:1bf376317a43 | 496 | pc.printf("\r\n+++++++++++++++++++\r\n"); |
faker_71 | 2:1bf376317a43 | 497 | } |
faker_71 | 2:1bf376317a43 | 498 | //**** |
faker_71 | 2:1bf376317a43 | 499 | i++; |
faker_71 | 2:1bf376317a43 | 500 | pc.printf("%c",webbuff[i]); |
faker_71 | 2:1bf376317a43 | 501 | } |
faker_71 | 2:1bf376317a43 | 502 | } |
faker_71 | 2:1bf376317a43 | 503 | pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i); |
faker_71 | 2:1bf376317a43 | 504 | } |
faker_71 | 2:1bf376317a43 | 505 | |
faker_71 | 2:1bf376317a43 | 506 | |
faker_71 | 2:1bf376317a43 | 507 | |
faker_71 | 2:1bf376317a43 | 508 | void sendcheck() |
faker_71 | 2:1bf376317a43 | 509 | { |
faker_71 | 2:1bf376317a43 | 510 | weberror=1; |
faker_71 | 2:1bf376317a43 | 511 | timeout=500; |
faker_71 | 2:1bf376317a43 | 512 | getcount=24; |
faker_71 | 2:1bf376317a43 | 513 | t2.reset(); |
faker_71 | 2:1bf376317a43 | 514 | t2.start(); |
faker_71 | 2:1bf376317a43 | 515 | |
faker_71 | 2:1bf376317a43 | 516 | /* |
faker_71 | 2:1bf376317a43 | 517 | while(weberror==1 && t2.read() <5) { |
faker_71 | 2:1bf376317a43 | 518 | getreply(); |
faker_71 | 2:1bf376317a43 | 519 | if (strstr(replybuff, "SEND OK") != NULL) { |
faker_71 | 2:1bf376317a43 | 520 | weberror=0; // wait for valid SEND OK |
faker_71 | 2:1bf376317a43 | 521 | } |
faker_71 | 2:1bf376317a43 | 522 | } |
faker_71 | 2:1bf376317a43 | 523 | */ |
faker_71 | 2:1bf376317a43 | 524 | if(weberror==1) { // restart connection |
faker_71 | 2:1bf376317a43 | 525 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
faker_71 | 2:1bf376317a43 | 526 | timeout=500; |
faker_71 | 2:1bf376317a43 | 527 | getcount=10; |
faker_71 | 2:1bf376317a43 | 528 | SendCMD(); |
faker_71 | 2:1bf376317a43 | 529 | getreply(); |
faker_71 | 2:1bf376317a43 | 530 | pc.printf(replybuff); |
faker_71 | 2:1bf376317a43 | 531 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
faker_71 | 2:1bf376317a43 | 532 | timeout=500; |
faker_71 | 2:1bf376317a43 | 533 | getcount=10; |
faker_71 | 2:1bf376317a43 | 534 | SendCMD(); |
faker_71 | 2:1bf376317a43 | 535 | getreply(); |
faker_71 | 2:1bf376317a43 | 536 | pc.printf(replybuff); |
faker_71 | 2:1bf376317a43 | 537 | } else { |
faker_71 | 2:1bf376317a43 | 538 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
faker_71 | 2:1bf376317a43 | 539 | SendCMD(); |
faker_71 | 2:1bf376317a43 | 540 | getreply(); |
faker_71 | 2:1bf376317a43 | 541 | pc.printf(replybuff); |
faker_71 | 2:1bf376317a43 | 542 | } |
faker_71 | 2:1bf376317a43 | 543 | t2.reset(); |
faker_71 | 2:1bf376317a43 | 544 | } |
faker_71 | 2:1bf376317a43 | 545 | |
faker_71 | 0:2e9a86765692 | 546 | // Starts and restarts webserver if errors detected. |
faker_71 | 0:2e9a86765692 | 547 | void startserver() |
faker_71 | 0:2e9a86765692 | 548 | { |
faker_71 | 0:2e9a86765692 | 549 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 550 | strcpy(cmdbuff,"AT+RST\r\n"); |
faker_71 | 0:2e9a86765692 | 551 | timeout=8000; |
faker_71 | 0:2e9a86765692 | 552 | getcount=1000; |
faker_71 | 0:2e9a86765692 | 553 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 554 | getreply(); |
faker_71 | 0:2e9a86765692 | 555 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 556 | pc.printf("%d",count_wifi); |
faker_71 | 0:2e9a86765692 | 557 | if (strstr(replybuff, "OK") != NULL) { |
faker_71 | 0:2e9a86765692 | 558 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 559 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
faker_71 | 0:2e9a86765692 | 560 | timeout=500; |
faker_71 | 0:2e9a86765692 | 561 | getcount=20; |
faker_71 | 0:2e9a86765692 | 562 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 563 | getreply(); |
faker_71 | 0:2e9a86765692 | 564 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 565 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
faker_71 | 0:2e9a86765692 | 566 | timeout=500; |
faker_71 | 0:2e9a86765692 | 567 | getcount=20; |
faker_71 | 0:2e9a86765692 | 568 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 569 | getreply(); |
faker_71 | 0:2e9a86765692 | 570 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 571 | wait(1); |
faker_71 | 0:2e9a86765692 | 572 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
faker_71 | 0:2e9a86765692 | 573 | timeout=500; |
faker_71 | 0:2e9a86765692 | 574 | getcount=50; |
faker_71 | 0:2e9a86765692 | 575 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 576 | getreply(); |
faker_71 | 0:2e9a86765692 | 577 | pc.printf(replybuff); |
faker_71 | 1:a45cf46c1adc | 578 | //wait(5); |
faker_71 | 1:a45cf46c1adc | 579 | wait(1); |
faker_71 | 0:2e9a86765692 | 580 | pc.printf("\n Getting Server IP \r\n"); |
faker_71 | 0:2e9a86765692 | 581 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
faker_71 | 0:2e9a86765692 | 582 | timeout=2500; |
faker_71 | 0:2e9a86765692 | 583 | getcount=200; |
faker_71 | 0:2e9a86765692 | 584 | while(weberror==0) { |
faker_71 | 0:2e9a86765692 | 585 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 586 | getreply(); |
faker_71 | 0:2e9a86765692 | 587 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
faker_71 | 0:2e9a86765692 | 588 | weberror=1; // wait for valid IP |
faker_71 | 0:2e9a86765692 | 589 | } |
faker_71 | 0:2e9a86765692 | 590 | } |
faker_71 | 0:2e9a86765692 | 591 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
faker_71 | 0:2e9a86765692 | 592 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
faker_71 | 0:2e9a86765692 | 593 | replybuff[strlen(replybuff)-1] = '\0'; |
faker_71 | 0:2e9a86765692 | 594 | //char* IP = replybuff + 5; |
faker_71 | 0:2e9a86765692 | 595 | sprintf(webdata,"%s", replybuff); |
faker_71 | 0:2e9a86765692 | 596 | pc.printf(webdata); |
faker_71 | 0:2e9a86765692 | 597 | led2=1; |
faker_71 | 0:2e9a86765692 | 598 | bufflen=200; |
faker_71 | 0:2e9a86765692 | 599 | //bufflen=100; |
faker_71 | 0:2e9a86765692 | 600 | count_wifi=0; |
faker_71 | 0:2e9a86765692 | 601 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
faker_71 | 0:2e9a86765692 | 602 | esp.attach(&callback); |
faker_71 | 0:2e9a86765692 | 603 | } else { |
faker_71 | 0:2e9a86765692 | 604 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 605 | led1=1; |
faker_71 | 0:2e9a86765692 | 606 | led2=1; |
faker_71 | 0:2e9a86765692 | 607 | led3=1; |
faker_71 | 0:2e9a86765692 | 608 | led4=1; |
faker_71 | 0:2e9a86765692 | 609 | while(1) { |
faker_71 | 0:2e9a86765692 | 610 | led1=!led1; |
faker_71 | 0:2e9a86765692 | 611 | led2=!led2; |
faker_71 | 0:2e9a86765692 | 612 | led3=!led3; |
faker_71 | 0:2e9a86765692 | 613 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 614 | wait(1); |
faker_71 | 0:2e9a86765692 | 615 | } |
faker_71 | 0:2e9a86765692 | 616 | } |
faker_71 | 0:2e9a86765692 | 617 | t2.reset(); |
faker_71 | 0:2e9a86765692 | 618 | t2.start(); |
faker_71 | 0:2e9a86765692 | 619 | } |
faker_71 | 0:2e9a86765692 | 620 | // ESP Command data send |
faker_71 | 0:2e9a86765692 | 621 | void SendCMD() |
faker_71 | 0:2e9a86765692 | 622 | { |
faker_71 | 0:2e9a86765692 | 623 | esp.printf("%s", cmdbuff); |
faker_71 | 0:2e9a86765692 | 624 | } |
faker_71 | 0:2e9a86765692 | 625 | // Get Command and ESP status replies |
faker_71 | 0:2e9a86765692 | 626 | void getreply() |
faker_71 | 0:2e9a86765692 | 627 | { |
faker_71 | 0:2e9a86765692 | 628 | memset(replybuff, '\0', sizeof(replybuff)); |
faker_71 | 0:2e9a86765692 | 629 | t1.reset(); |
faker_71 | 0:2e9a86765692 | 630 | t1.start(); |
faker_71 | 0:2e9a86765692 | 631 | replycount=0; |
faker_71 | 0:2e9a86765692 | 632 | while(t1.read_ms()< timeout && replycount < getcount) { |
faker_71 | 0:2e9a86765692 | 633 | if(esp.readable()) { |
faker_71 | 0:2e9a86765692 | 634 | replybuff[replycount] = esp.getc(); |
faker_71 | 0:2e9a86765692 | 635 | replycount++; |
faker_71 | 0:2e9a86765692 | 636 | } |
faker_71 | 0:2e9a86765692 | 637 | } |
faker_71 | 0:2e9a86765692 | 638 | t1.stop(); |
faker_71 | 0:2e9a86765692 | 639 | } |