Skip to content

Commit e728aad

Browse files
committed
WiFi.disconnect() "aligned with Aduino.cc". waiting for status change.
WiFi.disconnect renamed to disconnectAsync new WiFi.disconnect waits for status change
1 parent b2e7338 commit e728aad

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Diff for: libraries/WiFi/src/WiFiSTA.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ bool WiFiSTAClass::reconnect()
367367
* @param eraseap `true` to erase the AP configuration from the NVS memory.
368368
* @return `true` when successful.
369369
*/
370-
bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap)
370+
bool WiFiSTAClass::disconnectAsync(bool wifioff, bool eraseap)
371371
{
372372
wifi_config_t conf;
373373
wifi_sta_config(&conf);
@@ -391,6 +391,32 @@ bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap)
391391
return false;
392392
}
393393

394+
/**
395+
* Disconnect from the network.
396+
* @param wifioff `true` to turn the Wi-Fi radio off.
397+
* @param eraseap `true` to erase the AP configuration from the NVS memory.
398+
* @param timeoutLength timeout to wait for status change
399+
* @return `true` when successful.
400+
*/
401+
bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap, unsigned long timeoutLength)
402+
{
403+
if (!disconnectAsync(wifioff, eraseap)) {
404+
return false;
405+
}
406+
if (!timeoutLength) {
407+
return true;
408+
}
409+
unsigned long start = millis();
410+
uint32_t delay_time = timeoutLength < 5 ? timeoutLength : 5;
411+
while ((WiFiGenericClass::getStatusBits() & STA_CONNECTED_BIT) != 0) {
412+
if((millis() - start) >= timeoutLength){
413+
return false;
414+
}
415+
delay(delay_time);
416+
}
417+
return true;
418+
}
419+
394420
/**
395421
* @brief Reset WiFi settings in NVS to default values.
396422
* @return true if erase succeeded

Diff for: libraries/WiFi/src/WiFiSTA.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class WiFiSTAClass
6161
bool bandwidth(wifi_bandwidth_t bandwidth);
6262

6363
bool reconnect();
64-
bool disconnect(bool wifioff = false, bool eraseap = false);
64+
bool disconnectAsync(bool wifioff = false, bool eraseap = false);
65+
bool disconnect(bool wifioff = false, bool eraseap = false, unsigned long timeoutLength = 3000);
6566
bool eraseAP(void);
6667

6768
bool isConnected();

0 commit comments

Comments
 (0)