Replace blocking delay for wifi reconnect with non-blocking to keep button/display interactivity (#6983)

* Update WiFiAPClient.cpp to replace blocking delay() with non-blocking

* Update WiFiAPClient.cpp - fix extra endif

* Update WiFiAPClient.cpp remove duplicate section

* Update WiFiAPClient.cpp

* Update trunk_annotate_pr.yml

* Update trunk_annotate_pr.yml

* Update trunk_check.yml

* Update trunk_check.yml

* Update trunk_format_pr.yml

* Update trunk_annotate_pr.yml

* Attempted to address comments, and fix my other mess.
Thanks for your patience.

* Revert "Update trunk_annotate_pr.yml"

This reverts commit 7db4ff6444.

* Last mess cleanups (hopefully)

* Undid trunk.yaml changes

* Trunk format

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
Co-authored-by: Tom Fifield <tom@tomfifield.net>
This commit is contained in:
Matt Smith 2025-06-19 19:18:55 -04:00 committed by GitHub
parent f71fdef3fd
commit e9d5e36738
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -46,6 +46,10 @@ uint8_t wifiDisconnectReason = 0;
// Stores our hostname
char ourHost[16];
// To replace blocking wifi connect delay with a non-blocking sleep
static unsigned long wifiReconnectStartMillis = 0;
static bool wifiReconnectPending = false;
bool APStartupComplete = 0;
unsigned long lastrun_ntp = 0;
@ -160,17 +164,30 @@ static int32_t reconnectWiFi()
#endif
LOG_INFO("Reconnecting to WiFi access point %s", wifiName);
delay(5000);
// Start the non-blocking wait for 5 seconds
wifiReconnectStartMillis = millis();
wifiReconnectPending = true;
// Do not attempt to connect yet, wait for the next invocation
return 5000; // Schedule next check soon
}
if (!WiFi.isConnected()) {
// Check if we are ready to proceed with the WiFi connection after the 5s wait
if (wifiReconnectPending) {
if (millis() - wifiReconnectStartMillis >= 5000) {
if (!WiFi.isConnected()) {
#ifdef CONFIG_IDF_TARGET_ESP32C3
WiFi.mode(WIFI_MODE_NULL);
WiFi.useStaticBuffers(true);
WiFi.mode(WIFI_STA);
WiFi.mode(WIFI_MODE_NULL);
WiFi.useStaticBuffers(true);
WiFi.mode(WIFI_STA);
#endif
WiFi.begin(wifiName, wifiPsw);
WiFi.begin(wifiName, wifiPsw);
}
isReconnecting = false;
wifiReconnectPending = false;
} else {
// Still waiting for 5s to elapse
return 100; // Check again soon
}
isReconnecting = false;
}
#ifndef DISABLE_NTP
@ -193,8 +210,6 @@ static int32_t reconnectWiFi()
if (config.network.wifi_enabled && !WiFi.isConnected()) {
#ifdef ARCH_RP2040 // (ESP32 handles this in WiFiEvent)
/* If APStartupComplete, but we're not connected, try again.
Shouldn't try again before APStartupComplete. */
needReconnect = APStartupComplete;
#endif
return 1000; // check once per second
@ -486,4 +501,4 @@ uint8_t getWifiDisconnectReason()
{
return wifiDisconnectReason;
}
#endif
#endif // HAS_WIFI