From 9084a5542b1f43e5ee851e30fe679ab1d493f507 Mon Sep 17 00:00:00 2001 From: TD-er Date: Sun, 29 Apr 2018 13:06:20 +0200 Subject: [PATCH] [wifi] Do not rely on WiFi.status() and better log of status `WiFi.status() == WL_CONNECTED` may not always be correct. For ESP82xx check the SDK status. --- src/ESPEasy-Globals.h | 7 +++-- src/ESPEasy.ino | 4 +-- src/ESPEasyWifi.ino | 64 +++++++++++++++++++++++++++++++++---------- src/Networking.ino | 6 ++-- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/ESPEasy-Globals.h b/src/ESPEasy-Globals.h index c79bdcd898..82fc713d1f 100644 --- a/src/ESPEasy-Globals.h +++ b/src/ESPEasy-Globals.h @@ -557,6 +557,7 @@ enum Command { // Forward declarations. Command commandStringToEnum(const char * cmd); bool WiFiConnected(uint32_t timeout_ms); +bool WiFiConnected(); bool hostReachable(const IPAddress& ip); bool hostReachable(const String& hostname); void formatMAC(const uint8_t* mac, char (&strMAC)[20]); @@ -825,7 +826,7 @@ struct ControllerSettingsStruct if (!UseDNS) { return true; } - if (WiFi.status() != WL_CONNECTED) return false; + if (!WiFiConnected()) return false; IPAddress tmpIP; if (WiFi.hostByName(HostName, tmpIP)) { for (byte x = 0; x < 4; x++) { @@ -1205,11 +1206,11 @@ Ticker connectionCheck; bool reconnectChecker = false; void connectionCheckHandler() { - if (reconnectChecker == false && WiFi.status() != WL_CONNECTED){ + if (reconnectChecker == false && !WiFiConnected()){ reconnectChecker = true; WiFi.reconnect(); } - else if (WiFi.status() == WL_CONNECTED && reconnectChecker == true){ + else if (WiFiConnected() && reconnectChecker == true){ reconnectChecker = false; } } diff --git a/src/ESPEasy.ino b/src/ESPEasy.ino index a07b656ea4..20d43f3fc7 100644 --- a/src/ESPEasy.ino +++ b/src/ESPEasy.ino @@ -325,7 +325,7 @@ void setup() #endif #ifndef ESP32 - connectionCheck.attach(240, connectionCheckHandler); + connectionCheck.attach(30, connectionCheckHandler); #endif } @@ -393,7 +393,7 @@ void loop() if (wifiStatus >= ESPEASY_WIFI_CONNECTED) processConnect(); if (wifiStatus >= ESPEASY_WIFI_GOT_IP) processGotIP(); if (wifiStatus == ESPEASY_WIFI_DISCONNECTED) processDisconnect(); - } else if (WiFi.status() != WL_CONNECTED) { + } else if (!WiFiConnected()) { // Somehow the WiFi has entered a limbo state. // FIXME TD-er: This may happen on WiFi config with AP_STA mode active. // addLog(LOG_LEVEL_ERROR, F("Wifi status out sync")); diff --git a/src/ESPEasyWifi.ino b/src/ESPEasyWifi.ino index 09f0261d10..ed3cdb3ecc 100644 --- a/src/ESPEasyWifi.ino +++ b/src/ESPEasyWifi.ino @@ -411,9 +411,17 @@ bool useStaticIP() { return (Settings.IP[0] != 0 && Settings.IP[0] != 255); } +bool WiFiConnected() { + #ifdef ESP32 + return WiFi.status() == WL_CONNECTED; + #else + // For ESP82xx, do not rely on WiFi.status() with event based wifi. + return wifi_station_get_connect_status() == STATION_GOT_IP; + #endif +} void WiFiConnectRelaxed() { - if (WiFi.status() == WL_CONNECTED) + if (WiFiConnected()) return; //already connected, need to disconnect first if (prepareWiFi()) { if (selectValidWiFiSettings()) { @@ -666,20 +674,22 @@ String formatScanResult(int i, const String& separator) { return result; } -void logConnectionStatus() { - const uint8_t arduino_corelib_wifistatus = WiFi.status(); - String log; - #ifndef ESP32 - const uint8_t sdk_wifistatus = wifi_station_get_connect_status(); - if (arduino_corelib_wifistatus != sdk_wifistatus) { - log = F("WIFI : SDK station status differs from Arduino status. SDK-status: "); - log += sdk_wifistatus; - log += F(" Arduino status: "); - log += arduino_corelib_wifistatus; - addLog(LOG_LEVEL_ERROR, log); +#ifndef ESP32 +String SDKwifiStatusToString(uint8_t sdk_wifistatus) { + switch (sdk_wifistatus) { + case STATION_IDLE: return F("STATION_IDLE"); + case STATION_CONNECTING: return F("STATION_CONNECTING"); + case STATION_WRONG_PASSWORD: return F("STATION_WRONG_PASSWORD"); + case STATION_NO_AP_FOUND: return F("STATION_NO_AP_FOUND"); + case STATION_CONNECT_FAIL: return F("STATION_CONNECT_FAIL"); + case STATION_GOT_IP: return F("STATION_GOT_IP"); } - #endif - log = F("WIFI : Arduino wifi status: "); + return F("Unknown"); +} +#endif + +String ArduinoWifiStatusToString(uint8_t arduino_corelib_wifistatus) { + String log; switch (arduino_corelib_wifistatus) { case WL_IDLE_STATUS: log += F("WL_IDLE_STATUS"); break; case WL_NO_SSID_AVAIL: log += F("WL_NO_SSID_AVAIL"); break; @@ -690,7 +700,11 @@ void logConnectionStatus() { case WL_DISCONNECTED: log += F("WL_DISCONNECTED"); break; default: log += arduino_corelib_wifistatus; break; } - log += F(" ESPeasy internal wifi status: "); + return log; +} + +String ESPeasyWifiStatusToString() { + String log; switch (wifiStatus) { case ESPEASY_WIFI_DISCONNECTED: log += F("ESPEASY_WIFI_DISCONNECTED"); break; case ESPEASY_WIFI_CONNECTED: log += F("ESPEASY_WIFI_CONNECTED"); break; @@ -698,6 +712,26 @@ void logConnectionStatus() { case ESPEASY_WIFI_SERVICES_INITIALIZED: log += F("ESPEASY_WIFI_SERVICES_INITIALIZED"); break; default: log += wifiStatus; } + return log; +} + +void logConnectionStatus() { + const uint8_t arduino_corelib_wifistatus = WiFi.status(); + String log; + #ifndef ESP32 + const uint8_t sdk_wifistatus = wifi_station_get_connect_status(); + if ((arduino_corelib_wifistatus == WL_CONNECTED) != (sdk_wifistatus == STATION_GOT_IP)) { + log = F("WIFI : SDK station status differs from Arduino status. SDK-status: "); + log += SDKwifiStatusToString(sdk_wifistatus); + log += F(" Arduino status: "); + log += ArduinoWifiStatusToString(arduino_corelib_wifistatus); + addLog(LOG_LEVEL_ERROR, log); + } + #endif + log = F("WIFI : Arduino wifi status: "); + log += ArduinoWifiStatusToString(arduino_corelib_wifistatus); + log += F(" ESPeasy internal wifi status: "); + log += ESPeasyWifiStatusToString(); addLog(LOG_LEVEL_DEBUG_MORE, log); } diff --git a/src/Networking.ino b/src/Networking.ino index cbfaa3e823..d355d3a873 100644 --- a/src/Networking.ino +++ b/src/Networking.ino @@ -607,7 +607,7 @@ bool WiFiConnected(uint32_t timeout_ms) { // Apparently something needs network, perform check to see if it is ready now. // if (!tryConnectWiFi()) // return false; - while (WiFi.status() != WL_CONNECTED) { + while (!WiFiConnected()) { if (timeOutReached(timer)) { return false; } @@ -617,7 +617,7 @@ bool WiFiConnected(uint32_t timeout_ms) { } bool hostReachable(const IPAddress& ip) { - if (WiFi.status() != WL_CONNECTED) return false; + if (!WiFiConnected()) return false; // Only do 1 ping at a time to return early byte retry = 3; while (retry > 0) { @@ -643,7 +643,7 @@ bool hostReachable(const IPAddress& ip) { } bool hostReachable(const String& hostname) { - if (WiFi.status() != WL_CONNECTED) return false; + if (!WiFiConnected()) return false; IPAddress remote_addr; if (WiFi.hostByName(hostname.c_str(), remote_addr)) { return hostReachable(remote_addr);