Skip to content

Commit 1631294

Browse files
dav1901d-a-v
authored andcommitted
Add timeout to STA::waitForConnectResult (#5371)
1 parent e81bb6f commit 1631294

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

doc/esp8266wifi/station-class.rst

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Function returns one of the following connection statuses:
254254
- ``WL_CONNECT_FAILED`` if password is incorrect
255255
- ``WL_IDLE_STATUS`` when Wi-Fi is in process of changing between statuses
256256
- ``WL_DISCONNECTED`` if module is not configured in station mode
257+
- ``-1`` on timeout
257258

258259
Configuration
259260
~~~~~~~~~~~~~

libraries/ESP8266AVRISP/examples/Arduino_Wifi_AVRISP/Arduino_Wifi_AVRISP.ino

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ void setup() {
2424

2525
WiFi.mode(WIFI_STA);
2626
WiFi.begin(ssid, pass);
27-
while (WiFi.waitForConnectResult() != WL_CONNECTED);
27+
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
28+
WiFi.begin(ssid, pass);
29+
Serial.println("WiFi failed, retrying.");
30+
}
2831

2932
MDNS.begin(host);
3033
MDNS.addService("avrisp", "tcp", port);

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "ESP8266WiFi.h"
2626
#include "ESP8266WiFiGeneric.h"
2727
#include "ESP8266WiFiSTA.h"
28+
#include "PolledTimeout.h"
2829

2930
#include "c_types.h"
3031
#include "ets_sys.h"
@@ -441,17 +442,22 @@ bool ESP8266WiFiSTAClass::getAutoReconnect() {
441442
/**
442443
* Wait for WiFi connection to reach a result
443444
* returns the status reached or disconnect if STA is off
444-
* @return wl_status_t
445+
* @return wl_status_t or -1 on timeout
445446
*/
446-
uint8_t ESP8266WiFiSTAClass::waitForConnectResult() {
447+
int8_t ESP8266WiFiSTAClass::waitForConnectResult(unsigned long timeoutLength) {
447448
//1 and 3 have STA enabled
448449
if((wifi_get_opmode() & 1) == 0) {
449450
return WL_DISCONNECTED;
450451
}
451-
while(status() == WL_DISCONNECTED) {
452-
delay(100);
452+
using esp8266::polledTimeout::oneShot;
453+
oneShot timeout(timeoutLength); // number of milliseconds to wait before returning timeout error
454+
while(!timeout) {
455+
yield();
456+
if(status() != WL_DISCONNECTED) {
457+
return status();
458+
}
453459
}
454-
return status();
460+
return -1; // -1 indicates timeout
455461
}
456462

457463
/**

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ESP8266WiFiSTAClass {
5757
bool setAutoReconnect(bool autoReconnect);
5858
bool getAutoReconnect();
5959

60-
uint8_t waitForConnectResult();
60+
int8_t waitForConnectResult(unsigned long timeoutLength = 60000);
6161

6262
// STA network info
6363
IPAddress localIP();

0 commit comments

Comments
 (0)