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