Library für ESP01 Webserver STM32F103RB Nucleo
Dependents: Nucleo_dht11_Webserver WebServerGSOEWorkshopSlider WebServerGSOEWorkshopDynamisch WebServerGSOEWorkshopStatisch ... more
Diff: ESP8266Webserver.cpp
- Revision:
- 13:ed5c59ed4ae5
- Child:
- 14:d17cfd8131b1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266Webserver.cpp Mon Jun 07 18:32:57 2021 +0000 @@ -0,0 +1,261 @@ +#include "ESP8266Webserver.h" + +ESP8266Webserver::ESP8266Webserver(int Port,PinName tx, PinName rx, bool pDebug) +{ + //HAL_Delay(1000); + hs_count=0; + clientIdx=0; + if (pDebug) printf("\nATCmdParser with ESP8266 example"); + port=Port; + + dbg=pDebug; + _serial = new BufferedSerial(tx,rx, ESP8266_DEFAULT_BAUD_RATE); + _parser = new ATCmdParser(_serial, "\r\n"); + _parser->debug_on(false);//pDebug); + + _parser->set_timeout(1000); + +//AT + if (pDebug) printf("\nATCmdParser: AT\r\n"); + _parser->send("AT"); + + //HAL_Delay(1000); + + if (_parser->recv("OK")) { + if (pDebug) printf("\nAntw.: OK\r\n"); + } else { + if (pDebug) printf("\nATCmdParser: OK failed\r\n"); + } + + _parser->send("ATE0"); + //HAL_Delay(300); +//AT+CWMODE=1 + if (pDebug) printf("\nATCmdParser: //AT+CWMODE=2 \r\n"); + _parser->send("AT+CWMODE=2"); + + if (_parser->recv("OK")) { + if (pDebug) printf("\nAntw.: OK\r\n"); + } else { + if (pDebug) printf("\nATCmdParser: OK failed\r\n"); + } + +//AT+CWMODE? + if (pDebug) printf("\nATCmdParser: //AT+CWMODE? \r\n"); + _parser->send("AT+CWMODE?"); + + if (_parser->recv("OK")) { + if (pDebug) printf("\nAntw.: OK\r\n"); + } else { + if (pDebug) printf("\nATCmdParser: OK failed\r\n"); + } + + +//AT+CIFSR + if (pDebug) printf("\nATCmdParser: //AT+CIFSR \r\n"); + _parser->send("AT+CIFSR"); + + if (_parser->recv("OK")) { + if (pDebug) printf("\nAntw.: OK\r\n"); + } else { + if (pDebug) printf("\nATCmdParser: OK failed\r\n"); + } + +/* +//AT+CWJAP= “Wi-FiNetwork”,“Password” + printf("\nATCmdParser: //AT+CWJAP= \"Wi-FiNetwork\",\"Password\" \r\n"); + _parser->send("AT+CWJAP=\"x-Netz\",\"aCT3xuSbm9rt\""); + + if (_parser->recv("OK")) { + printf("\nAntw.: OK\r\n"); + } else { + printf("\nATCmdParser: OK failed\r\n"); + } +*/ +//AT+CIPAP="192.168.5.1","192.168.5.1","255.255.255.0" + if (pDebug) printf("\nATCmdParser: //AT+CIPAP=\"192.168.5.1\",\"192.168.5.1\",\"255.255.255.0\" \r\n"); + _parser->send("AT+CIPAP=\"192.168.5.1\",\"192.168.5.1\",\"255.255.255.0\""); + + if (_parser->recv("OK")) { + if (pDebug) printf("\nAntw.: OK\r\n"); + } else { + if (pDebug) printf("\nATCmdParser: OK failed\r\n"); + } + + +//AT+CIPMUX=1 + if (pDebug) printf("\nATCmdParser: //AT+CIPMUX=1"); + _parser->send("AT+CIPMUX=1"); + + if (_parser->recv("OK")) { + if (pDebug) printf("\nAntw.: OK\r\n"); + } else { + if (pDebug) printf("\nATCmdParser: OK failed\r\n"); + } + +} + + + +int ESP8266Webserver::on(const char* handlestring,Callback< void()> func) +{ + if (hs_count>9) return -1; + else + { + strcpy(hs[hs_count],handlestring); + if (dbg) printf("\r\n\r\n%s, %s\r\n",handlestring,hs[hs_count]); + cbs[hs_count]=func; + hs_count++; + return 0; + } + +} + +int ESP8266Webserver::begin(void) +{ +//AT+CIPSERVER=1,80 + if (dbg) printf("\nATCmdParser: //AT+CIPSERVER=1,80"); + _parser->send("AT+CIPSERVER=1,80"); + if (_parser->recv("OK")) { + if (dbg) printf("\nAntw.: OK\r\n"); + return 0; + } else { + if (dbg) printf("\nATCmdParser: OK failed\r\n"); + return -1; + } +} + +//durchsucht recbuf nach suchstring +bool ESP8266Webserver::beinhaltet(char* suchstring) +{ + char* referer; + char* fundort; + char* favicon; + favicon=strstr(recbuf,"favicon"); + if (favicon!=NULL) return false; + referer=strstr(recbuf,"Referer"); + fundort=strstr(recbuf, suchstring); + if (strstr(recbuf,"ERROR")==NULL) + if( fundort != NULL&&(fundort<referer||referer==NULL)) return true; + return false; +} + +int ESP8266Webserver::handleClient(void) +{ + int value,value2; + if (_serial->readable()) + { + /* HAL_Delay(1000); + _serial->read(buf,1000); + printf("\r\n ich %s\r\n",buf); + */ + if (_parser->recv("+IPD,%d,%d:",&clientID[clientIdx],&value2)) //_parser->recv("+IPD,%d",value) + { + + //HAL_Delay(1000); + + if (dbg) printf("\r\nich Client ID=%d, Anzahl=%d",clientID[clientIdx],value2); + _parser->read(recbuf, value2); + //HAL_Delay(1000); + + //recbuf[value2]=0; + if (dbg) printf("\r\nBuf=\n%s",recbuf); + + //(cbs[1])(); + gefunden=false; + for (int i=0;i<hs_count&&!gefunden;i++) + { + + if (beinhaltet(hs[i])) + { + + (cbs[i])(); + + gefunden=true; + } + } + + if (dbg) for (int i=0;i<hs_count;i++) + { + printf("\r\n%s\r\n",hs[i]); + } + + if (dbg) printf("\r\nich Got\r\n"); + + //_parser->flush(); + + } + + } + + return 0; +} + + +int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, string webseite) +{ + return ESP8266Webserver::send(HTTPStatus,Mimetype, webseite.c_str()); +} +int ESP8266Webserver::send(int HTTPStatus,const char* Mimetype, const char* webseite) +{ + char hilf[40]; + + printf("\r\nHallo\r\n"); + Aufrufe++; + sprintf(sendstring,"HTTP/1.1 %d OK\n",HTTPStatus); + strcat(sendstring,"Date: Wed, 23 Apr 2021 04:36:25 GMT\n"); + strcat(sendstring,"Connection: close\n"); + strcat(sendstring,"Content-Type: "); + strcat(sendstring,Mimetype); + strcat(sendstring,"\n"); + sprintf(hilf,"%d",strlen(webseite)); + strcat(sendstring,"Content-Length: "); + strcat(sendstring,hilf); + strcat(sendstring,"\n\n"); + strcat(sendstring,webseite); + if (dbg) printf("len=%d, inhalt=\n%s",strlen(sendstring),sendstring); + //sprintf(hilf,"AT+CIPSEND=%d,%d\r\n",clientID,strlen(sendstring)); + //printf("\n %s, %d\n",hilf,strlen(hilf)); + //_serial->write(hilf,strlen(hilf)); + _parser->debug_on(true); + _parser->send("AT+CIPSEND=%d,%d",clientID[clientIdx],strlen(sendstring)); + _parser->debug_on(false); + _parser->write(sendstring,strlen(sendstring)); + HAL_Delay(200); + _parser->send("AT+CIPCLOSE=%d",clientID[clientIdx]); + return 0; +} + +void ESP8266Webserver::debugOn(bool pD) +{ + dbg=pD; + _parser->debug_on(pD); + +} + +string ESP8266Webserver::gibWertString(string suchstring) +{ + return ESP8266Webserver::gibWert(suchstring.c_str()); +} + +const char* ESP8266Webserver::gibWert(const char* suchstring) +{ + static char hilf[20]; + + char *fundort; + int i=0; + fundort=strstr(recbuf,suchstring); + if (fundort!=NULL) + { + fundort=fundort+strlen(suchstring)+1; + while(i<20 && fundort[i]!=38 && fundort[i]>32 ) //space, & + { + hilf[i]=fundort[i]; + i++; + } + hilf[i]=0; + if (dbg) printf("\r\nsuchergebnis=%s\r\n",hilf); + return hilf; + } + else + return NULL; +} \ No newline at end of file