From e9d5e3673855cc38e9b2ba1d6818b64d07145b67 Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Thu, 19 Jun 2025 19:18:55 -0400 Subject: [PATCH] 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 7db4ff6444df0a5271d3d5df49ebfb54024ac18f. * Last mess cleanups (hopefully) * Undid trunk.yaml changes * Trunk format --------- Co-authored-by: Ben Meadors Co-authored-by: Tom Fifield --- src/mesh/wifi/WiFiAPClient.cpp | 35 ++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/mesh/wifi/WiFiAPClient.cpp b/src/mesh/wifi/WiFiAPClient.cpp index 945460c28..115817aab 100644 --- a/src/mesh/wifi/WiFiAPClient.cpp +++ b/src/mesh/wifi/WiFiAPClient.cpp @@ -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 \ No newline at end of file