From d09aff2f6d2976f6386865af681b4c45c7c7d2fe Mon Sep 17 00:00:00 2001 From: Shawn A Date: Fri, 27 Oct 2017 17:19:43 -0500 Subject: [PATCH] fixing some reliability issues, testing --- WiFiManager.cpp | 86 ++++++++++++++++++++++++++++++++++--------------- WiFiManager.h | 2 +- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index f66eaa4..1b9e2b7 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -127,6 +127,10 @@ bool WiFiManager::startAP(){ DEBUG_WM(F("Configuring access point with with SSID of ... ")); DEBUG_WM(_apName); + // bool dis = WiFi.softAPdisconnect(false); + // DEBUG_WM(F("Stopping SOFTAP")); + // DEBUG_WM(dis ? "success" : "failed"); + // setup optional soft AP static ip config if (_ap_static_ip) { DEBUG_WM(F("Custom AP IP/GW/Subnet")); @@ -135,14 +139,35 @@ bool WiFiManager::startAP(){ bool ret = true; + // WiFi.mode(WIFI_AP); // is a known state nefore startap required ? + // start soft AP with password or anonymous if (_apPassword != "") { + Serial.println((String)_apPassword.c_str()); ret = WiFi.softAP(_apName.c_str(), _apPassword.c_str());//password option } else { DEBUG_WM(F("AP has anonymous access")); ret = WiFi.softAP(_apName.c_str()); } + if(!ret) DEBUG_WM("There was an error starting the AP"); // @bug startAP returns unreliable success status + + struct softap_config conf; + wifi_softap_get_config(&conf); + + const char* ssid = reinterpret_cast(conf.ssid); + Serial.print("SSID ("); + Serial.print(strlen(ssid)); + Serial.print("): "); + Serial.println(ssid); + + const char* passphrase = reinterpret_cast(conf.password); + Serial.print("Passphrase ("); + Serial.print(strlen(passphrase)); + Serial.print("): "); + Serial.println(passphrase); + + delay(500); // slight delay to make sure we get an AP IP DEBUG_WM(F("AP IP address: ")); DEBUG_WM(WiFi.softAPIP()); @@ -152,6 +177,8 @@ bool WiFiManager::startAP(){ _apcallback(this); } + WiFi.printDiag(Serial); + return ret; } @@ -211,20 +238,20 @@ boolean WiFiManager::startConfigPortal() { boolean WiFiManager::startConfigPortal(char const *apName, char const *apPassword) { //setup AP - bool disableSTA = false; + bool disableSTA = false; // debug, always disable sta // @todo sometimes still cannot connect to AP for no known reason, no events in log either if(!WiFi.isConnected() || disableSTA){ // this fixes most ap problems, simply doing mode(WIFI_AP) does not work if sta connection is hanging + // @todo even if sta is connected, it is possible that softap connections will fail, IOS says "invalid password", windows says "cannot connect to this network" wifioff(); - wifimode(WIFI_AP); + if((WiFi.getMode() & WIFI_AP) != 0) wifimode(WIFI_AP); // @todo use bitwise toggling for these after testing DEBUG_WM("Disabling STA"); } else { - wifimode(WIFI_AP_STA); - } - - WiFi.printDiag(Serial); + if((WiFi.getMode() & WIFI_AP) != 0) wifimode(WIFI_AP_STA); + else wifimode(WIFI_STA); + } DEBUG_WM("Enabling AP"); @@ -239,8 +266,10 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo DEBUG_WM(F("setupConfigPortal")); _configPortalStart = millis(); - if(!startAP()) DEBUG_WM("There was an error starting the AP"); // @bug startAP returns unreliable success status - + + // start access point + startAP(); + // init configportal setupConfigPortal(); @@ -360,7 +389,12 @@ int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(WiFi.localIP()); } - wifimode(WIFI_AP_STA); // turn sta back on so begin does not call enablesta->mode + // make sure sta is on before `begin` so it does not call enablesta->mode during persistent + if((WiFi.getMode() & WIFI_AP) != 0) wifimode(WIFI_STA); + else wifimode(WIFI_AP_STA); + + WiFi.printDiag(Serial); + wifioff(); // disconnect before begin, in case anything is hung // if ssid argument provided connect to that @@ -383,7 +417,7 @@ int WiFiManager::connectWifi(String ssid, String pass) { uint8_t connRes = waitforconx ? waitForConnectResult() : WL_NO_SSID_AVAIL; DEBUG_WM ("Connection result: "); - DEBUG_WM ( getConnResString(connRes) ); + DEBUG_WM ( getWLStatusString(connRes) ); // do WPS, if WPS options enabled and not connected and no password was supplied // @todo this seems like wrong place for this, is it a fallback or option? @@ -424,22 +458,6 @@ uint8_t WiFiManager::waitForConnectResult() { return status; } -String WiFiManager::getConnResString(uint8_t status){ - switch(status) { - case STATION_GOT_IP: - return "WL_CONNECTED"; - case STATION_NO_AP_FOUND: - return "WL_NO_SSID_AVAIL"; - case STATION_CONNECT_FAIL: - case STATION_WRONG_PASSWORD: - return "WL_CONNECT_FAILED"; - case STATION_IDLE: - return "WL_IDLE_STATUS"; - default: - return "WL_DISCONNECTED"; - } -} - void WiFiManager::startWPS() { DEBUG_WM("START WPS"); WiFi.beginWPSConfig(); @@ -1223,6 +1241,22 @@ boolean WiFiManager::validApPassword(){ return true; } +String WiFiManager::getWLStatusString(uint8_t status){ + const char * const WIFI_STA_STATUS[] + { + "WL_IDLE_STATUS", + "WL_NO_SSID_AVAIL", + "WL_SCAN_COMPLETED", + "WL_CONNECTED", + "WL_CONNECT_FAILED", + "WL_CONNECTION_LOST", + "WL_DISCONNECTED" + }; + + if(status >=0 && status <=6) return WIFI_STA_STATUS[status]; + return "UNKNOWN"; +} + String WiFiManager::encryptionTypeStr(uint8_t authmode) { switch(authmode) { case ENC_TYPE_NONE: diff --git a/WiFiManager.h b/WiFiManager.h index 319d946..794e54b 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -207,7 +207,7 @@ class WiFiManager int connectWifi(String ssid, String pass); uint8_t waitForConnectResult(); uint8_t waitForConnectResult(uint16_t timeout); - String getConnResString(uint8_t status); + String getWLStatusString(uint8_t status); void handleRoot(); void handleWifi(boolean scan);