From db869fcb192be79d7e0e95cdf4271a7f63e3bb22 Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Sat, 4 Jul 2020 14:10:16 +0200 Subject: [PATCH] [WiFi] Reduce wifi reset calls and call full wifi init at reset --- lib/I2Cdevlib/library.json | 7 ++++++- src/ESPEasy.ino | 19 +---------------- src/ESPEasyWiFiEvent.cpp | 2 +- src/ESPEasyWiFiEvent.h | 2 +- src/ESPEasyWifi.cpp | 31 +++++++++++++++++++++++++++- src/ESPEasyWifi.h | 1 + src/ESPEasyWifi_ProcessEvent.cpp | 20 +++++++++++------- src/src/DataStructs/SettingsStruct.h | 2 +- src/src/Globals/ESPEasyWiFiEvent.cpp | 1 + src/src/Globals/ESPEasyWiFiEvent.h | 1 + 10 files changed, 56 insertions(+), 30 deletions(-) diff --git a/lib/I2Cdevlib/library.json b/lib/I2Cdevlib/library.json index d45609604..e974a9ab3 100644 --- a/lib/I2Cdevlib/library.json +++ b/lib/I2Cdevlib/library.json @@ -9,5 +9,10 @@ "url": "https://github.com/jrowberg/i2cdevlib.git" }, "frameworks": "arduino", - "platforms": "atmelavr" + "platforms": "atmelavr", + "dependencies": [ + { + "name": "Wire" + } + ] } diff --git a/src/ESPEasy.ino b/src/ESPEasy.ino index e1516083e..6e176535b 100644 --- a/src/ESPEasy.ino +++ b/src/ESPEasy.ino @@ -172,12 +172,7 @@ void setup() #ifdef ESP8266_DISABLE_EXTRA4K disable_extra4k_at_link_time(); #endif - WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters - WiFi.setAutoReconnect(false); - // The WiFi.disconnect() ensures that the WiFi is working correctly. If this is not done before receiving WiFi connections, - // those WiFi connections will take a long time to make or sometimes will not work at all. - WiFi.disconnect(); - setWifiMode(WIFI_OFF); + initWiFi(); run_compiletime_checks(); lowestFreeStack = getFreeStackWatermark(); @@ -209,18 +204,6 @@ void setup() initLog(); -#if defined(ESP32) - WiFi.onEvent(WiFiEvent); -#else - // WiFi event handlers - stationConnectedHandler = WiFi.onStationModeConnected(onConnected); - stationDisconnectedHandler = WiFi.onStationModeDisconnected(onDisconnect); - stationGotIpHandler = WiFi.onStationModeGotIP(onGotIP); - stationModeDHCPTimeoutHandler = WiFi.onStationModeDHCPTimeout(onDHCPTimeout); - APModeStationConnectedHandler = WiFi.onSoftAPModeStationConnected(onConnectedAPmode); - APModeStationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(onDisonnectedAPmode); -#endif - if (SpiffsSectors() < 32) { serialPrintln(F("\nNo (or too small) FS area..\nSystem Halted\nPlease reflash with 128k FS minimum!")); diff --git a/src/ESPEasyWiFiEvent.cpp b/src/ESPEasyWiFiEvent.cpp index 939b1e884..b0cc3222b 100644 --- a/src/ESPEasyWiFiEvent.cpp +++ b/src/ESPEasyWiFiEvent.cpp @@ -212,7 +212,7 @@ void onConnectedAPmode(const WiFiEventSoftAPModeStationConnected& event) { processedConnectAPmode = false; } -void onDisonnectedAPmode(const WiFiEventSoftAPModeStationDisconnected& event) { +void onDisconnectedAPmode(const WiFiEventSoftAPModeStationDisconnected& event) { for (byte i = 0; i < 6; ++i) { lastMacDisconnectedAPmode[i] = event.mac[i]; } diff --git a/src/ESPEasyWiFiEvent.h b/src/ESPEasyWiFiEvent.h index 2863fbddc..1bc9724bf 100644 --- a/src/ESPEasyWiFiEvent.h +++ b/src/ESPEasyWiFiEvent.h @@ -56,7 +56,7 @@ void ICACHE_RAM_ATTR onDHCPTimeout(); void onConnectedAPmode(const WiFiEventSoftAPModeStationConnected& event); -void onDisonnectedAPmode(const WiFiEventSoftAPModeStationDisconnected& event); +void onDisconnectedAPmode(const WiFiEventSoftAPModeStationDisconnected& event); #endif // ifdef ESP32 diff --git a/src/ESPEasyWifi.cpp b/src/ESPEasyWifi.cpp index 4ab6b0c8f..4c582916f 100644 --- a/src/ESPEasyWifi.cpp +++ b/src/ESPEasyWifi.cpp @@ -108,7 +108,6 @@ bool WiFiConnected() { STOP_TIMER(WIFI_ISCONNECTED_STATS); return true; } - // else wifiStatus is no longer in sync. addLog(LOG_LEVEL_INFO, F("WIFI : WiFiConnected() out of sync")); resetWiFi(); @@ -234,8 +233,13 @@ bool prepareWiFi() { void resetWiFi() { + if (lastWiFiResetMoment != 0 && timePassedSince(lastWiFiResetMoment) < 1000) { + // Don't reset WiFi too often + return; + } addLog(LOG_LEVEL_INFO, F("Reset WiFi.")); lastDisconnectMoment = millis(); + lastWiFiResetMoment = millis(); // Mark all flags to default to prevent handling old events. processedConnect = true; @@ -250,12 +254,36 @@ void resetWiFi() { // setWifiMode(WIFI_OFF); + initWiFi(); +} + +void initWiFi() +{ #ifdef ESP8266 // See https://github.com/esp8266/Arduino/issues/5527#issuecomment-460537616 WiFi.~ESP8266WiFiClass(); WiFi = ESP8266WiFiClass(); #endif // ifdef ESP8266 + + WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters + WiFi.setAutoReconnect(false); + // The WiFi.disconnect() ensures that the WiFi is working correctly. If this is not done before receiving WiFi connections, + // those WiFi connections will take a long time to make or sometimes will not work at all. + WiFi.disconnect(); + setWifiMode(WIFI_OFF); + +#if defined(ESP32) + WiFi.onEvent(WiFiEvent); +#else + // WiFi event handlers + stationConnectedHandler = WiFi.onStationModeConnected(onConnected); + stationDisconnectedHandler = WiFi.onStationModeDisconnected(onDisconnect); + stationGotIpHandler = WiFi.onStationModeGotIP(onGotIP); + stationModeDHCPTimeoutHandler = WiFi.onStationModeDHCPTimeout(onDHCPTimeout); + APModeStationConnectedHandler = WiFi.onSoftAPModeStationConnected(onConnectedAPmode); + APModeStationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(onDisconnectedAPmode); +#endif } // ******************************************************************************** @@ -272,6 +300,7 @@ void WifiDisconnect() #endif // if defined(ESP32) wifiStatus = ESPEASY_WIFI_DISCONNECTED; processedDisconnect = false; + wifiConnectAttemptNeeded = true; } // ******************************************************************************** diff --git a/src/ESPEasyWifi.h b/src/ESPEasyWifi.h index 3a11606ed..f3024802b 100644 --- a/src/ESPEasyWifi.h +++ b/src/ESPEasyWifi.h @@ -22,6 +22,7 @@ bool WiFiConnected(); void WiFiConnectRelaxed(); bool prepareWiFi(); void resetWiFi(); +void initWiFi(); void WifiDisconnect(); void WifiScan(bool async, bool quick); void WifiScan(); diff --git a/src/ESPEasyWifi_ProcessEvent.cpp b/src/ESPEasyWifi_ProcessEvent.cpp index f5b485513..3d52b3015 100644 --- a/src/ESPEasyWifi_ProcessEvent.cpp +++ b/src/ESPEasyWifi_ProcessEvent.cpp @@ -69,7 +69,7 @@ void handle_unprocessedWiFiEvents() addLog(LOG_LEVEL_DEBUG, F("WIFI : DHCP timeout, Calling disconnect()")); #endif // ifndef BUILD_NO_DEBUG processedDHCPTimeout = true; - processDisconnect(); + WifiDisconnect(); } if (wifiStatus & ESPEASY_WIFI_CONNECTED) { @@ -187,7 +187,7 @@ void processDisconnect() { } if (Settings.WiFiRestart_connection_lost()) { - setWifiMode(WIFI_OFF); + initWiFi(); delay(100); } logConnectionStatus(); @@ -195,13 +195,16 @@ void processDisconnect() { void processConnect() { if (processedConnect) { return; } + //delay(100); // FIXME TD-er: See https://github.com/letscontrolit/ESPEasy/issues/1987#issuecomment-451644424 + + if (!WiFi.isConnected()) { + return; + } + processedConnect = true; wifiStatus |= ESPEASY_WIFI_CONNECTED; - delay(100); // FIXME TD-er: See https://github.com/letscontrolit/ESPEasy/issues/1987#issuecomment-451644424 ++wifi_reconnects; - if (wifiStatus < ESPEASY_WIFI_CONNECTED) { return; } - if (loglevelActiveFor(LOG_LEVEL_INFO)) { const long connect_duration = timeDiff(last_wifi_connect_attempt_moment, lastConnectMoment); String log = F("WIFI : Connected! AP: "); @@ -253,8 +256,6 @@ void processGotIP() { return; } } - processedGotIP = true; - wifiStatus |= ESPEASY_WIFI_GOT_IP; const IPAddress gw = NetworkGatewayIP(); const IPAddress subnet = NetworkSubnetMask(); const long dhcp_duration = timeDiff(lastConnectMoment, lastGetIPmoment); @@ -324,6 +325,11 @@ void processGotIP() { SaveSettings(); } logConnectionStatus(); + + if (WiFi.isConnected() && hasIPaddr()) { + processedGotIP = true; + wifiStatus |= ESPEASY_WIFI_GOT_IP; + } } // A client disconnected from the AP on this node. diff --git a/src/src/DataStructs/SettingsStruct.h b/src/src/DataStructs/SettingsStruct.h index 07e13b998..8a9b76589 100644 --- a/src/src/DataStructs/SettingsStruct.h +++ b/src/src/DataStructs/SettingsStruct.h @@ -176,7 +176,7 @@ class SettingsStruct_tmpl uint32_t ResetFactoryDefaultPreference; // Do not clear this one in the clearAll() uint32_t I2C_clockSpeed; uint16_t WebserverPort; - uint16_t unused; + uint16_t unused = 0; // FIXME @TD-er: As discussed in #1292, the CRC for the settings is now disabled. // make sure crc is the last value in the struct diff --git a/src/src/Globals/ESPEasyWiFiEvent.cpp b/src/src/Globals/ESPEasyWiFiEvent.cpp index 943aeaeea..ad3de1412 100644 --- a/src/src/Globals/ESPEasyWiFiEvent.cpp +++ b/src/src/Globals/ESPEasyWiFiEvent.cpp @@ -29,6 +29,7 @@ bool channel_changed = false; WiFiDisconnectReason lastDisconnectReason = WIFI_DISCONNECT_REASON_UNSPECIFIED; unsigned long lastConnectMoment = 0; unsigned long lastDisconnectMoment = 0; +unsigned long lastWiFiResetMoment = 0; unsigned long lastGetIPmoment = 0; unsigned long lastGetScanMoment = 0; unsigned long lastConnectedDuration = 0; diff --git a/src/src/Globals/ESPEasyWiFiEvent.h b/src/src/Globals/ESPEasyWiFiEvent.h index d78cf38d6..47de5027b 100644 --- a/src/src/Globals/ESPEasyWiFiEvent.h +++ b/src/src/Globals/ESPEasyWiFiEvent.h @@ -84,6 +84,7 @@ extern bool channel_changed; extern WiFiDisconnectReason lastDisconnectReason; extern unsigned long lastConnectMoment; extern unsigned long lastDisconnectMoment; +extern unsigned long lastWiFiResetMoment; extern unsigned long lastGetIPmoment; extern unsigned long lastGetScanMoment; extern unsigned long lastConnectedDuration;