Skip to content

Commit 80f543c

Browse files
authored
Merge pull request #89 from pennam/esp-reconnect-2
ESP: WiFi: Fix (re)connection
2 parents 917cf13 + 91f7345 commit 80f543c

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/Arduino_ConnectionHandler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ typedef void (*OnNetworkEventCallback)();
155155
static unsigned int const CHECK_INTERVAL_TABLE[] =
156156
{
157157
/* INIT */ 100,
158+
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
159+
/* CONNECTING */ 2000,
160+
#else
158161
/* CONNECTING */ 500,
162+
#endif
159163
/* CONNECTED */ 10000,
160164
/* DISCONNECTING */ 100,
161165
/* DISCONNECTED */ 1000,

src/Arduino_WiFiConnectionHandler.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
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

5562
NetworkConnectionState 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

9597
NetworkConnectionState 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

Comments
 (0)