Skip to content

Commit 347d1d6

Browse files
authored
Merge pull request #25 from m-RNA/Optimize-WiFi-connection-time
Optimize WiFi connection time by using WIFI_FAST_SCAN
2 parents faef491 + 1131bd2 commit 347d1d6

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

components/wifiManager/wifiManager/wifiManager.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ void WiFiManager::SetCredentials(const char *ssid, const char *password)
7878
_wifi_cfg.sta.pmf_cfg.capable = false;
7979
_wifi_cfg.sta.pmf_cfg.required = false;
8080

81-
// IMPORTANT: Set scan method to ALL channels
82-
_wifi_cfg.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
81+
// OPTIMIZATION: Use fast scan instead of all channel scan for quicker connection
82+
_wifi_cfg.sta.scan_method = WIFI_FAST_SCAN;
8383
_wifi_cfg.sta.bssid_set = 0; // Don't use specific BSSID
84-
_wifi_cfg.sta.channel = 0; // Scan all channels
84+
_wifi_cfg.sta.channel = 0; // Auto channel detection
8585

8686
// Additional settings that might help with compatibility
8787
_wifi_cfg.sta.listen_interval = 0; // Default listen interval
@@ -102,7 +102,12 @@ void WiFiManager::ConnectWithHardcodedCredentials()
102102
{
103103
SystemEvent event = {EventSource::WIFI, WiFiState_e::WiFiState_ReadyToConnect};
104104
this->SetCredentials(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
105-
esp_wifi_stop();
105+
106+
wifi_mode_t mode;
107+
if (esp_wifi_get_mode(&mode) == ESP_OK) {
108+
esp_wifi_stop();
109+
}
110+
106111
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
107112
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &_wifi_cfg));
108113

@@ -112,11 +117,12 @@ void WiFiManager::ConnectWithHardcodedCredentials()
112117
event.value = WiFiState_e::WiFiState_Connecting;
113118
xQueueSend(this->eventQueue, &event, 10);
114119

120+
// Use shorter timeout for faster startup - 8 seconds should be enough for most networks
115121
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
116122
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
117123
pdFALSE,
118124
pdFALSE,
119-
portMAX_DELAY);
125+
pdMS_TO_TICKS(8000));
120126

121127
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
122128
* happened. */
@@ -192,7 +198,7 @@ void WiFiManager::ConnectWithStoredCredentials()
192198
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
193199
pdFALSE,
194200
pdFALSE,
195-
pdMS_TO_TICKS(30000)); // 30 second timeout instead of portMAX_DELAY
201+
pdMS_TO_TICKS(10000)); // 10 second timeout for faster failover
196202
if (bits & WIFI_CONNECTED_BIT)
197203
{
198204
ESP_LOGI(WIFI_MANAGER_TAG, "connected to ap SSID:%s",

0 commit comments

Comments
 (0)