diff --git a/WiFiManager.cpp b/WiFiManager.cpp index fcfcec7..17358ce 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -203,7 +203,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo WiFi.disconnect(); // this alone is not enough to stop the autoconnecter WiFi.mode(WIFI_AP); WiFi.persistent(true); - } + } else { //setup AP WiFi.mode(WIFI_AP_STA); @@ -232,26 +232,33 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo //HTTP server->handleClient(); - if (connect) { + delay(1000); connect = false; - delay(2000); - DEBUG_WM(F("Connecting to new AP")); - // using user-provided _ssid, _pass in place of system-stored ssid and pass - if (connectWifi(_ssid, _pass) != WL_CONNECTED) { - DEBUG_WM(F("Failed to connect.")); - } else { - //connected - WiFi.mode(WIFI_STA); - //notify that configuration has changed and any optional parameters should be saved - if ( _savecallback != NULL) { - //todo: check if any custom parameters actually exist, and check if they really changed maybe - _savecallback(); + // if saving with no ssid filled in, reconnect to ssid + // will not exit cp + if(_ssid == ""){ + DEBUG_WM(F("No ssid, skipping wifi")); + } + else{ + DEBUG_WM(F("Connecting to new AP")); + if (connectWifi(_ssid, _pass) != WL_CONNECTED) { + delay(2000); + // using user-provided _ssid, _pass in place of system-stored ssid and pass + DEBUG_WM(F("Failed to connect.")); } - break; + else { + //connected + WiFi.mode(WIFI_STA); + //notify that configuration has changed and any optional parameters should be saved + if ( _savecallback != NULL) { + //todo: check if any custom parameters actually exist, and check if they really changed maybe + _savecallback(); + } + break; + } } - if (_shouldBreakAfterConfig) { //flag set to exit after config after trying to connect //notify that configuration has changed and any optional parameters should be saved @@ -259,6 +266,18 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo //todo: check if any custom parameters actually exist, and check if they really changed maybe _savecallback(); } + WiFi.mode(WIFI_STA); // turn off ap + // reconnect to ssid + // int res = WiFi.begin(); + // attempt connect for 10 seconds + DEBUG_WM(F("Waiting for sta (10 secs) .......")); + for(size_t i = 0 ; i<100;i++){ + if(WiFi.status() == WL_CONNECTED) break; + DEBUG_WM("."); + // Serial.println(WiFi.status()); + delay(100); + } + delay(1000); break; } } @@ -286,9 +305,22 @@ int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(F("Already connected. Bailing out.")); return WL_CONNECTED; } + + DEBUG_WM(F("Status:")); + DEBUG_WM(WiFi.status()); + + wl_status_t res; //check if we have ssid and pass and force those, if not, try with last saved values if (ssid != "") { - WiFi.begin(ssid.c_str(), pass.c_str()); + //trying to fix connection in progress hanging + ETS_UART_INTR_DISABLE(); + wifi_station_disconnect(); + ETS_UART_INTR_ENABLE(); + res = WiFi.begin(ssid.c_str(), pass.c_str(),0,NULL,true); + if(res != WL_CONNECTED){ + DEBUG_WM(F("[ERROR] WiFi.begin res:")); + DEBUG_WM(res); + } } else { if (WiFi.SSID() != "") { DEBUG_WM(F("Using last saved values, should be faster")); @@ -296,8 +328,7 @@ int WiFiManager::connectWifi(String ssid, String pass) { ETS_UART_INTR_DISABLE(); wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); - - WiFi.begin(); + res = WiFi.begin(); } else { DEBUG_WM(F("No saved credentials")); }