mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-03 07:02:28 +03:00
Add timeout to STA::waitForConnectResult (#5371)
This commit is contained in:
parent
e81bb6f57c
commit
16312949c9
@ -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
|
||||
~~~~~~~~~~~~~
|
||||
|
@ -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);
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "ESP8266WiFi.h"
|
||||
#include "ESP8266WiFiGeneric.h"
|
||||
#include "ESP8266WiFiSTA.h"
|
||||
#include "PolledTimeout.h"
|
||||
|
||||
#include "c_types.h"
|
||||
#include "ets_sys.h"
|
||||
@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user