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

Committer:
edy05
Date:
Fri Oct 27 15:29:51 2017 +0000
Branch:
DistanceRegulation
Revision:
15:9cdf757269fb
Parent:
14:076ef843e1ba
Child:
21:0afb91824792
Addes _serverMessage for reporting if some value was not save correctly to config file

Who changed what in which revision?

UserRevisionLine numberNew 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 11:002927b2675d 77 // Save data to configFile
edy05 11:002927b2675d 78 writeSettingsToConfig();
edy05 2:d172c9963f87 79 if (servreq == 1 && weberror == 0) {
edy05 10:bb9c778f8e3e 80 // send HTTP Response
edy05 2:d172c9963f87 81 sendpage();
edy05 2:d172c9963f87 82 }
edy05 2:d172c9963f87 83 esp.attach(&callback);
edy05 2:d172c9963f87 84 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 85 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
edy05 2:d172c9963f87 86 pc.printf(" Web Characters sent : %d\n\n", bufl);
edy05 2:d172c9963f87 87 pc.printf(" -------------------------------------\n\n");
edy05 2:d172c9963f87 88 servreq=0;
edy05 15:9cdf757269fb 89 memset(_serverMessage, '\0', sizeof(_serverMessage));
edy05 2:d172c9963f87 90 }
edy05 2:d172c9963f87 91 Thread::wait(1000);
edy05 2:d172c9963f87 92 }
edy05 2:d172c9963f87 93 }
edy05 2:d172c9963f87 94
edy05 2:d172c9963f87 95
edy05 2:d172c9963f87 96 // Static WEB page
edy05 2:d172c9963f87 97 void sendpage(){
edy05 2:d172c9963f87 98 strcpy(webbuff, "<!DOCTYPE html>");
edy05 2:d172c9963f87 99 strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>");
edy05 2:d172c9963f87 100 strcat(webbuff, "<body>");
edy05 2:d172c9963f87 101 strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web PID Controller</h1>");
edy05 2:d172c9963f87 102 strcat(webbuff, "Hit Count - ");
edy05 2:d172c9963f87 103 strcat(webbuff, webcount);
edy05 2:d172c9963f87 104 strcat(webbuff, "</div><br /><hr>");
edy05 15:9cdf757269fb 105 //server message (mostly errors)
edy05 15:9cdf757269fb 106 strcat(webbuff, "<div stye=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h3>Server messages</h3>");
edy05 15:9cdf757269fb 107 strcat(webbuff, _serverMessage);
edy05 15:9cdf757269fb 108 strcat(webbuff, "</div><br><hr>");
edy05 2:d172c9963f87 109 strcat(webbuff, "<p><form method=\"POST\">");
edy05 2:d172c9963f87 110 if(led1==0) {
edy05 2:d172c9963f87 111 strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> LED 1 off");
edy05 2:d172c9963f87 112 strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > LED 1 on<br>");
edy05 2:d172c9963f87 113 } else {
edy05 2:d172c9963f87 114 strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > LED 1 off");
edy05 2:d172c9963f87 115 strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> LED 1 on<br>");
edy05 2:d172c9963f87 116 }
edy05 2:d172c9963f87 117 strcat(webbuff, "P: <input type=\"text\" name=\"proportional\" size=6 value=\"");
edy05 9:86a5af9935b1 118 ConvertToCharArray(_P);
edy05 9:86a5af9935b1 119 strcat(webbuff, _str);
edy05 2:d172c9963f87 120 strcat(webbuff, "\"><br>");
edy05 2:d172c9963f87 121 strcat(webbuff, "I: <input type=\"text\" name=\"integral\" size=6 value=\"");
edy05 9:86a5af9935b1 122 ConvertToCharArray(_I);
edy05 9:86a5af9935b1 123 strcat(webbuff, _str);
edy05 2:d172c9963f87 124 strcat(webbuff, "\"><br>");
edy05 2:d172c9963f87 125 strcat(webbuff, "D: <input type=\"text\" name=\"derivative\" size=6 value=\"");
edy05 9:86a5af9935b1 126 ConvertToCharArray(_D);
edy05 9:86a5af9935b1 127 strcat(webbuff, _str);
edy05 2:d172c9963f87 128 strcat(webbuff, "\"><br>");
edy05 14:076ef843e1ba 129 //Ground set Point
edy05 14:076ef843e1ba 130 strcat(webbuff, "Ground setPoint: <input type=\"text\" name=\"groundSetPoint\" size=6 value=\"");
edy05 14:076ef843e1ba 131 ConvertToCharArray(_groundSetPoint);
edy05 14:076ef843e1ba 132 strcat(webbuff, _str);
edy05 14:076ef843e1ba 133 strcat(webbuff, "\"><br>");
edy05 2:d172c9963f87 134 strcat(webbuff, "<p><input type=\"radio\" name=\"nothing\" value=\"1\" checked>");
edy05 2:d172c9963f87 135 strcat(webbuff, "<p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");
edy05 2:d172c9963f87 136 strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");
edy05 2:d172c9963f87 137 strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);");
edy05 2:d172c9963f87 138 strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
edy05 2:d172c9963f87 139 strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;");
edy05 2:d172c9963f87 140 strcat(webbuff, "background: #3cb0fd;");
edy05 2:d172c9963f87 141 strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);");
edy05 2:d172c9963f87 142 strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);");
edy05 2:d172c9963f87 143 strcat(webbuff, "text-decoration:none;\"></form>");
edy05 2:d172c9963f87 144 strcat(webbuff, "</body></html>");
edy05 2:d172c9963f87 145 // end of WEB page data
edy05 2:d172c9963f87 146 bufl = strlen(webbuff); // get total page buffer length
edy05 2:d172c9963f87 147 sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
edy05 2:d172c9963f87 148 timeout=200;
edy05 2:d172c9963f87 149 getcount=7;
edy05 2:d172c9963f87 150 SendCMD();
edy05 2:d172c9963f87 151 getreply();
edy05 2:d172c9963f87 152 SendWEB(); // send web page
edy05 2:d172c9963f87 153 memset(webbuff, '\0', sizeof(webbuff));
edy05 2:d172c9963f87 154 sendcheck();
edy05 2:d172c9963f87 155
edy05 2:d172c9963f87 156
edy05 2:d172c9963f87 157 }
edy05 2:d172c9963f87 158
edy05 2:d172c9963f87 159 void sendcheck(){
edy05 2:d172c9963f87 160 weberror=1;
edy05 2:d172c9963f87 161 timeout=500;
edy05 2:d172c9963f87 162 getcount=24;
edy05 2:d172c9963f87 163 t2.reset();
edy05 2:d172c9963f87 164 t2.start();
edy05 2:d172c9963f87 165 while(weberror==1 && t2.read() <5) {
edy05 2:d172c9963f87 166 getreply();
edy05 2:d172c9963f87 167 if (strstr(replybuff, "SEND OK") != NULL) {
edy05 2:d172c9963f87 168 weberror=0; // wait for valid SEND OK
edy05 2:d172c9963f87 169 }
edy05 2:d172c9963f87 170 }
edy05 2:d172c9963f87 171 if(weberror==1) { // restart connection
edy05 2:d172c9963f87 172 strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
edy05 2:d172c9963f87 173 timeout=500;
edy05 2:d172c9963f87 174 getcount=10;
edy05 2:d172c9963f87 175 SendCMD();
edy05 2:d172c9963f87 176 getreply();
edy05 2:d172c9963f87 177 pc.printf(replybuff);
edy05 2:d172c9963f87 178 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
edy05 2:d172c9963f87 179 timeout=500;
edy05 2:d172c9963f87 180 getcount=10;
edy05 2:d172c9963f87 181 SendCMD();
edy05 2:d172c9963f87 182 getreply();
edy05 2:d172c9963f87 183 pc.printf(replybuff);
edy05 2:d172c9963f87 184 } else {
edy05 2:d172c9963f87 185 sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
edy05 2:d172c9963f87 186 SendCMD();
edy05 2:d172c9963f87 187 getreply();
edy05 2:d172c9963f87 188 pc.printf(replybuff);
edy05 2:d172c9963f87 189 }
edy05 2:d172c9963f87 190 t2.reset();
edy05 2:d172c9963f87 191 }
edy05 2:d172c9963f87 192
edy05 2:d172c9963f87 193 // Large WEB buffer data send
edy05 2:d172c9963f87 194 void SendWEB()
edy05 2:d172c9963f87 195 {
edy05 2:d172c9963f87 196 int i=0;
edy05 2:d172c9963f87 197 if(esp.writeable()) {
edy05 2:d172c9963f87 198 while(webbuff[i]!='\0') {
edy05 2:d172c9963f87 199 esp.putc(webbuff[i]);
edy05 2:d172c9963f87 200 i++;
edy05 2:d172c9963f87 201 }
edy05 2:d172c9963f87 202 }
edy05 2:d172c9963f87 203 }
edy05 2:d172c9963f87 204
edy05 2:d172c9963f87 205 // Reads and processes GET and POST web data
edy05 2:d172c9963f87 206 void ReadWebData(){
edy05 2:d172c9963f87 207 wait_ms(200);
edy05 2:d172c9963f87 208 esp.attach(NULL);
edy05 2:d172c9963f87 209 count=0;
edy05 2:d172c9963f87 210 DataRX=0;
edy05 2:d172c9963f87 211 weberror=0;
edy05 2:d172c9963f87 212 memset(webdata, '\0', sizeof(webdata));
edy05 2:d172c9963f87 213 int x = strcspn (webbuff,"+");
edy05 2:d172c9963f87 214 if(x) {
edy05 2:d172c9963f87 215 strcpy(webdata, webbuff + x);
edy05 2:d172c9963f87 216 pc.printf("webdata received: %s", webdata);
edy05 2:d172c9963f87 217 weberror=0;
edy05 2:d172c9963f87 218 int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
edy05 2:d172c9963f87 219 if (strstr(webdata, "led1=1") != NULL ) {
edy05 2:d172c9963f87 220 led1=1;
edy05 2:d172c9963f87 221 }
edy05 2:d172c9963f87 222 if (strstr(webdata, "led1=0") != NULL ) {
edy05 2:d172c9963f87 223 led1=0;
edy05 2:d172c9963f87 224 }
edy05 2:d172c9963f87 225 if (strstr(webdata, "proportional") != NULL ){
edy05 2:d172c9963f87 226 pc.printf("\n\r looking for data \n\r");
edy05 2:d172c9963f87 227 char* p_webdata = strstr(webdata, "proportional");
edy05 2:d172c9963f87 228 p_webdata = p_webdata + strlen("proportional") + 1;
edy05 2:d172c9963f87 229 int i = 0;
edy05 2:d172c9963f87 230 while(*p_webdata != '&'){
edy05 2:d172c9963f87 231 pc.printf("%c", *p_webdata);
edy05 9:86a5af9935b1 232 _str[i] = *p_webdata;
edy05 2:d172c9963f87 233 p_webdata += 1;
edy05 2:d172c9963f87 234 i++;
edy05 2:d172c9963f87 235 }
edy05 2:d172c9963f87 236 pc.printf("\n\r");
edy05 9:86a5af9935b1 237 _str[i] = '\0';
edy05 9:86a5af9935b1 238 pc.printf("proportional: %s", _str);
edy05 9:86a5af9935b1 239 _P = atof(_str);
edy05 2:d172c9963f87 240 pc.printf("\n\r end of looking for data \n\r");
edy05 2:d172c9963f87 241 }
edy05 2:d172c9963f87 242 if (strstr(webdata, "integral") != NULL){
edy05 2:d172c9963f87 243 pc.printf("\n\r looking for data \n\r");
edy05 2:d172c9963f87 244 char* p_webdata = strstr(webdata, "integral");
edy05 2:d172c9963f87 245 p_webdata = p_webdata + strlen("integral") + 1;
edy05 2:d172c9963f87 246 int i = 0;
edy05 2:d172c9963f87 247 while(*p_webdata != '&'){
edy05 2:d172c9963f87 248 pc.printf("%c", *p_webdata);
edy05 9:86a5af9935b1 249 _str[i] = *p_webdata;
edy05 2:d172c9963f87 250 p_webdata += 1;
edy05 2:d172c9963f87 251 i++;
edy05 2:d172c9963f87 252 }
edy05 2:d172c9963f87 253 pc.printf("\n\r");
edy05 9:86a5af9935b1 254 _str[i] = '\0';
edy05 9:86a5af9935b1 255 pc.printf("integral: %s", _str);
edy05 9:86a5af9935b1 256 _I = atof(_str);
edy05 2:d172c9963f87 257 pc.printf("\n\r end of looking for data \n\r");
edy05 2:d172c9963f87 258
edy05 2:d172c9963f87 259 }
edy05 2:d172c9963f87 260 if (strstr(webdata, "derivative") != NULL){
edy05 2:d172c9963f87 261 pc.printf("\n\r looking for data \n\r");
edy05 2:d172c9963f87 262 char* p_webdata = strstr(webdata, "derivative");
edy05 2:d172c9963f87 263 p_webdata = p_webdata + strlen("derivative") + 1;
edy05 2:d172c9963f87 264 int i = 0;
edy05 2:d172c9963f87 265 while(*p_webdata != '&'){
edy05 2:d172c9963f87 266 pc.printf("%c", *p_webdata);
edy05 9:86a5af9935b1 267 _str[i] = *p_webdata;
edy05 2:d172c9963f87 268 p_webdata += 1;
edy05 2:d172c9963f87 269 i++;
edy05 2:d172c9963f87 270 }
edy05 2:d172c9963f87 271 pc.printf("\n\r");
edy05 9:86a5af9935b1 272 _str[i] = '\0';
edy05 9:86a5af9935b1 273 pc.printf("derivative: %s", _str);
edy05 9:86a5af9935b1 274 _D = atof(_str);
edy05 2:d172c9963f87 275 pc.printf("\n\r end of looking for data \n\r");
edy05 2:d172c9963f87 276 }
edy05 14:076ef843e1ba 277 if (strstr(webdata, "groundSetPoint") != NULL){
edy05 14:076ef843e1ba 278 pc.printf("\n\r looking for data \n\r");
edy05 14:076ef843e1ba 279 char* p_webdata = strstr(webdata, "groundSetPoint");
edy05 14:076ef843e1ba 280 p_webdata = p_webdata + strlen("groundSetPoint") + 1;
edy05 14:076ef843e1ba 281 int i = 0;
edy05 14:076ef843e1ba 282 while(*p_webdata != '&'){
edy05 14:076ef843e1ba 283 pc.printf("%c", *p_webdata);
edy05 14:076ef843e1ba 284 _str[i] = *p_webdata;
edy05 14:076ef843e1ba 285 p_webdata += 1;
edy05 14:076ef843e1ba 286 i++;
edy05 14:076ef843e1ba 287 }
edy05 14:076ef843e1ba 288 pc.printf("\n\r");
edy05 14:076ef843e1ba 289 _str[i] = '\0';
edy05 14:076ef843e1ba 290 pc.printf("groundSetPoint: %s", _str);
edy05 14:076ef843e1ba 291 _groundSetPoint = atof(_str);
edy05 14:076ef843e1ba 292 pc.printf("\n\r end of looking for data \n\r");
edy05 14:076ef843e1ba 293 }
edy05 2:d172c9963f87 294 sprintf(channel, "%d",linkID);
edy05 2:d172c9963f87 295 if (strstr(webdata, "GET") != NULL) {
edy05 2:d172c9963f87 296 servreq=1;
edy05 2:d172c9963f87 297 }
edy05 2:d172c9963f87 298 if (strstr(webdata, "POST") != NULL) {
edy05 2:d172c9963f87 299 servreq=1;
edy05 2:d172c9963f87 300 }
edy05 2:d172c9963f87 301 webcounter++;
edy05 2:d172c9963f87 302 sprintf(webcount, "%d",webcounter);
edy05 2:d172c9963f87 303 }else{
edy05 2:d172c9963f87 304 memset(webbuff, '\0', sizeof(webbuff));
edy05 2:d172c9963f87 305 esp.attach(&callback);
edy05 2:d172c9963f87 306 weberror=1;
edy05 2:d172c9963f87 307 }
edy05 2:d172c9963f87 308
edy05 2:d172c9963f87 309 }
edy05 2:d172c9963f87 310
edy05 2:d172c9963f87 311 void startserver(){
edy05 2:d172c9963f87 312 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
edy05 2:d172c9963f87 313 strcpy(cmdbuff,"AT+RST\r\n");
edy05 2:d172c9963f87 314 timeout=8000;
edy05 2:d172c9963f87 315 getcount=1000;
edy05 2:d172c9963f87 316 SendCMD();
edy05 2:d172c9963f87 317 getreply();
edy05 2:d172c9963f87 318 pc.printf(replybuff);
edy05 2:d172c9963f87 319 pc.printf("%d",count);
edy05 2:d172c9963f87 320 if (strstr(replybuff, "OK") != NULL) {
edy05 2:d172c9963f87 321 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
edy05 2:d172c9963f87 322 strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections.
edy05 2:d172c9963f87 323 timeout=500;
edy05 2:d172c9963f87 324 getcount=20;
edy05 2:d172c9963f87 325 SendCMD();
edy05 2:d172c9963f87 326 getreply();
edy05 2:d172c9963f87 327 pc.printf(replybuff);
edy05 2:d172c9963f87 328 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
edy05 2:d172c9963f87 329 timeout=500;
edy05 2:d172c9963f87 330 getcount=20;
edy05 2:d172c9963f87 331 SendCMD();
edy05 2:d172c9963f87 332 getreply();
edy05 2:d172c9963f87 333 pc.printf(replybuff);
edy05 2:d172c9963f87 334 wait(1);
edy05 2:d172c9963f87 335 sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
edy05 2:d172c9963f87 336 timeout=500;
edy05 2:d172c9963f87 337 getcount=50;
edy05 2:d172c9963f87 338 SendCMD();
edy05 2:d172c9963f87 339 getreply();
edy05 2:d172c9963f87 340 pc.printf(replybuff);
edy05 2:d172c9963f87 341 wait(5);
edy05 2:d172c9963f87 342 pc.printf("\n Getting Server IP \r\n");
edy05 2:d172c9963f87 343 strcpy(cmdbuff, "AT+CIFSR\r\n");
edy05 2:d172c9963f87 344 timeout=2500;
edy05 2:d172c9963f87 345 getcount=200;
edy05 2:d172c9963f87 346 while(weberror==0) {
edy05 2:d172c9963f87 347 SendCMD();
edy05 2:d172c9963f87 348 getreply();
edy05 2:d172c9963f87 349 if (strstr(replybuff, "0.0.0.0") == NULL) {
edy05 2:d172c9963f87 350 weberror=1; // wait for valid IP
edy05 2:d172c9963f87 351 }
edy05 2:d172c9963f87 352 }
edy05 2:d172c9963f87 353 pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
edy05 2:d172c9963f87 354 pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
edy05 2:d172c9963f87 355 replybuff[strlen(replybuff)-1] = '\0';
edy05 2:d172c9963f87 356 //char* IP = replybuff + 5;
edy05 2:d172c9963f87 357 sprintf(webdata,"%s", replybuff);
edy05 2:d172c9963f87 358 //SAVE IP ADDRESS
edy05 2:d172c9963f87 359 get_ip_address(webdata);
edy05 2:d172c9963f87 360 pc.printf("ip_address: %s\n\r",ip_address);
edy05 2:d172c9963f87 361 pc.printf(webdata);
edy05 2:d172c9963f87 362 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
edy05 2:d172c9963f87 363 esp.attach(&callback);
edy05 2:d172c9963f87 364 } else {
edy05 2:d172c9963f87 365 pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
edy05 2:d172c9963f87 366 while(1) {}
edy05 2:d172c9963f87 367 }
edy05 2:d172c9963f87 368 t2.reset();
edy05 2:d172c9963f87 369 t2.start();
edy05 2:d172c9963f87 370 }
edy05 2:d172c9963f87 371
edy05 2:d172c9963f87 372 void SendCMD()
edy05 2:d172c9963f87 373 {
edy05 2:d172c9963f87 374 esp.printf("%s", cmdbuff);
edy05 2:d172c9963f87 375 }
edy05 2:d172c9963f87 376
edy05 2:d172c9963f87 377 void getreply()
edy05 2:d172c9963f87 378 {
edy05 2:d172c9963f87 379 memset(replybuff, '\0', sizeof(replybuff));
edy05 2:d172c9963f87 380 t1.reset();
edy05 2:d172c9963f87 381 t1.start();
edy05 2:d172c9963f87 382 replycount=0;
edy05 2:d172c9963f87 383 while(t1.read_ms()< timeout && replycount < getcount) {
edy05 2:d172c9963f87 384 if(esp.readable()) {
edy05 2:d172c9963f87 385 replybuff[replycount] = esp.getc();
edy05 2:d172c9963f87 386 replycount++;
edy05 2:d172c9963f87 387 }
edy05 2:d172c9963f87 388 }
edy05 2:d172c9963f87 389 t1.stop();
edy05 2:d172c9963f87 390 }
edy05 2:d172c9963f87 391
edy05 2:d172c9963f87 392 void get_ip_address(char web_data[1024]){
edy05 2:d172c9963f87 393 char pre_text[] = "+CIFSR:STAIP,\"";
edy05 2:d172c9963f87 394 char* p_text = strstr(webdata, pre_text);
edy05 2:d172c9963f87 395 p_text = p_text+strlen(pre_text);
edy05 2:d172c9963f87 396 char* p_text2 = strstr(p_text, "\"");
edy05 2:d172c9963f87 397 for(int i = 0;;i++){
edy05 2:d172c9963f87 398 if(*(p_text+i) == *p_text2){
edy05 2:d172c9963f87 399 ip_address[i] = '\0';
edy05 2:d172c9963f87 400 break;
edy05 2:d172c9963f87 401 }
edy05 2:d172c9963f87 402 ip_address[i] = *(p_text+i);
edy05 2:d172c9963f87 403
edy05 2:d172c9963f87 404 }
edy05 2:d172c9963f87 405 }