Skip to content

Commit

Permalink
Don't disconnect WiFi when we're actively reconnecting (#3026)
Browse files Browse the repository at this point in the history
WiFiEvents may happen asynchronously
  • Loading branch information
GUVWAF authored Dec 21, 2023
1 parent d88baea commit db8f8db
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/mesh/wifi/WiFiAPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ bool APStartupComplete = 0;

unsigned long lastrun_ntp = 0;

bool needReconnect = true; // If we create our reconnector, run it once at the beginning
bool needReconnect = true; // If we create our reconnector, run it once at the beginning
bool isReconnecting = false; // If we are currently reconnecting

WiFiUDP syslogClient;
Syslog syslog(syslogClient);
Expand Down Expand Up @@ -115,6 +116,7 @@ static int32_t reconnectWiFi()
wifiPsw = NULL;

needReconnect = false;
isReconnecting = true;

// Make sure we clear old connection credentials
#ifdef ARCH_ESP32
Expand All @@ -129,6 +131,7 @@ static int32_t reconnectWiFi()
if (!WiFi.isConnected()) {
WiFi.begin(wifiName, wifiPsw);
}
isReconnecting = false;
}

#ifndef DISABLE_NTP
Expand Down Expand Up @@ -277,10 +280,12 @@ static void WiFiEvent(WiFiEvent_t event)
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
LOG_INFO("Disconnected from WiFi access point\n");
WiFi.disconnect(false, true);
syslog.disable();
needReconnect = true;
wifiReconnect->setIntervalFromNow(1000);
if (!isReconnecting) {
WiFi.disconnect(false, true);
syslog.disable();
needReconnect = true;
wifiReconnect->setIntervalFromNow(1000);
}
break;
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE:
LOG_INFO("Authentication mode of access point has changed\n");
Expand All @@ -294,10 +299,12 @@ static void WiFiEvent(WiFiEvent_t event)
break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
LOG_INFO("Lost IP address and IP address is reset to 0\n");
WiFi.disconnect(false, true);
syslog.disable();
needReconnect = true;
wifiReconnect->setIntervalFromNow(1000);
if (!isReconnecting) {
WiFi.disconnect(false, true);
syslog.disable();
needReconnect = true;
wifiReconnect->setIntervalFromNow(1000);
}
break;
case ARDUINO_EVENT_WPS_ER_SUCCESS:
LOG_INFO("WiFi Protected Setup (WPS): succeeded in enrollee mode\n");
Expand Down Expand Up @@ -398,4 +405,4 @@ static void WiFiEvent(WiFiEvent_t event)
uint8_t getWifiDisconnectReason()
{
return wifiDisconnectReason;
}
}

0 comments on commit db8f8db

Please sign in to comment.