[GH-ISSUE #1679] WiFi credentials not saved after reseting/shutting down esp32. #1425

Open
opened 2026-02-28 01:30:01 +03:00 by kerem · 7 comments
Owner

Originally created by @tuadru on GitHub (Nov 21, 2023).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1679

Basic Infos

Hardware

WiFimanager Branch/Release: tried both versions 2.0.16 and 2.0.14

Esp8266/Esp32: esp32

Hardware: esp-wroom-32/esp32 devkit v1

Core Version: v3.3.5-1-g85c43024c

Description

I have tried onDemand sketch from WiFi Manager. It connects properly first time but when I reset my esp32 or turn the power off then back on it doesent connect to the network, instead I have to go into ap mode andput in credentials all over again.

Settings in IDE

Module: ESP32 Dev Module

Additional libraries:

Sketch


/**
 * OnDemandConfigPortal.ino
 * example of running the configPortal AP manually, independantly from the captiveportal
 * trigger pin will start a configPortal AP for 120 seconds then turn it off.
 * 
 */
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

// select which pin will trigger the configuration portal when set to LOW
#define TRIGGER_PIN 15

int timeout = 120; // seconds to run for

void setup() {
  WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP  
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\n Starting");
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
}

void loop() {
  // is configuration portal requested?
  if ( digitalRead(TRIGGER_PIN) == LOW) {
    WiFiManager wm;    

    //reset settings - for testing
    //wm.resetSettings();
  
    // set configportal timeout
    wm.setConfigPortalTimeout(timeout);

    if (!wm.startConfigPortal("OnDemandAP")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.restart();
      delay(5000);
    }

    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");
    

  }

  // put your main code here, to run repeatedly:
  if (WiFi.status() == WL_CONNECTED) {
  Serial.println("Works!");
  delay(1000);
  
  }
}

Debug Messages

-
Originally created by @tuadru on GitHub (Nov 21, 2023). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1679 ### Basic Infos #### Hardware WiFimanager Branch/Release: tried both versions 2.0.16 and 2.0.14 Esp8266/Esp32: esp32 Hardware: esp-wroom-32/esp32 devkit v1 Core Version: v3.3.5-1-g85c43024c ### Description I have tried onDemand sketch from WiFi Manager. It connects properly first time but when I reset my esp32 or turn the power off then back on it doesent connect to the network, instead I have to go into ap mode andput in credentials all over again. ### Settings in IDE Module: ESP32 Dev Module Additional libraries: ### Sketch ```cpp /** * OnDemandConfigPortal.ino * example of running the configPortal AP manually, independantly from the captiveportal * trigger pin will start a configPortal AP for 120 seconds then turn it off. * */ #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager // select which pin will trigger the configuration portal when set to LOW #define TRIGGER_PIN 15 int timeout = 120; // seconds to run for void setup() { WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP // put your setup code here, to run once: Serial.begin(115200); Serial.println("\n Starting"); pinMode(TRIGGER_PIN, INPUT_PULLUP); } void loop() { // is configuration portal requested? if ( digitalRead(TRIGGER_PIN) == LOW) { WiFiManager wm; //reset settings - for testing //wm.resetSettings(); // set configportal timeout wm.setConfigPortalTimeout(timeout); if (!wm.startConfigPortal("OnDemandAP")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.restart(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } // put your main code here, to run repeatedly: if (WiFi.status() == WL_CONNECTED) { Serial.println("Works!"); delay(1000); } } ``` ### Debug Messages ``` - ```
Author
Owner

@tuadru commented on GitHub (Nov 21, 2023):

I have just used basic sketch and apparently I need auto-reconnect function to use saved credentials to reconnect.
So I must somehow combine this with button press cause I would really like to still be able to turn AP mode on demand if I want to switch wifi networks or if credential changes?
P.S.
So what I would like for my project:
First time I turn on device I need to press button for esp to make AP and then connect to that AP and write in wifi credentials. Then I want esp to try and connect every time to those credentials (after power is off, after wifi wasnt working or available for some time). I dont want AP to be made automatically even if there is no wifi network I entered in past, only way to change credentials and wifi network would be for me to press button again. While I dont press button I want esp to try to connect on stored credentials every time it is reset or whatever.

<!-- gh-comment-id:1820686965 --> @tuadru commented on GitHub (Nov 21, 2023): I have just used basic sketch and apparently I need auto-reconnect function to use saved credentials to reconnect. So I must somehow combine this with button press cause I would really like to still be able to turn AP mode on demand if I want to switch wifi networks or if credential changes? P.S. So what I would like for my project: First time I turn on device I need to press button for esp to make AP and then connect to that AP and write in wifi credentials. Then I want esp to try and connect every time to those credentials (after power is off, after wifi wasnt working or available for some time). I dont want AP to be made automatically even if there is no wifi network I entered in past, only way to change credentials and wifi network would be for me to press button again. While I dont press button I want esp to try to connect on stored credentials every time it is reset or whatever.
Author
Owner

@woodenplastic commented on GitHub (Nov 21, 2023):

Set your WiFiManager wm; before Setup.

<!-- gh-comment-id:1820793964 --> @woodenplastic commented on GitHub (Nov 21, 2023): Set your WiFiManager wm; before Setup.
Author
Owner

@tuadru commented on GitHub (Nov 22, 2023):

Set your WiFiManager wm; before Setup.

I have put WiFi Manager wm just below int timeout = 120;
but it still works the same, it doesen't save the credentials once I restart or power off esp.

<!-- gh-comment-id:1822195758 --> @tuadru commented on GitHub (Nov 22, 2023): > Set your WiFiManager wm; before Setup. I have put WiFi Manager wm just below int timeout = 120; but it still works the same, it doesen't save the credentials once I restart or power off esp.
Author
Owner

@tablatronix commented on GitHub (Nov 22, 2023):

Try full flash erase, could be corrupt flash or wrong flash size?

<!-- gh-comment-id:1822935168 --> @tablatronix commented on GitHub (Nov 22, 2023): Try full flash erase, could be corrupt flash or wrong flash size?
Author
Owner

@Karthik-Official commented on GitHub (Feb 9, 2024):

Set your WiFiManager wm; before Setup.

I have put WiFi Manager wm just below int timeout = 120; but it still works the same, it doesn't save the credentials once I restart or power off esp.

I might have a solution, but I am not sure it would work for you. Just try if you can.

Just like yours, I too had a problem with ESP not connecting to the Wi-Fi after a reset or turned off or even go inside the config portal and click exit.
I have been trying many possible solutions like full flash erase my ESP, trying different Flash sizes and settings etc.

Then I noticed:

wm

As you can see it does remember the name of the Wi-Fi that it connected before which means in my case, it does store the Wi-Fi credentials.

**So, I finally tried, putting WiFiManager wm; below the int timeout = 120; just like you, and it doesn't work as you said before.
Additionally I put wm.autoConnect(); just below the WiFi.mode(WIFI_STA); in the setup. Reason is because I thought if it could somehow tried to reconnect at the SETUP , it may work **

Surprisingly, it worked, now even after restarting or power off, it just connects to the Wi-Fi credentials and got the IP as it should be.

wm2

This may or may not work; either way, trying is always better then give-up.

Just let everyone know whether it worked or not.

Thank you for reading this.

<!-- gh-comment-id:1936035952 --> @Karthik-Official commented on GitHub (Feb 9, 2024): > > Set your WiFiManager wm; before Setup. > > I have put WiFi Manager wm just below int timeout = 120; but it still works the same, it doesn't save the credentials once I restart or power off esp. I might have a solution, but I am not sure it would work for you. Just try if you can. Just like yours, I too had a problem with ESP not connecting to the Wi-Fi after a **reset** or **turned off** or even go inside the **config portal** and click exit. I have been trying many possible solutions like full flash erase my ESP, trying different Flash sizes and settings etc. Then I noticed: ![wm](https://github.com/tzapu/WiFiManager/assets/88947048/b03ace07-8293-4d2b-9527-8c599a563cd3) As you can see it does remember the name of the Wi-Fi that it connected before which means in my case, it does store the Wi-Fi credentials. **So, I finally tried, putting ` WiFiManager wm; ` below the `int timeout = 120;` just like you, and it doesn't work as you said before. Additionally I put `wm.autoConnect(); ` just below the `WiFi.mode(WIFI_STA);` in the setup. Reason is because I thought if it could somehow tried to reconnect at the SETUP , it may work ** Surprisingly, it worked, now even after restarting or power off, it just connects to the Wi-Fi credentials and got the IP as it should be. ![wm2](https://github.com/tzapu/WiFiManager/assets/88947048/90cf7fa8-bc2f-44da-814b-d85b20c8b7a8) This may or may not work; either way, trying is always better then give-up. Just let everyone know whether it worked or not. Thank you for reading this.
Author
Owner

@tablatronix commented on GitHub (Feb 11, 2024):

Check your serial logs for auth failures some routers require a timeout before reconnect and you have to have a longer connecttimeout and multiple retries its very annoying

<!-- gh-comment-id:1937397114 --> @tablatronix commented on GitHub (Feb 11, 2024): Check your serial logs for auth failures some routers require a timeout before reconnect and you have to have a longer connecttimeout and multiple retries its very annoying
Author
Owner

@lindhardt commented on GitHub (Aug 30, 2024):

You need auto connect in setup....

bool res = wm.autoConnect("MY_AP_NAME");
   if(!res) {
        Serial.println("Failed to connect");
        // ESP.restart();
    } 
    else {
        //if you get here you have connected to the WiFi    
        Serial.println("connected...yeey :)");
    }
<!-- gh-comment-id:2319681172 --> @lindhardt commented on GitHub (Aug 30, 2024): You need auto connect in setup.... ``` bool res = wm.autoConnect("MY_AP_NAME"); if(!res) { Serial.println("Failed to connect"); // ESP.restart(); } else { //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } ```
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#1425
No description provided.