Skip to content

Commit 38fe6fc

Browse files
authored
Add timeout to WiFi connection loop in WiFiMulti (esp8266#4146)
1 parent dd00db1 commit 38fe6fc

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,20 @@ wl_status_t ESP8266WiFiMulti::run(void) {
120120
delay(0);
121121

122122
if(bestNetwork.ssid) {
123-
DEBUG_WIFI_MULTI("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channal: %d (%d)\n", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
123+
DEBUG_WIFI_MULTI("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channel: %d (%d)\n", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
124124

125125
WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase, bestChannel, bestBSSID);
126126
status = WiFi.status();
127127

128-
// wait for connection or fail
129-
while(status != WL_CONNECTED && status != WL_NO_SSID_AVAIL && status != WL_CONNECT_FAILED) {
128+
static const uint32_t connectTimeout = 5000; //5s timeout
129+
130+
auto startTime = millis();
131+
// wait for connection, fail, or timeout
132+
while(status != WL_CONNECTED && status != WL_NO_SSID_AVAIL && status != WL_CONNECT_FAILED && (millis() - startTime) <= connectTimeout) {
130133
delay(10);
131134
status = WiFi.status();
132135
}
136+
133137
#ifdef DEBUG_ESP_WIFI
134138
IPAddress ip;
135139
uint8_t * mac;

0 commit comments

Comments
 (0)