diff --git a/libraries/WiFi/src/WiFiSTA.cpp b/libraries/WiFi/src/WiFiSTA.cpp index fab3bce6563..02e930943c3 100644 --- a/libraries/WiFi/src/WiFiSTA.cpp +++ b/libraries/WiFi/src/WiFiSTA.cpp @@ -367,7 +367,7 @@ bool WiFiSTAClass::reconnect() * @param eraseap `true` to erase the AP configuration from the NVS memory. * @return `true` when successful. */ -bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap) +bool WiFiSTAClass::disconnectAsync(bool wifioff, bool eraseap) { wifi_config_t conf; wifi_sta_config(&conf); @@ -391,6 +391,31 @@ bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap) return false; } +/** + * Disconnect from the network. + * @param wifioff `true` to turn the Wi-Fi radio off. + * @param eraseap `true` to erase the AP configuration from the NVS memory. + * @param timeoutLength timeout to wait for status change + * @return `true` when successful. + */ +bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap, unsigned long timeoutLength) +{ + if (!disconnectAsync(wifioff, eraseap)) { + return false; + } + if (!timeoutLength) { + return true; + } + const unsigned long start = millis(); + while ((WiFiGenericClass::getStatusBits() & STA_CONNECTED_BIT) != 0) { + if((millis() - start) >= timeoutLength){ + return false; + } + delay(2); + } + return true; +} + /** * @brief Reset WiFi settings in NVS to default values. * @return true if erase succeeded diff --git a/libraries/WiFi/src/WiFiSTA.h b/libraries/WiFi/src/WiFiSTA.h index 4fbb789aa04..f66a9e3aa0c 100644 --- a/libraries/WiFi/src/WiFiSTA.h +++ b/libraries/WiFi/src/WiFiSTA.h @@ -61,7 +61,8 @@ class WiFiSTAClass bool bandwidth(wifi_bandwidth_t bandwidth); bool reconnect(); - bool disconnect(bool wifioff = false, bool eraseap = false); + bool disconnectAsync(bool wifioff = false, bool eraseap = false); + bool disconnect(bool wifioff = false, bool eraseap = false, unsigned long timeoutLength = 100); bool eraseAP(void); bool isConnected();