Skip to content

WiFi.disconnect() "aligned with Aduino.cc". waiting for status change. #9062

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 1 commit into from
Jan 18, 2024
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
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