From 8fa27244877d9fb8e6ab260f3c8ce7e32a1d4f50 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Wed, 3 Jan 2024 09:42:26 +0100 Subject: [PATCH] WiFi.disconnect() "aligned with Aduino.cc". waiting for status change. WiFi.disconnect renamed to disconnectAsync new WiFi.disconnect waits for status change --- libraries/WiFi/src/WiFiSTA.cpp | 27 ++++++++++++++++++++++++++- libraries/WiFi/src/WiFiSTA.h | 3 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/libraries/WiFi/src/WiFiSTA.cpp b/libraries/WiFi/src/WiFiSTA.cpp index bf80d37393b..fb8326d6377 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 07f01922cef..62f419bca8b 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();