Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ATCmdParserTest_V1_0_F103
ESP8266Webserver.cpp
00001 #include "ESP8266Webserver.h" 00002 //#include "string" 00003 00004 ESP8266Webserver::ESP8266Webserver() 00005 { 00006 bool erg; 00007 //HAL_Delay(1000); 00008 hs_count=0; 00009 clientIdx=0; 00010 if (pDebug) printf("\nATCmdParser with ESP8266 example"); 00011 port=Port; 00012 00013 dbg=pDebug; 00014 _serial = new BufferedSerial(tx,rx, ESP8266_DEFAULT_BAUD_RATE); 00015 _parser = new ATCmdParser(_serial, "\r\n"); 00016 _parser->debug_on(true); 00017 00018 _parser->set_timeout(2000); 00019 00020 //AT 00021 if (pDebug) printf("\nATCmdParser: AT\r\n"); 00022 _parser->send("AT"); 00023 00024 if (_parser->recv("OK")) { 00025 if (pDebug) printf("\nAntw.: OK\r\n"); 00026 } else { 00027 printf("\nATCmdParser: AT OK failed\r\n"); 00028 } 00029 00030 if (station==true) 00031 { 00032 listAPs(); 00033 printf("%s",ipadr); 00034 } 00035 else { 00036 _parser->send("ATE0"); 00037 strcpy(ipadr,ip); 00038 00039 if (pDebug) printf("\nATCmdParser: //AT+CWMODE=2 \r\n"); 00040 00041 _parser->send("AT+CWMODE=2"); 00042 HAL_Delay(1000); 00043 00044 if (_parser->recv("OK")) { 00045 if (pDebug) printf("\nAntw.: OK\r\n"); 00046 } else { 00047 printf("\nATCmdParser: AT+CWMODE=2 OK failed\r\n"); 00048 } 00049 00050 //AT+CWMODE? 00051 if (pDebug) printf("\nATCmdParser: //AT+CWMODE? \r\n"); 00052 00053 _parser->send("AT+CWMODE?"); 00054 00055 if (_parser->recv("OK")) { 00056 if (pDebug) printf("\nAntw.: OK\r\n"); 00057 } else { 00058 printf("\nATCmdParser: AT+CWMODE? OK failed\r\n"); 00059 } 00060 00061 00062 //AT+CIPAP="192.168.5.1","192.168.5.1","255.255.255.0" 00063 if (pDebug) printf("\nATCmdParser: //AT+CIPAP=\"%s\",\"%s\",\"%s\" \r\n",ipad.c_str(),ipad.c_str(),netzmaske); 00064 do 00065 { 00066 _parser->send("AT+CIPAP=\"%s\",\"%s\",\"%s\"",ipad.c_str(),ipad.c_str(),netzmaske); 00067 erg=_parser->recv("OK"); 00068 if (erg==true) { 00069 if (pDebug) printf("\nAntw.: OK\r\n"); 00070 } else { 00071 printf("\nATCmdParser: AT+CIPAP OK failed\r\n"); 00072 HAL_Delay(1000); 00073 } 00074 } 00075 while (erg==false); 00076 00077 00078 00079 //AT+CIPMUX=1 00080 if (pDebug) printf("\nATCmdParser: //AT+CIPMUX=1"); 00081 _parser->send("AT+CIPMUX=1"); 00082 00083 if (_parser->recv("OK")) { 00084 if (pDebug) printf("\nAntw.: OK\r\n"); 00085 } else { 00086 printf("\nATCmdParser: AT+CIPMUX=1 OK failed\r\n"); 00087 } 00088 } 00089 } 00090 00091 int ESP8266Webserver::on(const char* handlestring,Callback< void()> func) 00092 { 00093 if (hs_count>9) return -1; 00094 else 00095 { 00096 strcpy(hs[hs_count],handlestring); 00097 if (dbg) printf("\r\n\r\n%s, %s\r\n",handlestring,hs[hs_count]); 00098 cbs[hs_count]=func; 00099 hs_count++; 00100 return 0; 00101 } 00102 00103 } 00104 00105 int ESP8266Webserver::begin(void) 00106 { 00107 //AT+CIPSERVER=1,80 00108 if (dbg) printf("\nATCmdParser: //AT+CIPSERVER=1,%d",Port); 00109 _parser->send("AT+CIPSERVER=1,%d",Port); 00110 if (_parser->recv("OK")) { 00111 if (dbg) printf("\nAntw.: OK\r\n"); 00112 return 0; 00113 } else { 00114 printf("\nATCmdParser: AT+CIPSERVER=1 OK failed\r\n"); 00115 return -1; 00116 } 00117 } 00118 00119 //durchsucht recbuf nach suchstring 00120 bool ESP8266Webserver::beinhaltet(char* suchstring) 00121 { 00122 char* referer; 00123 char* fundort; 00124 char* favicon; 00125 favicon=strstr(recbuf,"favicon"); 00126 if (favicon!=NULL) return false; 00127 referer=strstr(recbuf,"Referer"); 00128 fundort=strstr(recbuf, suchstring); 00129 if (strstr(recbuf,"ERROR")==NULL) 00130 if( fundort != NULL&&(fundort<referer||referer==NULL)) return true; 00131 return false; 00132 } 00133 00134 int ESP8266Webserver::handleClient(void) 00135 { 00136 int value,value2; 00137 if (_serial->readable()) 00138 { 00139 if (_parser->recv("+IPD,%d,%d:",&clientID[clientIdx],&value2)) 00140 { 00141 if (dbg) printf("\r\nich Client ID=%d, Anzahl=%d",clientID[clientIdx],value2); 00142 _parser->read(recbuf, value2); 00143 00144 if (dbg) printf("\r\nBuf=\n%s",recbuf); 00145 00146 gefunden=false; 00147 for (int i=0;i<hs_count&&!gefunden;i++) 00148 { 00149 if (beinhaltet(hs[i])) 00150 { 00151 (cbs[i])(); 00152 gefunden=true; 00153 } 00154 } 00155 if (dbg) for (int i=0;i<hs_count;i++) 00156 { 00157 printf("\r\n%s\r\n",hs[i]); 00158 } 00159 if (dbg) printf("\r\nich Got\r\n"); 00160 } 00161 } 00162 00163 return 0; 00164 } 00165 00166 00167 int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, string webseite) 00168 { 00169 return ESP8266Webserver::send(HTTPStatus,Mimetype, webseite.c_str()); 00170 } 00171 00172 int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, const char* webseite) 00173 { 00174 char hilf[40]; 00175 00176 //printf("\r\nHallo\r\n"); 00177 Aufrufe++; 00178 sprintf(sendstring,"HTTP/1.1 %d OK\n",HTTPStatus); 00179 strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n"); 00180 strcat(sendstring,"Connection: close\n"); 00181 strcat(sendstring,"Content-Type: "); 00182 strcat(sendstring,Mimetype); 00183 strcat(sendstring,"\n"); 00184 sprintf(hilf,"%d",strlen(webseite)); 00185 strcat(sendstring,"Content-Length: "); 00186 strcat(sendstring,hilf); 00187 strcat(sendstring,"\n\n"); 00188 strcat(sendstring,webseite); 00189 if (dbg) printf("len=%d, inhalt=\n%s",strlen(sendstring),sendstring); 00190 00191 _parser->debug_on(true); 00192 _parser->send("AT+CIPSEND=%d,%d",clientID[clientIdx],strlen(sendstring)); 00193 _parser->debug_on(false); 00194 _parser->write(sendstring,strlen(sendstring)); 00195 HAL_Delay(200); 00196 _parser->send("AT+CIPCLOSE=%d",clientID[clientIdx]); 00197 return 0; 00198 } 00199 00200 void ESP8266Webserver::debugOn(bool pD) 00201 { 00202 dbg=pD; 00203 _parser->debug_on(pD); 00204 } 00205 00206 const char* ESP8266Webserver::gibWert(string suchstring) 00207 { 00208 return ESP8266Webserver::gibWert(suchstring.c_str()); 00209 } 00210 00211 const char* ESP8266Webserver::gibWert(const char* suchstring) 00212 { 00213 static char hilf[20]; 00214 00215 char *fundort; 00216 int i=0; 00217 fundort=strstr(recbuf,suchstring); 00218 if (fundort!=NULL) 00219 { 00220 fundort=fundort+strlen(suchstring)+1; 00221 while(i<20 && fundort[i]!=38 && fundort[i]>32 ) //space, & 00222 { 00223 hilf[i]=fundort[i]; 00224 i++; 00225 } 00226 hilf[i]=0; 00227 if (dbg) printf("\r\nsuchergebnis=%s\r\n",hilf); 00228 return hilf; 00229 } 00230 else 00231 return NULL; 00232 } 00233 00234 void ESP8266Webserver::listAPs() 00235 { 00236 int ips[4]; 00237 //_parser->debug_on(true); 00238 _parser->send("AT+CWMODE=1"); 00239 HAL_Delay(300); 00240 while (_parser->recv("OK")==false); 00241 HAL_Delay(1000); 00242 if (scanAPs==true) 00243 { 00244 _parser->send("AT+CWLAP"); 00245 while (!_serial->readable()); 00246 HAL_Delay(10000); 00247 printf("\r\nBuf="); 00248 while (_serial->readable()) 00249 { 00250 _parser->read(recbuf, 1); 00251 printf("%s",recbuf); 00252 } 00253 printf("\r\n"); 00254 } 00255 for (int i=0;i<5;i++) 00256 { 00257 _parser->send("AT+CWJAP=\"%s\",\"%s\"",ssid,passwort); 00258 HAL_Delay(1000); 00259 if (_parser->recv("WIFI GOT IP")) i=6; 00260 } 00261 _parser->send("AT+CIFSR"); 00262 while (_parser->recv("+CIFSR:STAIP,")==false) 00263 { 00264 _parser->send("AT+CIFSR"); 00265 HAL_Delay(100); 00266 } 00267 00268 // _parser->read(ipadr,15); 00269 _parser->scanf("\"%d.%d.%d.%d\"",&ips[0],&ips[1],&ips[2],&ips[3]); 00270 sprintf(ipadr,"%d.%d.%d.%d",ips[0],ips[1],ips[2],ips[3]); 00271 printf("%s",ipadr); 00272 00273 _parser->send("AT+CIPMUX=1"); 00274 while (_parser->recv("OK")==false); 00275 00276 //_parser->debug_on(false); 00277 } 00278 00279 char* ESP8266Webserver::gibIP() 00280 { 00281 static char ipa[16]; 00282 int j=0; 00283 for (int i=0;i<strlen(ipadr);i++) 00284 { 00285 if (i==0 && ipadr[0]=='"'){} 00286 else 00287 { 00288 switch (ipadr[i]) 00289 { 00290 case '0': 00291 case '1': 00292 case '2': 00293 case '3': 00294 case '4': 00295 case '5': 00296 case '6': 00297 case '7': 00298 case '8': 00299 case '9': 00300 case '.': ipa[j]=ipadr[i]; 00301 j++; 00302 break; 00303 default: i=10000; break; 00304 } 00305 if (i==10000) break; 00306 } 00307 } 00308 ipa[j+1]='\0'; 00309 00310 return ipa; 00311 } 00312
Generated on Fri Aug 12 2022 07:09:17 by
1.7.2