[GH-ISSUE #280] Calling WiFi.disconnect() seems to wipe WiFiManagers SSID/Password settings? #237

Closed
opened 2026-02-28 01:24:14 +03:00 by kerem · 5 comments
Owner

Originally created by @horendus on GitHub (Jan 7, 2017).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/280

Hey

I have been using WiFiManager for a project, fantastic library!

I have however run into a problem, my project needs to be able to connect/disconnect wifi at will.

Connecting is fine using wifiManager.autoConnect(); however if I call WiFi.disconnect(); at any point I cannot re connect to my wifi using wifiManager.autoConnect(); unless I go through the portal again and re enter my wifi detail.

The only think I can think of is WiFi.disconnect(); is wiping the saved wifi credentials for the library.

Is there another way I should be disconnecting from wifi? WiFiManager doesnt have a disconnect function.

Thanks for any help you can provide!

Originally created by @horendus on GitHub (Jan 7, 2017). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/280 Hey I have been using WiFiManager for a project, fantastic library! I have however run into a problem, my project needs to be able to connect/disconnect wifi at will. Connecting is fine using wifiManager.autoConnect(); however if I call WiFi.disconnect(); at any point I cannot re connect to my wifi using wifiManager.autoConnect(); unless I go through the portal again and re enter my wifi detail. The only think I can think of is WiFi.disconnect(); is wiping the saved wifi credentials for the library. Is there another way I should be disconnecting from wifi? WiFiManager doesnt have a disconnect function. Thanks for any help you can provide!
kerem closed this issue 2026-02-28 01:24:14 +03:00
Author
Owner

@ortegafernando commented on GitHub (Jan 12, 2017):

See this: https://github.com/tzapu/WiFiManager/issues/242

It is about persistent(false)

<!-- gh-comment-id:272280194 --> @ortegafernando commented on GitHub (Jan 12, 2017): See this: https://github.com/tzapu/WiFiManager/issues/242 It is about persistent(false)
Author
Owner

@tablatronix commented on GitHub (Jan 12, 2017):

persistent on or off ?

/**
 * Disconnect from the network
 * @param wifioff
 * @return  one value of wl_status_t enum
 */
bool ESP8266WiFiSTAClass::disconnect(bool wifioff) {
    bool ret;
    struct station_config conf;
    *conf.ssid = 0;
    *conf.password = 0;

    ETS_UART_INTR_DISABLE();
    if(WiFi._persistent) {
        wifi_station_set_config(&conf);
    } else {
        wifi_station_set_config_current(&conf);
    }
    ret = wifi_station_disconnect();
    ETS_UART_INTR_ENABLE();

    if(wifioff) {
        WiFi.enableSTA(false);
    }

    return ret;
}
<!-- gh-comment-id:272311683 --> @tablatronix commented on GitHub (Jan 12, 2017): persistent on or off ? ``` /** * Disconnect from the network * @param wifioff * @return one value of wl_status_t enum */ bool ESP8266WiFiSTAClass::disconnect(bool wifioff) { bool ret; struct station_config conf; *conf.ssid = 0; *conf.password = 0; ETS_UART_INTR_DISABLE(); if(WiFi._persistent) { wifi_station_set_config(&conf); } else { wifi_station_set_config_current(&conf); } ret = wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); if(wifioff) { WiFi.enableSTA(false); } return ret; } ```
Author
Owner

@ortegafernando commented on GitHub (Jan 13, 2017):

From this, for me is clear (almost ;) : https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/generic-class.md
that explain this:

"""""
Module is able to reconnect to last used Wi-Fi network on power up or reset basing on settings stored in specific sectors of flash memory. By default these settings are written to flash each time they are used in functions like WiFi.begin(ssid, password). This happens no matter if SSID or password has been actually changed.
This might result in some wear of flash memory depending on how often such functions are called.
Setting persistent to false will get SSID / password written to flash only if currently used values do not match what is already stored in flash.
Please note that functions WiFi.disconnect or WiFi.softAPdisconnect reset currently used SSID / password. If persistent is set to false, then using these functions will not affect SSID / password stored in flash.
To learn more about this functionality, and why it has been introduced, check issue report #1054
""""

So with persistent(true) Wifi.disconnect ERASES the SSID and password (usefull if you want to sell a module and don't want anyone to get your SSID and password from flash memory).

With persistent(false) Wifi.disconnect doesn't erase anything.

Meanwhile I have forked @kentaylor library and added option to use persistent(false), I will try to use it and see if there is any problema (I have still not use it)

I think will be the best to have persistent = false as default value in this library.

Regards

<!-- gh-comment-id:272431694 --> @ortegafernando commented on GitHub (Jan 13, 2017): From this, for me is clear (almost ;) : https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/generic-class.md that explain this: """"" Module is able to reconnect to last used Wi-Fi network on power up or reset basing on settings stored in specific sectors of flash memory. By default these settings are written to flash each time they are used in functions like WiFi.begin(ssid, password). This happens no matter if SSID or password has been actually changed. This might result in some wear of flash memory depending on how often such functions are called. Setting persistent to false will get SSID / password written to flash only if currently used values do not match what is already stored in flash. Please note that functions **WiFi.disconnect** or WiFi.softAPdisconnect reset currently used SSID / password. If persistent is set to false, then using these functions will not affect SSID / password stored in flash. To learn more about this functionality, and why it has been introduced, check issue report #1054 """" So with persistent(true) Wifi.disconnect ERASES the SSID and password (usefull if you want to sell a module and don't want anyone to get your SSID and password from flash memory). With persistent(false) Wifi.disconnect doesn't erase anything. Meanwhile I have forked @kentaylor library and added option to use persistent(false), I will try to use it and see if there is any problema (I have still not use it) I think will be the best to have persistent = false as default value in this library. Regards
Author
Owner

@tablatronix commented on GitHub (Jan 13, 2017):

I suppose the question is why is your sketch calling disconnect to begin with?
I think you can simply change modes, but you might be doing something like turning radio on and off, not sure.

I also thought he made his branch to specifically use persistence to store the creds in eeprom, original wifi manager was manually doing this.

I do not really understand how you are supposed to save the credentials to eeprom if not using persistent, that is the whole point of it. Nor do I understand what issue you are trying to solve.

You can disconnect from wifi by changing the wifi mode or you can write your own disconnect function
and call wifi_station_disconnect() directly then WiFi.enableSTA(false)

or simply wrap your disconnects

void mydisconnect{
WiFi.persistent = false;
WiFi.disconnect;
WiFi.persistent = true;
}

but i am not sure if this wipes your current config, so calling begin again might not work, might need to test that

<!-- gh-comment-id:272458858 --> @tablatronix commented on GitHub (Jan 13, 2017): I suppose the question is why is your sketch calling disconnect to begin with? I think you can simply change modes, but you might be doing something like turning radio on and off, not sure. I also thought he made his branch to specifically use persistence to store the creds in eeprom, original wifi manager was manually doing this. I do not really understand how you are supposed to save the credentials to eeprom if not using persistent, that is the whole point of it. Nor do I understand what issue you are trying to solve. You can disconnect from wifi by changing the wifi mode or you can write your own disconnect function and call `wifi_station_disconnect()` directly then `WiFi.enableSTA(false)` or simply wrap your disconnects ``` void mydisconnect{ WiFi.persistent = false; WiFi.disconnect; WiFi.persistent = true; } ``` but i am not sure if this wipes your current config, so calling begin again might not work, might need to test that
Author
Owner

@azevedo-manuel commented on GitHub (Jan 4, 2018):

Yes, I confirm this and the workaround by @tablatronix works with a minor change:

WiFi.persistent(false);      
WiFi.disconnect();          
WiFi.persistent(true);

Also answerting @tablatronix, I call this function when a user presses a button to call Wi-Fi Manager. If the ESP8266 is still connected to a network, without WiFi.disconnect() it would still be connected to that network and it would serve the Wi-Fi Manager webpage if a user connected to it.

<!-- gh-comment-id:355432922 --> @azevedo-manuel commented on GitHub (Jan 4, 2018): Yes, I confirm this and the workaround by @tablatronix works with a minor change: ``` WiFi.persistent(false); WiFi.disconnect(); WiFi.persistent(true); ``` Also answerting @tablatronix, I call this function when a user presses a button to call Wi-Fi Manager. If the ESP8266 is still connected to a network, without `WiFi.disconnect()` it would still be connected to that network and it would serve the Wi-Fi Manager webpage if a user connected to it.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#237
No description provided.