Skip to content

Commit 208ae9b

Browse files
committed
Wait for connection. Implement timeout for ESP8266
- On ESP8266 the call will block execution up to 3s
1 parent 0cac574 commit 208ae9b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/Arduino_WiFiConnectionHandler.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323

2424
#ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */
2525

26+
/******************************************************************************
27+
CONSTANTS
28+
******************************************************************************/
29+
#if defined(ARDUINO_ARCH_ESP8266)
30+
static int const ESP_WIFI_CONNECTION_TIMEOUT = 3000;
31+
#elif defined(ARDUINO_ARCH_ESP32)
32+
static int const ESP_WIFI_CONNECTION_TIMEOUT = 1000;
33+
#endif
34+
2635
/******************************************************************************
2736
CTOR/DTOR
2837
******************************************************************************/
@@ -93,12 +102,14 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
93102
if (WiFi.status() != WL_CONNECTED)
94103
{
95104
WiFi.begin(_ssid, _pass);
96-
#if defined(ARDUINO_ARCH_ESP8266)
97-
WiFi.waitForConnectResult();
98-
#endif
99-
#if defined(ARDUINO_ARCH_ESP32)
100-
WiFi.waitForConnectResult(1000);
105+
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
106+
/* Wait connection otherwise board won't connect */
107+
unsigned long start = millis();
108+
while((WiFi.status() != WL_CONNECTED) && (millis() - start) < ESP_WIFI_CONNECTION_TIMEOUT) {
109+
delay(100);
110+
}
101111
#endif
112+
102113
}
103114

104115
if (WiFi.status() != NETWORK_CONNECTED)

0 commit comments

Comments
 (0)