diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 9a5d6961..79758066 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -155,7 +155,11 @@ typedef void (*OnNetworkEventCallback)(); static unsigned int const CHECK_INTERVAL_TABLE[] = { /* INIT */ 100, +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) + /* CONNECTING */ 2000, +#else /* CONNECTING */ 500, +#endif /* CONNECTED */ 10000, /* DISCONNECTING */ 100, /* DISCONNECTED */ 1000, diff --git a/src/Arduino_WiFiConnectionHandler.cpp b/src/Arduino_WiFiConnectionHandler.cpp index cc6727cf..3eadcca2 100644 --- a/src/Arduino_WiFiConnectionHandler.cpp +++ b/src/Arduino_WiFiConnectionHandler.cpp @@ -23,6 +23,13 @@ #ifdef BOARD_HAS_WIFI /* Only compile if the board has WiFi */ +/****************************************************************************** + CONSTANTS + ******************************************************************************/ +#if defined(ARDUINO_ARCH_ESP8266) +static int const ESP_WIFI_CONNECTION_TIMEOUT = 3000; +#endif + /****************************************************************************** CTOR/DTOR ******************************************************************************/ @@ -54,10 +61,11 @@ unsigned long WiFiConnectionHandler::getTime() NetworkConnectionState WiFiConnectionHandler::update_handleInit() { -#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) #if !defined(__AVR__) Debug.print(DBG_INFO, F("WiFi.status(): %d"), WiFi.status()); #endif + +#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) if (WiFi.status() == NETWORK_HARDWARE_ERROR) { #if !defined(__AVR__) @@ -80,26 +88,26 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit() delay(5000); } #endif - #else - Debug.print(DBG_INFO, F("WiFi status ESP: %d"), WiFi.status()); - WiFi.disconnect(); - delay(300); - WiFi.begin(_ssid, _pass); - delay(1000); + WiFi.mode(WIFI_STA); #endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */ - return NetworkConnectionState::CONNECTING; } NetworkConnectionState WiFiConnectionHandler::update_handleConnecting() { -#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) if (WiFi.status() != WL_CONNECTED) { WiFi.begin(_ssid, _pass); +#if defined(ARDUINO_ARCH_ESP8266) + /* Wait connection otherwise board won't connect */ + unsigned long start = millis(); + while((WiFi.status() != WL_CONNECTED) && (millis() - start) < ESP_WIFI_CONNECTION_TIMEOUT) { + delay(100); + } +#endif + } -#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */ if (WiFi.status() != NETWORK_CONNECTED) {