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+ #endif
32+
2633/* *****************************************************************************
2734 CTOR/DTOR
2835 ******************************************************************************/
@@ -54,10 +61,11 @@ unsigned long WiFiConnectionHandler::getTime()
5461
5562NetworkConnectionState WiFiConnectionHandler::update_handleInit ()
5663{
57- #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
5864#if !defined(__AVR__)
5965 Debug.print (DBG_INFO, F (" WiFi.status(): %d" ), WiFi.status ());
6066#endif
67+
68+ #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
6169 if (WiFi.status () == NETWORK_HARDWARE_ERROR)
6270 {
6371#if !defined(__AVR__)
@@ -80,26 +88,26 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
8088 delay (5000 );
8189 }
8290#endif
83-
8491#else
85- Debug.print (DBG_INFO, F (" WiFi status ESP: %d" ), WiFi.status ());
86- WiFi.disconnect ();
87- delay (300 );
88- WiFi.begin (_ssid, _pass);
89- delay (1000 );
92+ WiFi.mode (WIFI_STA);
9093#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
91-
9294 return NetworkConnectionState::CONNECTING;
9395}
9496
9597NetworkConnectionState WiFiConnectionHandler::update_handleConnecting ()
9698{
97- #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
9899 if (WiFi.status () != WL_CONNECTED)
99100 {
100101 WiFi.begin (_ssid, _pass);
102+ #if defined(ARDUINO_ARCH_ESP8266)
103+ /* Wait connection otherwise board won't connect */
104+ unsigned long start = millis ();
105+ while ((WiFi.status () != WL_CONNECTED) && (millis () - start) < ESP_WIFI_CONNECTION_TIMEOUT) {
106+ delay (100 );
107+ }
108+ #endif
109+
101110 }
102- #endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
103111
104112 if (WiFi.status () != NETWORK_CONNECTED)
105113 {
0 commit comments