Skip to content

Add timeout to waitForConnectResult #5371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/esp8266wifi/station-class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Function returns one of the following connection statuses:
- ``WL_CONNECT_FAILED`` if password is incorrect
- ``WL_IDLE_STATUS`` when Wi-Fi is in process of changing between statuses
- ``WL_DISCONNECTED`` if module is not configured in station mode
- ``-1`` on timeout

Configuration
~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ void setup() {

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.waitForConnectResult() != WL_CONNECTED);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.println("WiFi failed, retrying.");
}

MDNS.begin(host);
MDNS.addService("avrisp", "tcp", port);
Expand Down
16 changes: 11 additions & 5 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ESP8266WiFi.h"
#include "ESP8266WiFiGeneric.h"
#include "ESP8266WiFiSTA.h"
#include "PolledTimeout.h"

#include "c_types.h"
#include "ets_sys.h"
Expand Down Expand Up @@ -441,17 +442,22 @@ bool ESP8266WiFiSTAClass::getAutoReconnect() {
/**
* Wait for WiFi connection to reach a result
* returns the status reached or disconnect if STA is off
* @return wl_status_t
* @return wl_status_t or -1 on timeout
*/
uint8_t ESP8266WiFiSTAClass::waitForConnectResult() {
int8_t ESP8266WiFiSTAClass::waitForConnectResult(unsigned long timeoutLength) {
//1 and 3 have STA enabled
if((wifi_get_opmode() & 1) == 0) {
return WL_DISCONNECTED;
}
while(status() == WL_DISCONNECTED) {
delay(100);
using esp8266::polledTimeout::oneShot;
oneShot timeout(timeoutLength); // number of milliseconds to wait before returning timeout error
while(!timeout) {
yield();
if(status() != WL_DISCONNECTED) {
return status();
}
}
return status();
return -1; // -1 indicates timeout
}

/**
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ESP8266WiFiSTAClass {
bool setAutoReconnect(bool autoReconnect);
bool getAutoReconnect();

uint8_t waitForConnectResult();
int8_t waitForConnectResult(unsigned long timeoutLength = 60000);

// STA network info
IPAddress localIP();
Expand Down