Skip to content

Commit

Permalink
WiFi.disconnect() "aligned with Aduino.cc". waiting for status change. (
Browse files Browse the repository at this point in the history
#9062)

WiFi.disconnect renamed to disconnectAsync

new WiFi.disconnect waits for status change
  • Loading branch information
JAndrassy authored Jan 18, 2024
1 parent b3de161 commit 4766608
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
27 changes: 26 additions & 1 deletion libraries/WiFi/src/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion libraries/WiFi/src/WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4766608

Please sign in to comment.