Regenerating PPM signal based on distances from ultrasonic sensors, ESP8266 for connectin via wifi. Autonomous quadcopter behaviour, autonomou height holding. Flying direction based on front and back ultrasonic sensors.
Dependencies: ConfigFile HCSR04 PID PPM2 mbed-rtos mbed
ESP8266/Server.h@9:86a5af9935b1, 2017-10-27 (annotated)
- Committer:
- edy05
- Date:
- Fri Oct 27 09:09:24 2017 +0000
- Revision:
- 9:86a5af9935b1
- Parent:
- 2:d172c9963f87
- Child:
- 10:bb9c778f8e3e
Server - Reading PID values from configFile
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edy05 | 2:d172c9963f87 | 1 | #include "mbed.h" |
edy05 | 2:d172c9963f87 | 2 | #include "rtos.h" |
edy05 | 9:86a5af9935b1 | 3 | #include "hardware.h" |
edy05 | 2:d172c9963f87 | 4 | |
edy05 | 2:d172c9963f87 | 5 | |
edy05 | 2:d172c9963f87 | 6 | RawSerial esp(p9, p10); // tx, rx |
edy05 | 2:d172c9963f87 | 7 | DigitalOut reset(p8); |
edy05 | 2:d172c9963f87 | 8 | |
edy05 | 2:d172c9963f87 | 9 | // Standard Mbed LED definitions |
edy05 | 2:d172c9963f87 | 10 | DigitalOut led1(LED1); // (PTB18) |
edy05 | 2:d172c9963f87 | 11 | DigitalOut led3(LED3); // (PTD1) |
edy05 | 2:d172c9963f87 | 12 | |
edy05 | 2:d172c9963f87 | 13 | Timer t1; |
edy05 | 2:d172c9963f87 | 14 | Timer t2; |
edy05 | 2:d172c9963f87 | 15 | |
edy05 | 2:d172c9963f87 | 16 | char cmdbuff[32]; |
edy05 | 2:d172c9963f87 | 17 | char replybuff[1024]; |
edy05 | 2:d172c9963f87 | 18 | char webcount[8]; |
edy05 | 2:d172c9963f87 | 19 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
edy05 | 2:d172c9963f87 | 20 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
edy05 | 2:d172c9963f87 | 21 | char type[16]; |
edy05 | 2:d172c9963f87 | 22 | char channel[2]; |
edy05 | 2:d172c9963f87 | 23 | char ip_address[20]; |
edy05 | 2:d172c9963f87 | 24 | int bufflen, DataRX, count, getcount, replycount, servreq, timeout; |
edy05 | 2:d172c9963f87 | 25 | int bufl, ipdLen, linkID, weberror, webcounter; |
edy05 | 2:d172c9963f87 | 26 | |
edy05 | 2:d172c9963f87 | 27 | void getreply(); void SendCMD(); |
edy05 | 2:d172c9963f87 | 28 | void startserver(); |
edy05 | 2:d172c9963f87 | 29 | void ReadWebData(); |
edy05 | 2:d172c9963f87 | 30 | void sendpage(); |
edy05 | 2:d172c9963f87 | 31 | void sendcheck(); |
edy05 | 2:d172c9963f87 | 32 | void get_ip_address(char web_data[1024]); |
edy05 | 2:d172c9963f87 | 33 | void SendWEB(); |
edy05 | 2:d172c9963f87 | 34 | |
edy05 | 2:d172c9963f87 | 35 | void serverRun(); |
edy05 | 2:d172c9963f87 | 36 | void serverMain(); |
edy05 | 2:d172c9963f87 | 37 | void serverLoop(); |
edy05 | 2:d172c9963f87 | 38 | void callback(); |
edy05 | 2:d172c9963f87 | 39 | |
edy05 | 2:d172c9963f87 | 40 | int port =80; // set server port |
edy05 | 2:d172c9963f87 | 41 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
edy05 | 2:d172c9963f87 | 42 | |
edy05 | 9:86a5af9935b1 | 43 | |
edy05 | 2:d172c9963f87 | 44 | |
edy05 | 2:d172c9963f87 | 45 | //serverThread.start(serverRun); |
edy05 | 2:d172c9963f87 | 46 | |
edy05 | 2:d172c9963f87 | 47 | void callback(){ |
edy05 | 2:d172c9963f87 | 48 | //pc.printf("callback!!!!!!!!!!!!!!!!_________________________\n\r"); |
edy05 | 2:d172c9963f87 | 49 | led3 = 1; |
edy05 | 2:d172c9963f87 | 50 | while (esp.readable()) { |
edy05 | 2:d172c9963f87 | 51 | webbuff[count] = esp.getc(); |
edy05 | 2:d172c9963f87 | 52 | count++; |
edy05 | 2:d172c9963f87 | 53 | } |
edy05 | 2:d172c9963f87 | 54 | if(strlen(webbuff)>bufflen) { |
edy05 | 2:d172c9963f87 | 55 | DataRX=1; |
edy05 | 2:d172c9963f87 | 56 | led3=0; |
edy05 | 2:d172c9963f87 | 57 | } |
edy05 | 2:d172c9963f87 | 58 | } |
edy05 | 2:d172c9963f87 | 59 | |
edy05 | 2:d172c9963f87 | 60 | |
edy05 | 2:d172c9963f87 | 61 | void serverRun(){ |
edy05 | 2:d172c9963f87 | 62 | reset=0; |
edy05 | 2:d172c9963f87 | 63 | pc.baud(115200); |
edy05 | 2:d172c9963f87 | 64 | |
edy05 | 2:d172c9963f87 | 65 | pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r"); |
edy05 | 2:d172c9963f87 | 66 | wait(0.5); |
edy05 | 2:d172c9963f87 | 67 | reset=1; |
edy05 | 2:d172c9963f87 | 68 | timeout=6000; |
edy05 | 2:d172c9963f87 | 69 | getcount=500; |
edy05 | 2:d172c9963f87 | 70 | getreply(); |
edy05 | 2:d172c9963f87 | 71 | esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
edy05 | 2:d172c9963f87 | 72 | startserver(); |
edy05 | 9:86a5af9935b1 | 73 | loadConfigFile(); |
edy05 | 2:d172c9963f87 | 74 | while(1){ |
edy05 | 2:d172c9963f87 | 75 | if(DataRX==1) { |
edy05 | 2:d172c9963f87 | 76 | ReadWebData(); |
edy05 | 2:d172c9963f87 | 77 | if (servreq == 1 && weberror == 0) { |
edy05 | 2:d172c9963f87 | 78 | sendpage(); |
edy05 | 2:d172c9963f87 | 79 | } |
edy05 | 2:d172c9963f87 | 80 | esp.attach(&callback); |
edy05 | 2:d172c9963f87 | 81 | 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); |
edy05 | 2:d172c9963f87 | 82 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
edy05 | 2:d172c9963f87 | 83 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
edy05 | 2:d172c9963f87 | 84 | pc.printf(" -------------------------------------\n\n"); |
edy05 | 2:d172c9963f87 | 85 | servreq=0; |
edy05 | 2:d172c9963f87 | 86 | } |
edy05 | 2:d172c9963f87 | 87 | Thread::wait(1000); |
edy05 | 2:d172c9963f87 | 88 | } |
edy05 | 2:d172c9963f87 | 89 | } |
edy05 | 2:d172c9963f87 | 90 | |
edy05 | 2:d172c9963f87 | 91 | |
edy05 | 2:d172c9963f87 | 92 | // Static WEB page |
edy05 | 2:d172c9963f87 | 93 | void sendpage(){ |
edy05 | 2:d172c9963f87 | 94 | strcpy(webbuff, "<!DOCTYPE html>"); |
edy05 | 2:d172c9963f87 | 95 | strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>"); |
edy05 | 2:d172c9963f87 | 96 | strcat(webbuff, "<body>"); |
edy05 | 2:d172c9963f87 | 97 | strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web PID Controller</h1>"); |
edy05 | 2:d172c9963f87 | 98 | strcat(webbuff, "Hit Count - "); |
edy05 | 2:d172c9963f87 | 99 | strcat(webbuff, webcount); |
edy05 | 2:d172c9963f87 | 100 | strcat(webbuff, "</div><br /><hr>"); |
edy05 | 2:d172c9963f87 | 101 | strcat(webbuff, "<p><form method=\"POST\">"); |
edy05 | 2:d172c9963f87 | 102 | if(led1==0) { |
edy05 | 2:d172c9963f87 | 103 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> LED 1 off"); |
edy05 | 2:d172c9963f87 | 104 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > LED 1 on<br>"); |
edy05 | 2:d172c9963f87 | 105 | } else { |
edy05 | 2:d172c9963f87 | 106 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > LED 1 off"); |
edy05 | 2:d172c9963f87 | 107 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> LED 1 on<br>"); |
edy05 | 2:d172c9963f87 | 108 | } |
edy05 | 2:d172c9963f87 | 109 | strcat(webbuff, "P: <input type=\"text\" name=\"proportional\" size=6 value=\""); |
edy05 | 9:86a5af9935b1 | 110 | ConvertToCharArray(_P); |
edy05 | 9:86a5af9935b1 | 111 | strcat(webbuff, _str); |
edy05 | 2:d172c9963f87 | 112 | strcat(webbuff, "\"><br>"); |
edy05 | 2:d172c9963f87 | 113 | strcat(webbuff, "I: <input type=\"text\" name=\"integral\" size=6 value=\""); |
edy05 | 9:86a5af9935b1 | 114 | ConvertToCharArray(_I); |
edy05 | 9:86a5af9935b1 | 115 | strcat(webbuff, _str); |
edy05 | 2:d172c9963f87 | 116 | strcat(webbuff, "\"><br>"); |
edy05 | 2:d172c9963f87 | 117 | strcat(webbuff, "D: <input type=\"text\" name=\"derivative\" size=6 value=\""); |
edy05 | 9:86a5af9935b1 | 118 | ConvertToCharArray(_D); |
edy05 | 9:86a5af9935b1 | 119 | strcat(webbuff, _str); |
edy05 | 2:d172c9963f87 | 120 | strcat(webbuff, "\"><br>"); |
edy05 | 2:d172c9963f87 | 121 | strcat(webbuff, "<p><input type=\"radio\" name=\"nothing\" value=\"1\" checked>"); |
edy05 | 2:d172c9963f87 | 122 | strcat(webbuff, "<p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;"); |
edy05 | 2:d172c9963f87 | 123 | strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); |
edy05 | 2:d172c9963f87 | 124 | strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); |
edy05 | 2:d172c9963f87 | 125 | strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:"); |
edy05 | 2:d172c9963f87 | 126 | strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
edy05 | 2:d172c9963f87 | 127 | strcat(webbuff, "background: #3cb0fd;"); |
edy05 | 2:d172c9963f87 | 128 | strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); |
edy05 | 2:d172c9963f87 | 129 | strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); |
edy05 | 2:d172c9963f87 | 130 | strcat(webbuff, "text-decoration:none;\"></form>"); |
edy05 | 2:d172c9963f87 | 131 | strcat(webbuff, "</body></html>"); |
edy05 | 2:d172c9963f87 | 132 | // end of WEB page data |
edy05 | 2:d172c9963f87 | 133 | bufl = strlen(webbuff); // get total page buffer length |
edy05 | 2:d172c9963f87 | 134 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
edy05 | 2:d172c9963f87 | 135 | timeout=200; |
edy05 | 2:d172c9963f87 | 136 | getcount=7; |
edy05 | 2:d172c9963f87 | 137 | SendCMD(); |
edy05 | 2:d172c9963f87 | 138 | getreply(); |
edy05 | 2:d172c9963f87 | 139 | SendWEB(); // send web page |
edy05 | 2:d172c9963f87 | 140 | memset(webbuff, '\0', sizeof(webbuff)); |
edy05 | 2:d172c9963f87 | 141 | sendcheck(); |
edy05 | 2:d172c9963f87 | 142 | |
edy05 | 2:d172c9963f87 | 143 | |
edy05 | 2:d172c9963f87 | 144 | } |
edy05 | 2:d172c9963f87 | 145 | |
edy05 | 2:d172c9963f87 | 146 | void sendcheck(){ |
edy05 | 2:d172c9963f87 | 147 | weberror=1; |
edy05 | 2:d172c9963f87 | 148 | timeout=500; |
edy05 | 2:d172c9963f87 | 149 | getcount=24; |
edy05 | 2:d172c9963f87 | 150 | t2.reset(); |
edy05 | 2:d172c9963f87 | 151 | t2.start(); |
edy05 | 2:d172c9963f87 | 152 | while(weberror==1 && t2.read() <5) { |
edy05 | 2:d172c9963f87 | 153 | getreply(); |
edy05 | 2:d172c9963f87 | 154 | if (strstr(replybuff, "SEND OK") != NULL) { |
edy05 | 2:d172c9963f87 | 155 | weberror=0; // wait for valid SEND OK |
edy05 | 2:d172c9963f87 | 156 | } |
edy05 | 2:d172c9963f87 | 157 | } |
edy05 | 2:d172c9963f87 | 158 | if(weberror==1) { // restart connection |
edy05 | 2:d172c9963f87 | 159 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
edy05 | 2:d172c9963f87 | 160 | timeout=500; |
edy05 | 2:d172c9963f87 | 161 | getcount=10; |
edy05 | 2:d172c9963f87 | 162 | SendCMD(); |
edy05 | 2:d172c9963f87 | 163 | getreply(); |
edy05 | 2:d172c9963f87 | 164 | pc.printf(replybuff); |
edy05 | 2:d172c9963f87 | 165 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
edy05 | 2:d172c9963f87 | 166 | timeout=500; |
edy05 | 2:d172c9963f87 | 167 | getcount=10; |
edy05 | 2:d172c9963f87 | 168 | SendCMD(); |
edy05 | 2:d172c9963f87 | 169 | getreply(); |
edy05 | 2:d172c9963f87 | 170 | pc.printf(replybuff); |
edy05 | 2:d172c9963f87 | 171 | } else { |
edy05 | 2:d172c9963f87 | 172 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
edy05 | 2:d172c9963f87 | 173 | SendCMD(); |
edy05 | 2:d172c9963f87 | 174 | getreply(); |
edy05 | 2:d172c9963f87 | 175 | pc.printf(replybuff); |
edy05 | 2:d172c9963f87 | 176 | } |
edy05 | 2:d172c9963f87 | 177 | t2.reset(); |
edy05 | 2:d172c9963f87 | 178 | } |
edy05 | 2:d172c9963f87 | 179 | |
edy05 | 2:d172c9963f87 | 180 | // Large WEB buffer data send |
edy05 | 2:d172c9963f87 | 181 | void SendWEB() |
edy05 | 2:d172c9963f87 | 182 | { |
edy05 | 2:d172c9963f87 | 183 | int i=0; |
edy05 | 2:d172c9963f87 | 184 | if(esp.writeable()) { |
edy05 | 2:d172c9963f87 | 185 | while(webbuff[i]!='\0') { |
edy05 | 2:d172c9963f87 | 186 | esp.putc(webbuff[i]); |
edy05 | 2:d172c9963f87 | 187 | i++; |
edy05 | 2:d172c9963f87 | 188 | } |
edy05 | 2:d172c9963f87 | 189 | } |
edy05 | 2:d172c9963f87 | 190 | } |
edy05 | 2:d172c9963f87 | 191 | |
edy05 | 2:d172c9963f87 | 192 | // Reads and processes GET and POST web data |
edy05 | 2:d172c9963f87 | 193 | void ReadWebData(){ |
edy05 | 2:d172c9963f87 | 194 | wait_ms(200); |
edy05 | 2:d172c9963f87 | 195 | esp.attach(NULL); |
edy05 | 2:d172c9963f87 | 196 | count=0; |
edy05 | 2:d172c9963f87 | 197 | DataRX=0; |
edy05 | 2:d172c9963f87 | 198 | weberror=0; |
edy05 | 2:d172c9963f87 | 199 | memset(webdata, '\0', sizeof(webdata)); |
edy05 | 2:d172c9963f87 | 200 | int x = strcspn (webbuff,"+"); |
edy05 | 2:d172c9963f87 | 201 | if(x) { |
edy05 | 2:d172c9963f87 | 202 | strcpy(webdata, webbuff + x); |
edy05 | 2:d172c9963f87 | 203 | pc.printf("webdata received: %s", webdata); |
edy05 | 2:d172c9963f87 | 204 | weberror=0; |
edy05 | 2:d172c9963f87 | 205 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
edy05 | 2:d172c9963f87 | 206 | if (strstr(webdata, "led1=1") != NULL ) { |
edy05 | 2:d172c9963f87 | 207 | led1=1; |
edy05 | 2:d172c9963f87 | 208 | } |
edy05 | 2:d172c9963f87 | 209 | if (strstr(webdata, "led1=0") != NULL ) { |
edy05 | 2:d172c9963f87 | 210 | led1=0; |
edy05 | 2:d172c9963f87 | 211 | } |
edy05 | 2:d172c9963f87 | 212 | if (strstr(webdata, "proportional") != NULL ){ |
edy05 | 2:d172c9963f87 | 213 | pc.printf("\n\r looking for data \n\r"); |
edy05 | 2:d172c9963f87 | 214 | char* p_webdata = strstr(webdata, "proportional"); |
edy05 | 2:d172c9963f87 | 215 | p_webdata = p_webdata + strlen("proportional") + 1; |
edy05 | 2:d172c9963f87 | 216 | int i = 0; |
edy05 | 2:d172c9963f87 | 217 | while(*p_webdata != '&'){ |
edy05 | 2:d172c9963f87 | 218 | pc.printf("%c", *p_webdata); |
edy05 | 9:86a5af9935b1 | 219 | _str[i] = *p_webdata; |
edy05 | 2:d172c9963f87 | 220 | p_webdata += 1; |
edy05 | 2:d172c9963f87 | 221 | i++; |
edy05 | 2:d172c9963f87 | 222 | } |
edy05 | 2:d172c9963f87 | 223 | pc.printf("\n\r"); |
edy05 | 9:86a5af9935b1 | 224 | _str[i] = '\0'; |
edy05 | 9:86a5af9935b1 | 225 | pc.printf("proportional: %s", _str); |
edy05 | 9:86a5af9935b1 | 226 | _P = atof(_str); |
edy05 | 2:d172c9963f87 | 227 | pc.printf("\n\r end of looking for data \n\r"); |
edy05 | 2:d172c9963f87 | 228 | } |
edy05 | 2:d172c9963f87 | 229 | if (strstr(webdata, "integral") != NULL){ |
edy05 | 2:d172c9963f87 | 230 | pc.printf("\n\r looking for data \n\r"); |
edy05 | 2:d172c9963f87 | 231 | char* p_webdata = strstr(webdata, "integral"); |
edy05 | 2:d172c9963f87 | 232 | p_webdata = p_webdata + strlen("integral") + 1; |
edy05 | 2:d172c9963f87 | 233 | int i = 0; |
edy05 | 2:d172c9963f87 | 234 | while(*p_webdata != '&'){ |
edy05 | 2:d172c9963f87 | 235 | pc.printf("%c", *p_webdata); |
edy05 | 9:86a5af9935b1 | 236 | _str[i] = *p_webdata; |
edy05 | 2:d172c9963f87 | 237 | p_webdata += 1; |
edy05 | 2:d172c9963f87 | 238 | i++; |
edy05 | 2:d172c9963f87 | 239 | } |
edy05 | 2:d172c9963f87 | 240 | pc.printf("\n\r"); |
edy05 | 9:86a5af9935b1 | 241 | _str[i] = '\0'; |
edy05 | 9:86a5af9935b1 | 242 | pc.printf("integral: %s", _str); |
edy05 | 9:86a5af9935b1 | 243 | _I = atof(_str); |
edy05 | 2:d172c9963f87 | 244 | pc.printf("\n\r end of looking for data \n\r"); |
edy05 | 2:d172c9963f87 | 245 | |
edy05 | 2:d172c9963f87 | 246 | } |
edy05 | 2:d172c9963f87 | 247 | if (strstr(webdata, "derivative") != NULL){ |
edy05 | 2:d172c9963f87 | 248 | pc.printf("\n\r looking for data \n\r"); |
edy05 | 2:d172c9963f87 | 249 | char* p_webdata = strstr(webdata, "derivative"); |
edy05 | 2:d172c9963f87 | 250 | p_webdata = p_webdata + strlen("derivative") + 1; |
edy05 | 2:d172c9963f87 | 251 | int i = 0; |
edy05 | 2:d172c9963f87 | 252 | while(*p_webdata != '&'){ |
edy05 | 2:d172c9963f87 | 253 | pc.printf("%c", *p_webdata); |
edy05 | 9:86a5af9935b1 | 254 | _str[i] = *p_webdata; |
edy05 | 2:d172c9963f87 | 255 | p_webdata += 1; |
edy05 | 2:d172c9963f87 | 256 | i++; |
edy05 | 2:d172c9963f87 | 257 | } |
edy05 | 2:d172c9963f87 | 258 | pc.printf("\n\r"); |
edy05 | 9:86a5af9935b1 | 259 | _str[i] = '\0'; |
edy05 | 9:86a5af9935b1 | 260 | pc.printf("derivative: %s", _str); |
edy05 | 9:86a5af9935b1 | 261 | _D = atof(_str); |
edy05 | 2:d172c9963f87 | 262 | pc.printf("\n\r end of looking for data \n\r"); |
edy05 | 2:d172c9963f87 | 263 | } |
edy05 | 2:d172c9963f87 | 264 | sprintf(channel, "%d",linkID); |
edy05 | 2:d172c9963f87 | 265 | if (strstr(webdata, "GET") != NULL) { |
edy05 | 2:d172c9963f87 | 266 | servreq=1; |
edy05 | 2:d172c9963f87 | 267 | } |
edy05 | 2:d172c9963f87 | 268 | if (strstr(webdata, "POST") != NULL) { |
edy05 | 2:d172c9963f87 | 269 | servreq=1; |
edy05 | 2:d172c9963f87 | 270 | } |
edy05 | 2:d172c9963f87 | 271 | webcounter++; |
edy05 | 2:d172c9963f87 | 272 | sprintf(webcount, "%d",webcounter); |
edy05 | 9:86a5af9935b1 | 273 | // Save data to configFile |
edy05 | 2:d172c9963f87 | 274 | }else{ |
edy05 | 2:d172c9963f87 | 275 | memset(webbuff, '\0', sizeof(webbuff)); |
edy05 | 2:d172c9963f87 | 276 | esp.attach(&callback); |
edy05 | 2:d172c9963f87 | 277 | weberror=1; |
edy05 | 2:d172c9963f87 | 278 | } |
edy05 | 2:d172c9963f87 | 279 | |
edy05 | 2:d172c9963f87 | 280 | } |
edy05 | 2:d172c9963f87 | 281 | |
edy05 | 2:d172c9963f87 | 282 | void startserver(){ |
edy05 | 2:d172c9963f87 | 283 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
edy05 | 2:d172c9963f87 | 284 | strcpy(cmdbuff,"AT+RST\r\n"); |
edy05 | 2:d172c9963f87 | 285 | timeout=8000; |
edy05 | 2:d172c9963f87 | 286 | getcount=1000; |
edy05 | 2:d172c9963f87 | 287 | SendCMD(); |
edy05 | 2:d172c9963f87 | 288 | getreply(); |
edy05 | 2:d172c9963f87 | 289 | pc.printf(replybuff); |
edy05 | 2:d172c9963f87 | 290 | pc.printf("%d",count); |
edy05 | 2:d172c9963f87 | 291 | if (strstr(replybuff, "OK") != NULL) { |
edy05 | 2:d172c9963f87 | 292 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
edy05 | 2:d172c9963f87 | 293 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
edy05 | 2:d172c9963f87 | 294 | timeout=500; |
edy05 | 2:d172c9963f87 | 295 | getcount=20; |
edy05 | 2:d172c9963f87 | 296 | SendCMD(); |
edy05 | 2:d172c9963f87 | 297 | getreply(); |
edy05 | 2:d172c9963f87 | 298 | pc.printf(replybuff); |
edy05 | 2:d172c9963f87 | 299 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
edy05 | 2:d172c9963f87 | 300 | timeout=500; |
edy05 | 2:d172c9963f87 | 301 | getcount=20; |
edy05 | 2:d172c9963f87 | 302 | SendCMD(); |
edy05 | 2:d172c9963f87 | 303 | getreply(); |
edy05 | 2:d172c9963f87 | 304 | pc.printf(replybuff); |
edy05 | 2:d172c9963f87 | 305 | wait(1); |
edy05 | 2:d172c9963f87 | 306 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
edy05 | 2:d172c9963f87 | 307 | timeout=500; |
edy05 | 2:d172c9963f87 | 308 | getcount=50; |
edy05 | 2:d172c9963f87 | 309 | SendCMD(); |
edy05 | 2:d172c9963f87 | 310 | getreply(); |
edy05 | 2:d172c9963f87 | 311 | pc.printf(replybuff); |
edy05 | 2:d172c9963f87 | 312 | wait(5); |
edy05 | 2:d172c9963f87 | 313 | pc.printf("\n Getting Server IP \r\n"); |
edy05 | 2:d172c9963f87 | 314 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
edy05 | 2:d172c9963f87 | 315 | timeout=2500; |
edy05 | 2:d172c9963f87 | 316 | getcount=200; |
edy05 | 2:d172c9963f87 | 317 | while(weberror==0) { |
edy05 | 2:d172c9963f87 | 318 | SendCMD(); |
edy05 | 2:d172c9963f87 | 319 | getreply(); |
edy05 | 2:d172c9963f87 | 320 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
edy05 | 2:d172c9963f87 | 321 | weberror=1; // wait for valid IP |
edy05 | 2:d172c9963f87 | 322 | } |
edy05 | 2:d172c9963f87 | 323 | } |
edy05 | 2:d172c9963f87 | 324 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
edy05 | 2:d172c9963f87 | 325 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
edy05 | 2:d172c9963f87 | 326 | replybuff[strlen(replybuff)-1] = '\0'; |
edy05 | 2:d172c9963f87 | 327 | //char* IP = replybuff + 5; |
edy05 | 2:d172c9963f87 | 328 | sprintf(webdata,"%s", replybuff); |
edy05 | 2:d172c9963f87 | 329 | //SAVE IP ADDRESS |
edy05 | 2:d172c9963f87 | 330 | get_ip_address(webdata); |
edy05 | 2:d172c9963f87 | 331 | pc.printf("ip_address: %s\n\r",ip_address); |
edy05 | 2:d172c9963f87 | 332 | pc.printf(webdata); |
edy05 | 2:d172c9963f87 | 333 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
edy05 | 2:d172c9963f87 | 334 | esp.attach(&callback); |
edy05 | 2:d172c9963f87 | 335 | } else { |
edy05 | 2:d172c9963f87 | 336 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
edy05 | 2:d172c9963f87 | 337 | while(1) {} |
edy05 | 2:d172c9963f87 | 338 | } |
edy05 | 2:d172c9963f87 | 339 | t2.reset(); |
edy05 | 2:d172c9963f87 | 340 | t2.start(); |
edy05 | 2:d172c9963f87 | 341 | } |
edy05 | 2:d172c9963f87 | 342 | |
edy05 | 2:d172c9963f87 | 343 | void SendCMD() |
edy05 | 2:d172c9963f87 | 344 | { |
edy05 | 2:d172c9963f87 | 345 | esp.printf("%s", cmdbuff); |
edy05 | 2:d172c9963f87 | 346 | } |
edy05 | 2:d172c9963f87 | 347 | |
edy05 | 2:d172c9963f87 | 348 | void getreply() |
edy05 | 2:d172c9963f87 | 349 | { |
edy05 | 2:d172c9963f87 | 350 | memset(replybuff, '\0', sizeof(replybuff)); |
edy05 | 2:d172c9963f87 | 351 | t1.reset(); |
edy05 | 2:d172c9963f87 | 352 | t1.start(); |
edy05 | 2:d172c9963f87 | 353 | replycount=0; |
edy05 | 2:d172c9963f87 | 354 | while(t1.read_ms()< timeout && replycount < getcount) { |
edy05 | 2:d172c9963f87 | 355 | if(esp.readable()) { |
edy05 | 2:d172c9963f87 | 356 | replybuff[replycount] = esp.getc(); |
edy05 | 2:d172c9963f87 | 357 | replycount++; |
edy05 | 2:d172c9963f87 | 358 | } |
edy05 | 2:d172c9963f87 | 359 | } |
edy05 | 2:d172c9963f87 | 360 | t1.stop(); |
edy05 | 2:d172c9963f87 | 361 | } |
edy05 | 2:d172c9963f87 | 362 | |
edy05 | 2:d172c9963f87 | 363 | void get_ip_address(char web_data[1024]){ |
edy05 | 2:d172c9963f87 | 364 | char pre_text[] = "+CIFSR:STAIP,\""; |
edy05 | 2:d172c9963f87 | 365 | char* p_text = strstr(webdata, pre_text); |
edy05 | 2:d172c9963f87 | 366 | p_text = p_text+strlen(pre_text); |
edy05 | 2:d172c9963f87 | 367 | char* p_text2 = strstr(p_text, "\""); |
edy05 | 2:d172c9963f87 | 368 | for(int i = 0;;i++){ |
edy05 | 2:d172c9963f87 | 369 | if(*(p_text+i) == *p_text2){ |
edy05 | 2:d172c9963f87 | 370 | ip_address[i] = '\0'; |
edy05 | 2:d172c9963f87 | 371 | break; |
edy05 | 2:d172c9963f87 | 372 | } |
edy05 | 2:d172c9963f87 | 373 | ip_address[i] = *(p_text+i); |
edy05 | 2:d172c9963f87 | 374 | |
edy05 | 2:d172c9963f87 | 375 | } |
edy05 | 2:d172c9963f87 | 376 | } |