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 14:59:21 2017 +0000
Branch:
DistanceRegulation
Revision:
14:076ef843e1ba
Parent:
11:002927b2675d
Child:
15:9cdf757269fb
groundSetPoint added to server page and configFile

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