Library für ESP01 Webserver STM32F103RB Nucleo
Dependents: Nucleo_dht11_Webserver WebServerGSOEWorkshopSlider WebServerGSOEWorkshopDynamisch WebServerGSOEWorkshopStatisch ... more
Diff: ESP8266Webserver.cpp
- Revision:
- 15:e710f0431920
- Parent:
- 14:d17cfd8131b1
- Child:
- 16:b09391fb73b4
diff -r d17cfd8131b1 -r e710f0431920 ESP8266Webserver.cpp --- a/ESP8266Webserver.cpp Thu Jun 10 07:32:53 2021 +0000 +++ b/ESP8266Webserver.cpp Fri Jun 18 15:33:21 2021 +0000 @@ -27,18 +27,28 @@ 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 (station==true) + { + listAPs(); + printf("%s",ipadr); + } + else { + _parser->send("ATE0"); + strcpy(ipadr,ip); - if (_parser->recv("OK")) { - if (pDebug) printf("\nAntw.: OK\r\n"); - } else { - if (pDebug) printf("\nATCmdParser: OK failed\r\n"); - } - + //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?"); @@ -71,17 +81,18 @@ 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=\"%s\",\"%s\",\"%s\" \r\n",ipad.c_str(),ipad.c_str(),netzmaske); -// _parser->send("AT+CIPAP=\"192.168.5.1\",\"192.168.5.1\",\"255.255.255.0\""); - _parser->send("AT+CIPAP=\"%s\",\"%s\",\"%s\"",ipad.c_str(),ipad.c_str(),netzmaske); - - if (_parser->recv("OK")) { - if (pDebug) printf("\nAntw.: OK\r\n"); - } else { - if (pDebug) printf("\nATCmdParser: OK failed\r\n"); - } - + if (pDebug) printf("\nATCmdParser: //AT+CIPAP=\"%s\",\"%s\",\"%s\" \r\n",ipad.c_str(),ipad.c_str(),netzmaske); + // _parser->send("AT+CIPAP=\"192.168.5.1\",\"192.168.5.1\",\"255.255.255.0\""); + _parser->send("AT+CIPAP=\"%s\",\"%s\",\"%s\"",ipad.c_str(),ipad.c_str(),netzmaske); + + 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"); @@ -92,7 +103,7 @@ } else { if (pDebug) printf("\nATCmdParser: OK failed\r\n"); } - + } } @@ -114,8 +125,8 @@ int ESP8266Webserver::begin(void) { //AT+CIPSERVER=1,80 - if (dbg) printf("\nATCmdParser: //AT+CIPSERVER=1,80"); - _parser->send("AT+CIPSERVER=1,80"); + if (dbg) printf("\nATCmdParser: //AT+CIPSERVER=1,%d",Port); + _parser->send("AT+CIPSERVER=1,%d",Port); if (_parser->recv("OK")) { if (dbg) printf("\nAntw.: OK\r\n"); return 0; @@ -261,73 +272,49 @@ return NULL; } -void ESP8266Webserver::connect_wifi(string cmd, int t) //This function is for connecting ESP8266 with wifi network by using AT commands +void ESP8266Webserver::listAPs() { - int i=0; - while(1) - { - //Serial2println(cmd); //sends to ESP8266 via serial communication - HAL_Delay(t); - /* - while(Serial2available()) + + //_parser->debug_on(true); + _parser->send("AT+CWMODE=1"); + while (_parser->recv("OK")==false); + HAL_Delay(1000); + if (scanAPs==true) { - if(Serial2find("OK")) - i=8; + _parser->send("AT+CWLAP"); + while (!_serial->readable()); + HAL_Delay(10000); + printf("\r\nBuf="); + while (_serial->readable()) + { + _parser->read(recbuf, 1); + printf("%s",recbuf); + } + printf("\r\n"); } - */ - if(i>5) - break; - i++; - } - -void ESP8266Webserver::WIFIsetup(string pSSID, string pPW) -{ - ssid=pSSID; - password=pPW; - wifi_init(); + for (int i=0;i<5;i++) + { + _parser->send("AT+CWJAP=\"%s\",\"%s\"",ssid,passwort); + HAL_Delay(1000); + if (_parser->recv("WIFI GOT IP")) i=6; + } + _parser->send("AT+CIFSR"); + while (_parser->recv("+CIFSR:STAIP,")==false) + { + _parser->send("AT+CIFSR"); + HAL_Delay(100); + } + _parser->read(ipadr,15); + printf("%s",ipadr); + + _parser->send("AT+CIPMUX=1"); + while (_parser->recv("OK")==false); + + //_parser->debug_on(false); } -void ESP8266Webserver::wifi_init() //This function contains AT commands that passes to connect_wifi() +char* ESP8266Webserver::gibIP() { - //clearBuffer(); - //connect_wifi("AT",100); //Sends AT command with time(Command for Achknowledgement) - //connect_wifi("AT+CWMODE=1",100); //Sends AT command with time (For setting mode of Wifi) - //connect_wifi("AT+CIFSR",100); - //bool No_IP=check4IP(100); - if (No_IP) - { - char anmeldedaten[50]; - strcpy(anmeldedaten,"AT+CWJAP=\""); - strcat(anmeldedaten,ssid); - strcat(anmeldedaten,"\",\""); - strcat(anmeldedaten,password); - strcat(anmeldedaten,"\""); - connect_wifi(anmeldedaten,7000); - //connect_wifi("AT+CWJAP=\"x-Netz\",\"aCT3xuSbm9rt\"",7000); //provide your WiFi username and password here - } - IP[0]=0; - get_ip(); - LCD_i2c_textzeile1(IP); - - connect_wifi("AT+CIPMUX=1",100); //Sends AT command with time (For creating multiple connections) - connect_wifi("AT+CIPSERVER=1,80",100); //Sends AT command with time (For setting up server with port 80) - clearBuffer(); - writeBit(PC7,1); + return ipadr; } -bool ESP8266Webserver::check4IP(int t1) //A function to check ip of ESP8266 -{ - HAL_Delay((long)t1); - bool No_IP=true; - /* - while(Serial2available()>0) - { - if(Serial2find("STAIP")) - { - No_IP=false; - } - } - */ - return No_IP; - -} \ No newline at end of file