real time wifi
Dependencies: RemoteIR TextLCD
main.cpp@1:a45cf46c1adc, 2020-08-06 (annotated)
- Committer:
- faker_71
- Date:
- Thu Aug 06 01:50:55 2020 +0000
- Revision:
- 1:a45cf46c1adc
- Parent:
- 0:2e9a86765692
- Child:
- 2:1bf376317a43
yo
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 | 0:2e9a86765692 | 16 | |
faker_71 | 0:2e9a86765692 | 17 | Timer t1; |
faker_71 | 0:2e9a86765692 | 18 | Timer t2; |
faker_71 | 0:2e9a86765692 | 19 | |
faker_71 | 0:2e9a86765692 | 20 | struct tm t; |
faker_71 | 0:2e9a86765692 | 21 | |
faker_71 | 0:2e9a86765692 | 22 | |
faker_71 | 0:2e9a86765692 | 23 | int bufflen, DataRX, count_wifi, getcount, replycount, servreq, timeout; |
faker_71 | 0:2e9a86765692 | 24 | int bufl, ipdLen, linkID, weberror, webcounter,click_flag; |
faker_71 | 0:2e9a86765692 | 25 | float temperature, AdcIn, Ht; |
faker_71 | 0:2e9a86765692 | 26 | float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage |
faker_71 | 0:2e9a86765692 | 27 | char Vcc[10]; |
faker_71 | 0:2e9a86765692 | 28 | char webcount[8]; |
faker_71 | 0:2e9a86765692 | 29 | char type[16]; |
faker_71 | 0:2e9a86765692 | 30 | char type1[16]; |
faker_71 | 0:2e9a86765692 | 31 | char channel[2]; |
faker_71 | 0:2e9a86765692 | 32 | char cmdbuff[32]; |
faker_71 | 0:2e9a86765692 | 33 | char replybuff[1024]; |
faker_71 | 0:2e9a86765692 | 34 | |
faker_71 | 0:2e9a86765692 | 35 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
faker_71 | 0:2e9a86765692 | 36 | char webbuff[4096*4]; // Currently using 1986 characters, Increase this if more web page data added |
faker_71 | 0:2e9a86765692 | 37 | |
faker_71 | 0:2e9a86765692 | 38 | |
faker_71 | 0:2e9a86765692 | 39 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(),touchuan(); |
faker_71 | 0:2e9a86765692 | 40 | void wifi(void const *argument); |
faker_71 | 0:2e9a86765692 | 41 | |
faker_71 | 0:2e9a86765692 | 42 | Thread wifi_thread(wifi, NULL, osPriorityNormal); // decodeIRをスレッド化 :+3 |
faker_71 | 0:2e9a86765692 | 43 | |
faker_71 | 0:2e9a86765692 | 44 | int port =80; // set server port |
faker_71 | 0:2e9a86765692 | 45 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
faker_71 | 0:2e9a86765692 | 46 | |
faker_71 | 0:2e9a86765692 | 47 | // Serial Interrupt read ESP data |
faker_71 | 0:2e9a86765692 | 48 | void callback() |
faker_71 | 0:2e9a86765692 | 49 | { |
faker_71 | 0:2e9a86765692 | 50 | //pc.printf("\n\r------------ callback is being called --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 51 | led3=1; |
faker_71 | 0:2e9a86765692 | 52 | while (esp.readable()) { |
faker_71 | 0:2e9a86765692 | 53 | webbuff[count_wifi] = esp.getc(); |
faker_71 | 0:2e9a86765692 | 54 | count_wifi++; |
faker_71 | 0:2e9a86765692 | 55 | } |
faker_71 | 0:2e9a86765692 | 56 | if(strlen(webbuff)>bufflen) { |
faker_71 | 0:2e9a86765692 | 57 | pc.printf("\f\n\r------------ webbuff over bufflen --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 58 | DataRX=1; |
faker_71 | 0:2e9a86765692 | 59 | led3=0; |
faker_71 | 0:2e9a86765692 | 60 | } |
faker_71 | 0:2e9a86765692 | 61 | } |
faker_71 | 0:2e9a86765692 | 62 | |
faker_71 | 0:2e9a86765692 | 63 | void wifi(void const *argument){ |
faker_71 | 0:2e9a86765692 | 64 | pc.baud(115200); |
faker_71 | 0:2e9a86765692 | 65 | |
faker_71 | 0:2e9a86765692 | 66 | pc.printf("\f\n\r------------ ESP8266 Hardware Reset psq --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 67 | wait(0.5); |
faker_71 | 0:2e9a86765692 | 68 | led1=1,led2=0,led3=0; |
faker_71 | 0:2e9a86765692 | 69 | timeout=6000; |
faker_71 | 0:2e9a86765692 | 70 | getcount=500; |
faker_71 | 0:2e9a86765692 | 71 | getreply(); |
faker_71 | 0:2e9a86765692 | 72 | esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
faker_71 | 0:2e9a86765692 | 73 | startserver(); |
faker_71 | 0:2e9a86765692 | 74 | |
faker_71 | 0:2e9a86765692 | 75 | while(1) { |
faker_71 | 0:2e9a86765692 | 76 | if(DataRX==1) { |
faker_71 | 0:2e9a86765692 | 77 | pc.printf("\f\n\r------------ main while > if --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 78 | click_flag = 1; |
faker_71 | 0:2e9a86765692 | 79 | ReadWebData(); |
faker_71 | 0:2e9a86765692 | 80 | pc.printf("\f\n\r------------ click_flag=%d --------------\n\r",click_flag); |
faker_71 | 0:2e9a86765692 | 81 | //if ((servreq == 1 && weberror == 0) && click_flag == 1) { |
faker_71 | 0:2e9a86765692 | 82 | if (servreq == 1 && weberror == 0) { |
faker_71 | 0:2e9a86765692 | 83 | pc.printf("\f\n\r------------ befor send page --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 84 | sendpage(); |
faker_71 | 0:2e9a86765692 | 85 | } |
faker_71 | 0:2e9a86765692 | 86 | pc.printf("\f\n\r------------ send_check begin --------------\n\r"); |
faker_71 | 0:2e9a86765692 | 87 | |
faker_71 | 0:2e9a86765692 | 88 | //sendcheck(); |
faker_71 | 0:2e9a86765692 | 89 | pc.printf("\f\n\r------------ ssend_check end--------------\n\r"); |
faker_71 | 0:2e9a86765692 | 90 | |
faker_71 | 0:2e9a86765692 | 91 | esp.attach(&callback); |
faker_71 | 0:2e9a86765692 | 92 | 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 | 93 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
faker_71 | 0:2e9a86765692 | 94 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
faker_71 | 0:2e9a86765692 | 95 | pc.printf(" -------------------------------------\n\n"); |
faker_71 | 0:2e9a86765692 | 96 | servreq=0; |
faker_71 | 0:2e9a86765692 | 97 | } |
faker_71 | 0:2e9a86765692 | 98 | } |
faker_71 | 0:2e9a86765692 | 99 | } |
faker_71 | 0:2e9a86765692 | 100 | int main() |
faker_71 | 0:2e9a86765692 | 101 | { |
faker_71 | 0:2e9a86765692 | 102 | while(1){ |
faker_71 | 0:2e9a86765692 | 103 | Thread::wait(100); |
faker_71 | 0:2e9a86765692 | 104 | } |
faker_71 | 0:2e9a86765692 | 105 | } |
faker_71 | 0:2e9a86765692 | 106 | // Static WEB page |
faker_71 | 0:2e9a86765692 | 107 | void sendpage() |
faker_71 | 0:2e9a86765692 | 108 | { |
faker_71 | 0:2e9a86765692 | 109 | // WEB page data |
faker_71 | 0:2e9a86765692 | 110 | |
faker_71 | 0:2e9a86765692 | 111 | strcpy(webbuff, "<!DOCTYPE html>"); |
faker_71 | 0:2e9a86765692 | 112 | strcat(webbuff, "<html><head><title>RobotCar</title><meta name='viewport' content='width=device-width'/>"); |
faker_71 | 0:2e9a86765692 | 113 | strcat(webbuff, "</head><body><center><p><strong>Robot Car Remot Controller"); |
faker_71 | 0:2e9a86765692 | 114 | strcat(webbuff, "</strong></p><td style='vertical-align:top;'><strong>Battery level "); |
faker_71 | 0:2e9a86765692 | 115 | strcat(webbuff, "<input type=\"text\" id=\"leftms\" size=4 value=250>%</strong>"); |
faker_71 | 0:2e9a86765692 | 116 | strcat(webbuff, "</td></p><tr ><strong>Now speed : </strong></tr><nobr id=\"speprint\">ready"); |
faker_71 | 0:2e9a86765692 | 117 | strcat(webbuff, "</nobr><br><tr ><strong>Now action : </strong></tr>"); |
faker_71 | 0:2e9a86765692 | 118 | strcat(webbuff, "<nobr id=\"funprint\">ready</nobr><table><tr><td></td><td>"); |
faker_71 | 0:2e9a86765692 | 119 | |
faker_71 | 0:2e9a86765692 | 120 | 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 | 121 | strcat(webbuff, "</button></td><td></td></tr><tr><td>"); |
faker_71 | 0:2e9a86765692 | 122 | 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 | 123 | strcat(webbuff, "</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 124 | |
faker_71 | 0:2e9a86765692 | 125 | 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 | 126 | strcat(webbuff, "</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 127 | 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 | 128 | strcat(webbuff, "</button></td></tr><td></td><td>"); |
faker_71 | 0:2e9a86765692 | 129 | 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 | 130 | strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>"); |
faker_71 | 0:2e9a86765692 | 131 | |
faker_71 | 0:2e9a86765692 | 132 | |
faker_71 | 0:2e9a86765692 | 133 | |
faker_71 | 0:2e9a86765692 | 134 | //begin |
faker_71 | 0:2e9a86765692 | 135 | strcat(webbuff, "<strong>Addition functions:</strong><table>"); |
faker_71 | 0:2e9a86765692 | 136 | strcat(webbuff, "<td>");//ok |
faker_71 | 0:2e9a86765692 | 137 | 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 | 138 | strcat(webbuff, "AVOIDANCE</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 139 | 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 | 140 | strcat(webbuff, "</button></td></table><table><td>"); |
faker_71 | 0:2e9a86765692 | 141 | strcat(webbuff, "</td><strong>Speed level:</strong>"); |
faker_71 | 0:2e9a86765692 | 142 | strcat(webbuff, "</table><table><td>"); |
faker_71 | 0:2e9a86765692 | 143 | 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 | 144 | strcat(webbuff, "</button></td><td>"); |
faker_71 | 0:2e9a86765692 | 145 | 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 | 146 | strcat(webbuff, "</button>"); |
faker_71 | 0:2e9a86765692 | 147 | strcat(webbuff, "</td>"); |
faker_71 | 0:2e9a86765692 | 148 | strcat(webbuff, "<td>"); |
faker_71 | 0:2e9a86765692 | 149 | 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 | 150 | strcat(webbuff, "</button>"); |
faker_71 | 0:2e9a86765692 | 151 | strcat(webbuff, "</td></table>"); |
faker_71 | 0:2e9a86765692 | 152 | strcat(webbuff, "<table>"); |
faker_71 | 0:2e9a86765692 | 153 | strcat(webbuff, "<td>"); |
faker_71 | 0:2e9a86765692 | 154 | strcat(webbuff, "</td>"); |
faker_71 | 0:2e9a86765692 | 155 | strcat(webbuff, "</table>"); //end |
faker_71 | 0:2e9a86765692 | 156 | |
faker_71 | 0:2e9a86765692 | 157 | |
faker_71 | 0:2e9a86765692 | 158 | |
faker_71 | 0:2e9a86765692 | 159 | strcat(webbuff, "</center>"); |
faker_71 | 0:2e9a86765692 | 160 | strcat(webbuff, "</body>"); |
faker_71 | 0:2e9a86765692 | 161 | strcat(webbuff, "</html>"); |
faker_71 | 0:2e9a86765692 | 162 | strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">"); |
faker_71 | 0:2e9a86765692 | 163 | strcat(webbuff, "function send_mes(btnmes,btnval){"); |
faker_71 | 0:2e9a86765692 | 164 | strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;"); |
faker_71 | 0:2e9a86765692 | 165 | strcat(webbuff, "htmlacs(url);"); |
faker_71 | 0:2e9a86765692 | 166 | strcat(webbuff, "console.log(url);"); |
faker_71 | 0:2e9a86765692 | 167 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 168 | strcat(webbuff, "function htmlacs(url) {"); |
faker_71 | 0:2e9a86765692 | 169 | strcat(webbuff, "var xhr = new XMLHttpRequest();"); |
faker_71 | 0:2e9a86765692 | 170 | strcat(webbuff, "xhr.open(\"GET\", url);"); |
faker_71 | 0:2e9a86765692 | 171 | strcat(webbuff, "xhr.send(\"\");"); |
faker_71 | 0:2e9a86765692 | 172 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 173 | strcat(webbuff, "function send_mes_spe(btnmes,btnval){"); |
faker_71 | 0:2e9a86765692 | 174 | strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;"); |
faker_71 | 0:2e9a86765692 | 175 | strcat(webbuff, "htmlacs(url);"); |
faker_71 | 0:2e9a86765692 | 176 | strcat(webbuff, "if(document.getElementById(\"speprint\")){"); |
faker_71 | 0:2e9a86765692 | 177 | strcat(webbuff, "document.getElementById(\"speprint\").innerHTML=btnval;"); |
faker_71 | 0:2e9a86765692 | 178 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 179 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 180 | strcat(webbuff, "function send_mes_fun(btnmes,btnval){"); |
faker_71 | 0:2e9a86765692 | 181 | strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;"); |
faker_71 | 0:2e9a86765692 | 182 | strcat(webbuff, "htmlacs(url);"); |
faker_71 | 0:2e9a86765692 | 183 | strcat(webbuff, "if(document.getElementById(\"funprint\")){"); |
faker_71 | 0:2e9a86765692 | 184 | strcat(webbuff, "document.getElementById(\"funprint\").innerHTML=btnval;"); |
faker_71 | 0:2e9a86765692 | 185 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 186 | strcat(webbuff, "}"); |
faker_71 | 0:2e9a86765692 | 187 | strcat(webbuff, "</script>"); |
faker_71 | 0:2e9a86765692 | 188 | // end of WEB page data |
faker_71 | 0:2e9a86765692 | 189 | bufl = strlen(webbuff); // get total page buffer length |
faker_71 | 0:2e9a86765692 | 190 | //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
faker_71 | 0:2e9a86765692 | 191 | |
faker_71 | 0:2e9a86765692 | 192 | 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 | 193 | timeout=500; |
faker_71 | 0:2e9a86765692 | 194 | getcount=40; |
faker_71 | 0:2e9a86765692 | 195 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 196 | getreply(); |
faker_71 | 0:2e9a86765692 | 197 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 198 | pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl)); |
faker_71 | 0:2e9a86765692 | 199 | |
faker_71 | 0:2e9a86765692 | 200 | pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl); |
faker_71 | 0:2e9a86765692 | 201 | |
faker_71 | 0:2e9a86765692 | 202 | //pastthrough mode |
faker_71 | 0:2e9a86765692 | 203 | SendWEB(); // send web page |
faker_71 | 0:2e9a86765692 | 204 | pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 205 | |
faker_71 | 0:2e9a86765692 | 206 | memset(webbuff, '\0', sizeof(webbuff)); |
faker_71 | 0:2e9a86765692 | 207 | sendcheck(); |
faker_71 | 0:2e9a86765692 | 208 | } |
faker_71 | 0:2e9a86765692 | 209 | |
faker_71 | 0:2e9a86765692 | 210 | // Large WEB buffer data send |
faker_71 | 0:2e9a86765692 | 211 | void SendWEB() |
faker_71 | 0:2e9a86765692 | 212 | { |
faker_71 | 0:2e9a86765692 | 213 | int i=0; |
faker_71 | 0:2e9a86765692 | 214 | if(esp.writeable()) { |
faker_71 | 0:2e9a86765692 | 215 | while(webbuff[i]!='\0') { |
faker_71 | 0:2e9a86765692 | 216 | esp.putc(webbuff[i]); |
faker_71 | 0:2e9a86765692 | 217 | |
faker_71 | 0:2e9a86765692 | 218 | //**** |
faker_71 | 0:2e9a86765692 | 219 | //output at command when 2000 |
faker_71 | 0:2e9a86765692 | 220 | if(((i%2047)==0) && (i>0)){ |
faker_71 | 0:2e9a86765692 | 221 | //wait_ms(10); |
faker_71 | 0:2e9a86765692 | 222 | 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 | 0:2e9a86765692 | 223 | pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048)); |
faker_71 | 0:2e9a86765692 | 224 | timeout=600; |
faker_71 | 0:2e9a86765692 | 225 | getcount=50; |
faker_71 | 0:2e9a86765692 | 226 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 227 | getreply(); |
faker_71 | 0:2e9a86765692 | 228 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 229 | pc.printf("\r\n+++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 230 | } |
faker_71 | 0:2e9a86765692 | 231 | //**** |
faker_71 | 0:2e9a86765692 | 232 | i++; |
faker_71 | 0:2e9a86765692 | 233 | pc.printf("%c",webbuff[i]); |
faker_71 | 0:2e9a86765692 | 234 | } |
faker_71 | 0:2e9a86765692 | 235 | } |
faker_71 | 0:2e9a86765692 | 236 | pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i); |
faker_71 | 0:2e9a86765692 | 237 | } |
faker_71 | 0:2e9a86765692 | 238 | |
faker_71 | 0:2e9a86765692 | 239 | |
faker_71 | 0:2e9a86765692 | 240 | |
faker_71 | 0:2e9a86765692 | 241 | void sendcheck() |
faker_71 | 0:2e9a86765692 | 242 | { |
faker_71 | 0:2e9a86765692 | 243 | weberror=1; |
faker_71 | 0:2e9a86765692 | 244 | timeout=500; |
faker_71 | 0:2e9a86765692 | 245 | getcount=24; |
faker_71 | 0:2e9a86765692 | 246 | t2.reset(); |
faker_71 | 0:2e9a86765692 | 247 | t2.start(); |
faker_71 | 0:2e9a86765692 | 248 | |
faker_71 | 0:2e9a86765692 | 249 | /* |
faker_71 | 0:2e9a86765692 | 250 | while(weberror==1 && t2.read() <5) { |
faker_71 | 0:2e9a86765692 | 251 | getreply(); |
faker_71 | 0:2e9a86765692 | 252 | if (strstr(replybuff, "SEND OK") != NULL) { |
faker_71 | 0:2e9a86765692 | 253 | weberror=0; // wait for valid SEND OK |
faker_71 | 0:2e9a86765692 | 254 | } |
faker_71 | 0:2e9a86765692 | 255 | } |
faker_71 | 0:2e9a86765692 | 256 | */ |
faker_71 | 0:2e9a86765692 | 257 | if(weberror==1) { // restart connection |
faker_71 | 0:2e9a86765692 | 258 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
faker_71 | 0:2e9a86765692 | 259 | timeout=500; |
faker_71 | 0:2e9a86765692 | 260 | getcount=10; |
faker_71 | 0:2e9a86765692 | 261 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 262 | getreply(); |
faker_71 | 0:2e9a86765692 | 263 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 264 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
faker_71 | 0:2e9a86765692 | 265 | timeout=500; |
faker_71 | 0:2e9a86765692 | 266 | getcount=10; |
faker_71 | 0:2e9a86765692 | 267 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 268 | getreply(); |
faker_71 | 0:2e9a86765692 | 269 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 270 | } else { |
faker_71 | 0:2e9a86765692 | 271 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
faker_71 | 0:2e9a86765692 | 272 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 273 | getreply(); |
faker_71 | 0:2e9a86765692 | 274 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 275 | } |
faker_71 | 0:2e9a86765692 | 276 | t2.reset(); |
faker_71 | 0:2e9a86765692 | 277 | } |
faker_71 | 0:2e9a86765692 | 278 | |
faker_71 | 0:2e9a86765692 | 279 | // Reads and processes GET and POST web data |
faker_71 | 0:2e9a86765692 | 280 | void ReadWebData() |
faker_71 | 0:2e9a86765692 | 281 | { |
faker_71 | 0:2e9a86765692 | 282 | pc.printf("+++++++++++++++++Read Web Data+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 283 | wait_ms(200); |
faker_71 | 0:2e9a86765692 | 284 | esp.attach(NULL); |
faker_71 | 0:2e9a86765692 | 285 | count_wifi=0; |
faker_71 | 0:2e9a86765692 | 286 | DataRX=0; |
faker_71 | 0:2e9a86765692 | 287 | weberror=0; |
faker_71 | 0:2e9a86765692 | 288 | memset(webdata, '\0', sizeof(webdata)); |
faker_71 | 0:2e9a86765692 | 289 | int x = strcspn (webbuff,"+"); |
faker_71 | 0:2e9a86765692 | 290 | if(x) { |
faker_71 | 0:2e9a86765692 | 291 | strcpy(webdata, webbuff + x); |
faker_71 | 0:2e9a86765692 | 292 | weberror=0; |
faker_71 | 0:2e9a86765692 | 293 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
faker_71 | 0:2e9a86765692 | 294 | //int i=0; |
faker_71 | 0:2e9a86765692 | 295 | pc.printf("+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 296 | pc.printf("%s",webdata); |
faker_71 | 0:2e9a86765692 | 297 | pc.printf("+++++++++++++++++succed rec end+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 298 | /* if( strstr(webdata, "LEFT") != NULL ) { |
faker_71 | 0:2e9a86765692 | 299 | click_flag = 0; |
faker_71 | 0:2e9a86765692 | 300 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 301 | pc.printf("mode = LEFT\r\n"); |
faker_71 | 0:2e9a86765692 | 302 | mode = LEFT; // 左折モード |
faker_71 | 0:2e9a86765692 | 303 | run = LEFT; // 左折 |
faker_71 | 0:2e9a86765692 | 304 | display(); |
faker_71 | 0:2e9a86765692 | 305 | } |
faker_71 | 0:2e9a86765692 | 306 | |
faker_71 | 0:2e9a86765692 | 307 | if( strstr(webdata, "RIGHT") != NULL ) { |
faker_71 | 0:2e9a86765692 | 308 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 309 | pc.printf("mode = RIGHT\r\n"); |
faker_71 | 0:2e9a86765692 | 310 | mode = RIGHT; // 右折モード |
faker_71 | 0:2e9a86765692 | 311 | run = RIGHT; // 右折 |
faker_71 | 0:2e9a86765692 | 312 | display(); // ディスプレイ表示 |
faker_71 | 0:2e9a86765692 | 313 | } |
faker_71 | 0:2e9a86765692 | 314 | |
faker_71 | 0:2e9a86765692 | 315 | if( strstr(webdata, "GO") != NULL ) { |
faker_71 | 0:2e9a86765692 | 316 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 317 | pc.printf("mode = ADVANCE\r\n"); |
faker_71 | 0:2e9a86765692 | 318 | mode = ADVANCE; // 前進モード |
faker_71 | 0:2e9a86765692 | 319 | run = ADVANCE; // 前進 |
faker_71 | 0:2e9a86765692 | 320 | display(); // ディスプレイ表示 |
faker_71 | 0:2e9a86765692 | 321 | } |
faker_71 | 0:2e9a86765692 | 322 | |
faker_71 | 0:2e9a86765692 | 323 | if( strstr(webdata, "BACK") != NULL ) { |
faker_71 | 0:2e9a86765692 | 324 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 325 | pc.printf("mode = BACK\r\n"); |
faker_71 | 0:2e9a86765692 | 326 | mode = BACK; // 後退モード |
faker_71 | 0:2e9a86765692 | 327 | run = BACK; // 後退 |
faker_71 | 0:2e9a86765692 | 328 | display(); // ディスプレイ表示 |
faker_71 | 0:2e9a86765692 | 329 | } |
faker_71 | 0:2e9a86765692 | 330 | |
faker_71 | 0:2e9a86765692 | 331 | if( strstr(webdata, "STOP") != NULL ) { |
faker_71 | 0:2e9a86765692 | 332 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 333 | pc.printf("mode = STOP\r\n"); |
faker_71 | 0:2e9a86765692 | 334 | mode = STOP; // 停止モード |
faker_71 | 0:2e9a86765692 | 335 | run = STOP; // 停止 |
faker_71 | 0:2e9a86765692 | 336 | display(); // ディスプレイ表示 |
faker_71 | 0:2e9a86765692 | 337 | } |
faker_71 | 0:2e9a86765692 | 338 | |
faker_71 | 0:2e9a86765692 | 339 | if( strstr(webdata, "AVOIDANCE") != NULL ) { |
faker_71 | 0:2e9a86765692 | 340 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 341 | pc.printf("mode = AVOIDANCE\r\n"); |
faker_71 | 0:2e9a86765692 | 342 | mode=AVOIDANCE; // 障害物回避モード |
faker_71 | 0:2e9a86765692 | 343 | run = ADVANCE; // 前進 |
faker_71 | 0:2e9a86765692 | 344 | display(); |
faker_71 | 0:2e9a86765692 | 345 | } |
faker_71 | 0:2e9a86765692 | 346 | |
faker_71 | 0:2e9a86765692 | 347 | if( strstr(webdata, "LINETRACE") != NULL ) { |
faker_71 | 0:2e9a86765692 | 348 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 349 | pc.printf("mode = LINE_TRACE\r\n"); |
faker_71 | 0:2e9a86765692 | 350 | mode=LINE_TRACE; // ライントレースモード |
faker_71 | 0:2e9a86765692 | 351 | display(); |
faker_71 | 0:2e9a86765692 | 352 | } |
faker_71 | 0:2e9a86765692 | 353 | |
faker_71 | 0:2e9a86765692 | 354 | if( strstr(webdata, "SLOW") != NULL ) { |
faker_71 | 0:2e9a86765692 | 355 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 356 | pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 357 | } |
faker_71 | 0:2e9a86765692 | 358 | |
faker_71 | 0:2e9a86765692 | 359 | if( strstr(webdata, "FAST") != NULL ) { |
faker_71 | 0:2e9a86765692 | 360 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 361 | pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 362 | } |
faker_71 | 0:2e9a86765692 | 363 | |
faker_71 | 0:2e9a86765692 | 364 | if( strstr(webdata, "VERYFAST") != NULL ) { |
faker_71 | 0:2e9a86765692 | 365 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 366 | pc.printf("\r\n+++++++++++++++++bbutton right clicked+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 367 | }*/ |
faker_71 | 0:2e9a86765692 | 368 | sprintf(channel, "%d",linkID); |
faker_71 | 0:2e9a86765692 | 369 | if (strstr(webdata, "GET") != NULL) { |
faker_71 | 0:2e9a86765692 | 370 | servreq=1; |
faker_71 | 0:2e9a86765692 | 371 | pc.printf("\r\n+++++++++++++++++GET+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 372 | |
faker_71 | 0:2e9a86765692 | 373 | } |
faker_71 | 0:2e9a86765692 | 374 | if (strstr(webdata, "POST") != NULL) { |
faker_71 | 0:2e9a86765692 | 375 | servreq=1; |
faker_71 | 0:2e9a86765692 | 376 | pc.printf("\r\n+++++++++++++++++POST+++++++++++++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 377 | } |
faker_71 | 0:2e9a86765692 | 378 | webcounter++; |
faker_71 | 0:2e9a86765692 | 379 | sprintf(webcount, "%d",webcounter); |
faker_71 | 0:2e9a86765692 | 380 | } else { |
faker_71 | 0:2e9a86765692 | 381 | pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 382 | memset(webbuff, '\0', sizeof(webbuff)); |
faker_71 | 0:2e9a86765692 | 383 | esp.attach(&callback); |
faker_71 | 0:2e9a86765692 | 384 | weberror=1; |
faker_71 | 0:2e9a86765692 | 385 | } |
faker_71 | 0:2e9a86765692 | 386 | } |
faker_71 | 0:2e9a86765692 | 387 | // Starts and restarts webserver if errors detected. |
faker_71 | 0:2e9a86765692 | 388 | void startserver() |
faker_71 | 0:2e9a86765692 | 389 | { |
faker_71 | 0:2e9a86765692 | 390 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 391 | strcpy(cmdbuff,"AT+RST\r\n"); |
faker_71 | 0:2e9a86765692 | 392 | timeout=8000; |
faker_71 | 0:2e9a86765692 | 393 | getcount=1000; |
faker_71 | 0:2e9a86765692 | 394 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 395 | getreply(); |
faker_71 | 0:2e9a86765692 | 396 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 397 | pc.printf("%d",count_wifi); |
faker_71 | 0:2e9a86765692 | 398 | if (strstr(replybuff, "OK") != NULL) { |
faker_71 | 0:2e9a86765692 | 399 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 400 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
faker_71 | 0:2e9a86765692 | 401 | timeout=500; |
faker_71 | 0:2e9a86765692 | 402 | getcount=20; |
faker_71 | 0:2e9a86765692 | 403 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 404 | getreply(); |
faker_71 | 0:2e9a86765692 | 405 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 406 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
faker_71 | 0:2e9a86765692 | 407 | timeout=500; |
faker_71 | 0:2e9a86765692 | 408 | getcount=20; |
faker_71 | 0:2e9a86765692 | 409 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 410 | getreply(); |
faker_71 | 0:2e9a86765692 | 411 | pc.printf(replybuff); |
faker_71 | 0:2e9a86765692 | 412 | wait(1); |
faker_71 | 0:2e9a86765692 | 413 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
faker_71 | 0:2e9a86765692 | 414 | timeout=500; |
faker_71 | 0:2e9a86765692 | 415 | getcount=50; |
faker_71 | 0:2e9a86765692 | 416 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 417 | getreply(); |
faker_71 | 0:2e9a86765692 | 418 | pc.printf(replybuff); |
faker_71 | 1:a45cf46c1adc | 419 | //wait(5); |
faker_71 | 1:a45cf46c1adc | 420 | wait(1); |
faker_71 | 0:2e9a86765692 | 421 | pc.printf("\n Getting Server IP \r\n"); |
faker_71 | 0:2e9a86765692 | 422 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
faker_71 | 0:2e9a86765692 | 423 | timeout=2500; |
faker_71 | 0:2e9a86765692 | 424 | getcount=200; |
faker_71 | 0:2e9a86765692 | 425 | while(weberror==0) { |
faker_71 | 0:2e9a86765692 | 426 | SendCMD(); |
faker_71 | 0:2e9a86765692 | 427 | getreply(); |
faker_71 | 0:2e9a86765692 | 428 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
faker_71 | 0:2e9a86765692 | 429 | weberror=1; // wait for valid IP |
faker_71 | 0:2e9a86765692 | 430 | } |
faker_71 | 0:2e9a86765692 | 431 | } |
faker_71 | 0:2e9a86765692 | 432 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
faker_71 | 0:2e9a86765692 | 433 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
faker_71 | 0:2e9a86765692 | 434 | replybuff[strlen(replybuff)-1] = '\0'; |
faker_71 | 0:2e9a86765692 | 435 | //char* IP = replybuff + 5; |
faker_71 | 0:2e9a86765692 | 436 | sprintf(webdata,"%s", replybuff); |
faker_71 | 0:2e9a86765692 | 437 | pc.printf(webdata); |
faker_71 | 0:2e9a86765692 | 438 | led2=1; |
faker_71 | 0:2e9a86765692 | 439 | bufflen=200; |
faker_71 | 0:2e9a86765692 | 440 | //bufflen=100; |
faker_71 | 0:2e9a86765692 | 441 | count_wifi=0; |
faker_71 | 0:2e9a86765692 | 442 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
faker_71 | 0:2e9a86765692 | 443 | esp.attach(&callback); |
faker_71 | 0:2e9a86765692 | 444 | } else { |
faker_71 | 0:2e9a86765692 | 445 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
faker_71 | 0:2e9a86765692 | 446 | led1=1; |
faker_71 | 0:2e9a86765692 | 447 | led2=1; |
faker_71 | 0:2e9a86765692 | 448 | led3=1; |
faker_71 | 0:2e9a86765692 | 449 | led4=1; |
faker_71 | 0:2e9a86765692 | 450 | while(1) { |
faker_71 | 0:2e9a86765692 | 451 | led1=!led1; |
faker_71 | 0:2e9a86765692 | 452 | led2=!led2; |
faker_71 | 0:2e9a86765692 | 453 | led3=!led3; |
faker_71 | 0:2e9a86765692 | 454 | led4=!led4; |
faker_71 | 0:2e9a86765692 | 455 | wait(1); |
faker_71 | 0:2e9a86765692 | 456 | } |
faker_71 | 0:2e9a86765692 | 457 | } |
faker_71 | 0:2e9a86765692 | 458 | t2.reset(); |
faker_71 | 0:2e9a86765692 | 459 | t2.start(); |
faker_71 | 0:2e9a86765692 | 460 | } |
faker_71 | 0:2e9a86765692 | 461 | // ESP Command data send |
faker_71 | 0:2e9a86765692 | 462 | void SendCMD() |
faker_71 | 0:2e9a86765692 | 463 | { |
faker_71 | 0:2e9a86765692 | 464 | esp.printf("%s", cmdbuff); |
faker_71 | 0:2e9a86765692 | 465 | } |
faker_71 | 0:2e9a86765692 | 466 | // Get Command and ESP status replies |
faker_71 | 0:2e9a86765692 | 467 | void getreply() |
faker_71 | 0:2e9a86765692 | 468 | { |
faker_71 | 0:2e9a86765692 | 469 | memset(replybuff, '\0', sizeof(replybuff)); |
faker_71 | 0:2e9a86765692 | 470 | t1.reset(); |
faker_71 | 0:2e9a86765692 | 471 | t1.start(); |
faker_71 | 0:2e9a86765692 | 472 | replycount=0; |
faker_71 | 0:2e9a86765692 | 473 | while(t1.read_ms()< timeout && replycount < getcount) { |
faker_71 | 0:2e9a86765692 | 474 | if(esp.readable()) { |
faker_71 | 0:2e9a86765692 | 475 | replybuff[replycount] = esp.getc(); |
faker_71 | 0:2e9a86765692 | 476 | replycount++; |
faker_71 | 0:2e9a86765692 | 477 | } |
faker_71 | 0:2e9a86765692 | 478 | } |
faker_71 | 0:2e9a86765692 | 479 | t1.stop(); |
faker_71 | 0:2e9a86765692 | 480 | } |