From a35f474c0d1a29e9302354872a12150e54d08fb9 Mon Sep 17 00:00:00 2001 From: m-RNA Date: Sun, 21 Dec 2025 23:55:20 +0800 Subject: [PATCH 1/2] Optimize WiFi connection time by using WIFI_FAST_SCAN (~2000ms improvement) --- components/wifiManager/wifiManager/wifiManager.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/components/wifiManager/wifiManager/wifiManager.cpp b/components/wifiManager/wifiManager/wifiManager.cpp index 1fe72e7..0a07d41 100644 --- a/components/wifiManager/wifiManager/wifiManager.cpp +++ b/components/wifiManager/wifiManager/wifiManager.cpp @@ -78,10 +78,10 @@ void WiFiManager::SetCredentials(const char *ssid, const char *password) _wifi_cfg.sta.pmf_cfg.capable = false; _wifi_cfg.sta.pmf_cfg.required = false; - // IMPORTANT: Set scan method to ALL channels - _wifi_cfg.sta.scan_method = WIFI_ALL_CHANNEL_SCAN; + // OPTIMIZATION: Use fast scan instead of all channel scan for quicker connection + _wifi_cfg.sta.scan_method = WIFI_FAST_SCAN; _wifi_cfg.sta.bssid_set = 0; // Don't use specific BSSID - _wifi_cfg.sta.channel = 0; // Scan all channels + _wifi_cfg.sta.channel = 0; // Auto channel detection // Additional settings that might help with compatibility _wifi_cfg.sta.listen_interval = 0; // Default listen interval @@ -102,7 +102,7 @@ void WiFiManager::ConnectWithHardcodedCredentials() { SystemEvent event = {EventSource::WIFI, WiFiState_e::WiFiState_ReadyToConnect}; this->SetCredentials(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD); - esp_wifi_stop(); + // Skip esp_wifi_stop() on first connection - WiFi not started yet ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &_wifi_cfg)); @@ -112,11 +112,12 @@ void WiFiManager::ConnectWithHardcodedCredentials() event.value = WiFiState_e::WiFiState_Connecting; xQueueSend(this->eventQueue, &event, 10); + // Use shorter timeout for faster startup - 8 seconds should be enough for most networks EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, - portMAX_DELAY); + pdMS_TO_TICKS(8000)); /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually * happened. */ @@ -192,7 +193,7 @@ void WiFiManager::ConnectWithStoredCredentials() WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, - pdMS_TO_TICKS(30000)); // 30 second timeout instead of portMAX_DELAY + pdMS_TO_TICKS(10000)); // 10 second timeout for faster failover if (bits & WIFI_CONNECTED_BIT) { ESP_LOGI(WIFI_MANAGER_TAG, "connected to ap SSID:%s", From 1131bd2448495e3c0637f8cc7ee0cd2320cfab18 Mon Sep 17 00:00:00 2001 From: m-RNA Date: Mon, 22 Dec 2025 00:09:42 +0800 Subject: [PATCH 2/2] Check whether WIFI is open before closing --- components/wifiManager/wifiManager/wifiManager.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/wifiManager/wifiManager/wifiManager.cpp b/components/wifiManager/wifiManager/wifiManager.cpp index 0a07d41..b5ceb6c 100644 --- a/components/wifiManager/wifiManager/wifiManager.cpp +++ b/components/wifiManager/wifiManager/wifiManager.cpp @@ -102,7 +102,12 @@ void WiFiManager::ConnectWithHardcodedCredentials() { SystemEvent event = {EventSource::WIFI, WiFiState_e::WiFiState_ReadyToConnect}; this->SetCredentials(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD); - // Skip esp_wifi_stop() on first connection - WiFi not started yet + + wifi_mode_t mode; + if (esp_wifi_get_mode(&mode) == ESP_OK) { + esp_wifi_stop(); + } + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &_wifi_cfg));