However, I am getting a consistent connection error in the browser whenever I try to connect with the ESP in WIFI_STA mode after having first used the ESP in WIFI_AP mode. Note that if I don’t connect to the Access Point and just allow the 60 second timeout to occur then the connection to the Station works fine.
Below is a sample sketch that replicates the problem. If you connect to the access point during the first 60 seconds and hit the home page (in this example you will get a “myAPServer found” response in the browser) and then try and connect to the subsequent Station you get a browser connection error. If you don’t connect to the Access Point and just allow the timeout to occur then a connection to the Station will occur fine and a hit on the home page will give a “myServer found” response.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer * myAPServer = NULL;
ESP8266WebServer * myServer = NULL;
const char * SSID = "ENTER SSID";
const char * psk = "ENTER PSK";
bool _continue = false;
void handleRoot(ESP8266WebServer * server, const char * str)
{
server->send(200, "text/plain", str);
_continue = true;
}
void setup(void)
{
Serial.begin(115200);
WiFi.disconnect();
Serial.print("\n\n*** Starting AP Portal for 60 seconds.\n");
Serial.print("*** Connect to TestAP and go to 192.168.4.1 in web browser\n");
Serial.print("*** You should get a \"myAPServer found\" response\n");
WiFi.mode(WIFI_AP);
WiFi.softAP("TestAP");
myAPServer = new ESP8266WebServer(80);
myAPServer->on("/", std::bind(handleRoot, myAPServer, "myAPServer found"));
myAPServer->begin();
unsigned long start_time = millis();
while ((millis() - start_time < 60000) && _continue == false) {
myAPServer->handleClient();
yield();
}
WiFi.softAPdisconnect();
delete myAPServer;
Serial.print("\n\n*** Starting STA mode.\n");
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, psk);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.print("Connection Timeout\n");
ESP.reset();
}
myServer = new ESP8266WebServer(80);
myServer->on("/", std::bind(handleRoot, myServer, "myServer found"));
myServer->begin();
Serial.print("*** Connect to usual AP and go to ");
Serial.print(WiFi.localIP());
Serial.print(" in web browser.\n");
Serial.print("*** I get a \"connection error\" rather than \"myServer found\" response\n");
delay(5000);
}
void loop(void) {
myServer->handleClient();
}
Appreciate any thoughts on the above even if it is only confirmation that the error can be replicated! This is on a NodeMCU 1.0 (ESP-12E Module) with SDK 1.5.1 and ESP8266 2.1.0