[WiFi] Reduce wifi reset calls and call full wifi init at reset

This commit is contained in:
Gijs Noorlander
2020-07-09 21:37:54 +02:00
parent 30eb0bccb8
commit db869fcb19
10 changed files with 56 additions and 30 deletions
+6 -1
View File
@@ -9,5 +9,10 @@
"url": "https://github.com/jrowberg/i2cdevlib.git"
},
"frameworks": "arduino",
"platforms": "atmelavr"
"platforms": "atmelavr",
"dependencies": [
{
"name": "Wire"
}
]
}
+1 -18
View File
@@ -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!"));
+1 -1
View File
@@ -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];
}
+1 -1
View File
@@ -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
+30 -1
View File
@@ -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;
}
// ********************************************************************************
+1
View File
@@ -22,6 +22,7 @@ bool WiFiConnected();
void WiFiConnectRelaxed();
bool prepareWiFi();
void resetWiFi();
void initWiFi();
void WifiDisconnect();
void WifiScan(bool async, bool quick);
void WifiScan();
+13 -7
View File
@@ -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.
+1 -1
View File
@@ -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
+1
View File
@@ -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;
+1
View File
@@ -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;