tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.53k stars 1.96k forks source link

resetSettings(); not working #1275

Open nhlives opened 3 years ago

nhlives commented 3 years ago

Basic Infos

Hardware

WiFimanager Branch/Release: Master

Esp8266/Esp32:

Hardware: ESP-12E

Core Version: 2.4.0, staging

Description

Problem description

Settings in IDE

Module: NodeMcu 1,0 (esp-12E module) Additional libraries:

Sketch

#BEGIN
       WiFiManager wifiManager;
        wifiManager.resetSettings();
        delay(4000);
        ESP.reset();
#END

Debug Messages

The resetSettings(); doesn't bring up the AP. I need to cause the AP to start after a successful connect. This did work on platform 2.7.4, I'm on 3.0.2

tablatronix commented 3 years ago

What does serial say? Esp32 or 8266?

tarontop commented 3 years ago
    wifiManager.resetSettings();
    ESP.eraseConfig(); 
    delay(2000);
    ESP.reset(); 

This works for me

hungtcs commented 3 years ago
    wifiManager.resetSettings();
    ESP.eraseConfig(); 
    delay(2000);
    ESP.reset(); 

This works for me

ESP.eraseConfig(); working for me.

radacina commented 2 years ago

Per ESP8266 SDK doc, the advantage over eraseConfig would be IMHO that the "damage" would be limited to the struct size and address:

void user_set_station_config(void) 
{ 
char ssid[32] = "";  
char password[64] = "";  
struct station_config stationConf;  
stationConf.bssid_set = 0;      //need not check MAC address of AP 
os_memcpy(&stationConf.ssid, ssid, 32);  
os_memcpy(&stationConf.password, password, 64);  
wifi_station_set_config(&stationConf);  
}
tablatronix commented 2 years ago

ESP8266 or ESP32?????

radacina commented 2 years ago

ESP8266, I updated the comment

tablatronix commented 2 years ago

I am assuming ( lack of logs ) that erase is failing.

I will do some testing with newer libs

wm.resetsettings does a simple disconnect() which hits the sdk with an empty config struct, so could be bugs there.. wm.erase does a ESP.eraseconfig() with bool return

Ill see if i can add a more reliable method, and add status checking

tablatronix commented 2 years ago

Also some of these will fail if sta mode is faulty or not active, which could also be a bug in sdk sometimes setmode fails

AndyT86 commented 2 years ago

After reading I think I know, I am expecting resetSettings() be permanent but it is not, thus buggy. Commenting out that code is having entire suite reading back in EEPROM'd SSID/password instead of actually erasing it; while removing comment it is like a Safe Mode to block the EEPROM, the actual erasing is somewhere else; given the intent and name this is buggy to use even though it may be actual intent of the suite in the view of WifiManager team.

tablatronix commented 2 years ago

Huh?

AndyT86 commented 2 years ago

It's not permanent erasure of configuration data, that's what I mean, all it does to my ESP32 is like someone pressed F8 in Windows bootup. That is not expected, commenting out and it reads back in the data written to somewhere to connect to what was set instead of starting anew.

tablatronix commented 2 years ago

It erases the wifi credentials, can you tell me what esp chip and lib ?

AndyT86 commented 2 years ago

I figured it "bypass" the Wi-Fi creds no erasure really happened, one could tell since it pre-dots the passcode field and rebooting the ESP reads in the cred prior to these, the chip is ESP32-WROOM-32 on a Wemos D1 R32 board; the one shaped like Uno, since that is one that happens to be compatible here, I have not tried the no frill DevKit board I also have. The core lib is Espressif https://github.com/espressif/arduino-esp32

tablatronix commented 2 years ago

Not sure what compatible means, all esp are compatible regardless of what dev board/project.

I don't know what pre dots means, the info page clearly shows wifi information as does the serial output.. It should be fairly clear even by the status at the bottom of the home page...

tablatronix commented 2 years ago

works for me just fine...

*wm:[1] Free heap:        277632
*wm:[1] ESP SDK version:  v3.3.5-1-g85c43024c
[WIFI] WIFI INFO DEBUG
Mode: STA
Channel: 1
SSID (5): leela
Passphrase (8): Turdtr0n
BSSID set: 0
[WIFI] SAVED: YES
[WIFI] SSID: myap
[WIFI] PASS: password
*wm:[1] resetSettings 
*wm:[1] SETTINGS ERASED 
[WIFI] WIFI INFO DEBUG
Mode: STA
Channel: 1
SSID (0): 
Passphrase (0): 
BSSID set: 0
[WIFI] SAVED: NO
[WIFI] SSID: 
[WIFI] PASS: 
*wm:[2] Added Parameter:
*wm:[2] Added Parameter: server
tablatronix commented 2 years ago

Cannot reproduce, unless you can show me what its doing. So you are using git staging esp32? IDF? what?

AndyT86 commented 2 years ago

I meant USB chip compatibility, some of them are compatible under Windows while the rest need drivers to do the ISP or FTDI; that's neither here nor there. Forget about "pre-dot" I just meant I can observe visually old data is retained. But as you said, serial log is saying exactly that too. A moment while I gather stuff.

tablatronix commented 2 years ago

and tested staging OK


INFORMATION] TEST
*wm:[1] Free heap:        259012
*wm:[1] ESP SDK version:  v4.4-beta1-308-gf3e0c8bc41
[WIFI] WIFI INFO DEBUG
[WIFI] SAVED: YES
[WIFI] SSID: myAp
[WIFI] PASS: password
*wm:[1] resetSettings 
[  1211][V][WiFiGeneric.cpp:275] _arduino_event_cb(): STA Stopped
[  1212][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 3 - STA_STOP
*wm:[1] SETTINGS ERASED 
[WIFI] WIFI INFO DEBUG
[WIFI] SAVED: NO
[WIFI] SSID: �&�?xV��
[WIFI] PASS: 
*wm:[2] Added Parameter:
tablatronix commented 2 years ago

Ok I see the problem here. esp32 does nothing if wifi is not enabled, let me see how else I can do this, direct erase etc. Also getting stuff like ssid fails in the same way even if its set.

This works fine if you set WiFi.mode(STA) in your code, as that starts wifi up

add WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP and it will work

AndyT86 commented 2 years ago

I think I figure it out, it works before calling autoConnect(), otherwise the serial log says it is working but it would not be after, might as well since no one need to care whether reset is pre or post turning on.

tablatronix commented 2 years ago

resetsettings starts sta actually so nm what i said, but there might be a race condition, i will try adding a delay in there to make sure and also add a check to see if it actually suceeds

tablatronix commented 2 years ago

hmm i can test that too, I use ondemand button and it usually also works so might be same issue

AndyT86 commented 2 years ago

Oh while I got your attention:

collect2.exe: error: ld returned 1 exit status Multiple libraries were found for "WiFi.h" Used: C:\Users\actsa\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.2\libraries\WiFi Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries\WiFi exit status 1 Error compiling for board WEMOS D1 MINI ESP32.

Some #define and #ifndef stuff I suspect.

tablatronix commented 2 years ago

No idea, sounds like an arduino issue,

AndyT86 commented 2 years ago

I think I have huge IDE issue, don't know where it is wrong it was fine last night.

diegotam855 commented 2 years ago

Hi @tablatronix, I have a similar issue, so as you said the code is working fine with this line in the setup void:

WiFi.mode(WIFI_STA);

But it doesn't work when I move it into a IF statment:

if (digitalRead(TRIGGER_PIN) == LOW){      
        wifiManager.resetSettings();
        delay(500);
        ESP.restart();
        delay(1000);
    }

I'm trying to get the memory erase when I press a button on sturtup. Any suggestion?

My hardware is a ESP8266 Wemos D1 mini Pro

AndyT86 commented 2 years ago

If I may interject, I believe I can be of some help instead of @tablatronix on this, Shawn once said there maybe a bug and I am getting around this at startup differently: image This I am using ESP32, I have both DevKit and Wemos Mini version but shouldn't matter.

diegotam855 commented 2 years ago

If I may interject, I believe I can be of some help instead of @tablatronix on this, Shawn once said there maybe a bug and I am getting around this at startup differently: image This I am using ESP32, I have both DevKit and Wemos Mini version but shouldn't matter.

Thank you for answering! But it seams that wifimanager doesn't have .erase(); function, what's that?

As a work around I called .startConfigPortal(); with the button without erasing the old credentials.

diegotam855 commented 2 years ago

Here is what I'm doing now

bool forceConfig = false;

    if (digitalRead(TRIGGER_PIN) == LOW){    
      forceConfig = true;
    }    

    if (forceConfig){
      wm.setConfigPortalTimeout(timeout);
      if (!wm.startConfigPortal(ESP_AP)){
        Serial.println("failed to connect and hit timeout");
        delay(1000);
        ESP.restart();
        delay(1000);
      }
    }
    else{
      if (!wm.autoConnect("ESP_AP")){
          Serial.println("Failed to connect");
          delay(1000);
          ESP.restart();
          delay(1000);
      }
      else {
          Serial.println("Connected!");
      }
AndyT86 commented 2 years ago

No it's not like that, check mine; it just flows into no branching is needed besides that if button ... little detour.

tablatronix commented 2 years ago

No idea it always erases for me.. what esp version?

tom-ch1 commented 1 year ago

do we actually know what the various wm functions are supposed to do? I did not find a documentation of resetSettings() nor erase() :-(

tablatronix commented 1 year ago

lol, seriously

https://github.com/tzapu/WiFiManager/wiki/API-reference

Brian-Kristiansen commented 1 year ago

Hi -

Sorry to revive this old issue...

I ran into the same problem as he original poster, today - trying to use resetSettings() to clear WiFi credentials on a ESP8266 (Wemos D1 mini). I use the library tzapu/WiFiManager@^0.16.0.

When using resetSettings() right before autoConnect() it correctly starts the 192.168.4.1 website etc.. but when using resetSettings() to only clear the credentials, and then immediately restart the board without calling resetSettings(), the previous WiFi credentials are loaded and used by autoConnect(). It looks like the resetSettings() didn't save/clear. Even calling WiFi.disconnect(true) didn't clear the credentials.

I suspected, maybe it tried to clear the EEPROM information, but was now allowed/ignored. So I tried:

  WiFi.mode(WIFI_STA);
  WiFiManager wm;

  // Clearing WiFi credentials
  WiFi.persistent(true);
  wm.resetSettings();

This was a succes - setting WiFi.peristent(true) works for me!! Looks like the WiFi library defaults to WiFi.persistent(false) - so the resetSettings() never persists the clearing. - OR my cheap ESP8266 board does not follow standards .. or ..

Anyway - I hope my tip can help somebody else in the same situation.

@tablatronix - Would it be risky to add a WiFi.persistent(true) in the resetSettings() code?

tablatronix commented 1 year ago

Not sure what the problem is

but it already does that in 2.x

void WiFiManager::resetSettings() {
#ifdef WM_DEBUG_LEVEL
  DEBUG_WM(F("resetSettings"));
  #endif
  WiFi_enableSTA(true,true); // must be sta to disconnect erase
  delay(500); // ensure sta is enabled
  if (_resetcallback != NULL){
      _resetcallback();  // @CALLBACK
  }

  #ifdef ESP32
    WiFi.disconnect(true,true);
  #else
    WiFi.persistent(true);
    WiFi.disconnect(true);
    WiFi.persistent(false);
  #endif
  #ifdef WM_DEBUG_LEVEL
  DEBUG_WM(F("SETTINGS ERASED"));
  #endif
}
Brian-Kristiansen commented 1 year ago

Thanks @tablatronix, I see my problem. I use v0.16.0 - as this is what lib_deps in PlatformIO fetches for me automatically. I cannot see any newer versions there.

In my version of v0.16.0 it looks like this:

void WiFiManager::resetSettings() {
  DEBUG_WM(F("settings invalidated"));
  DEBUG_WM(F("THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA."));
  WiFi.disconnect(true);
  //delay(200);
} 

.. which corresponds to the log messages I get, when running the application.

Now - how to get PlatformIO to use v2.x? Hmmm.. When will v2.x upgrade from being a RC? ;-)

Thanks.

tablatronix commented 1 year ago

You have to use the git address in lib deps

Suresh6060 commented 1 month ago

I am also having similar issue on ESP32 dev board. It brings out the AutoConnectAP correctly first time when code is uploaded.

However when attempt is made to bring it up, say for changing the ssid and password (network) , It gets stuck at

wm.setWifiAutoReconnect(false) done wm:Disconnecting wm.disconnectWiFi() done wm:resetSettings wm:SETTINGS ERASED w.resetSettings() 2 done wm:AutoConnect wm:No wifi saved, skipping wm:AutoConnect: FAILED for 21 ms wm:StartAP with SSID: AutoConnectAP wm:AP IP address: 192.168.4.1 *wm:Starting Web Portal

. On the mobile , it shows the autoConnectAP . When trying to connect, it asks for the password too. But it doesn't bring the network assigning page to enter ssis and password. I have tried almost all the methods seen and elsewhere. But the result is same. In some instance it connects to the ESP_XXXXX SSID . Then uploading the code (Practically erases all the saved wifi information ) seems to be the only solution.

what could be the solution ?

tablatronix commented 1 month ago

Does it not bring up the captive portal, what if you goto the ip manually in browser?

Suresh6060 commented 1 month ago

Does it not bring up the captive portal, what if you goto the ip manually in browser?

It doesnt connect. I can see the AutoCoonectAp under the list. However if I click on it, It says ApConnectAP has no internet Tap for options . Looks like it behaves like any other WiFi on the network. The page for entering SSID and PASSWORD doesn't show.

tablatronix commented 1 month ago

Thats the captive portal, goto 192.168.4.1 and see if it works,.

Suresh6060 commented 1 month ago

Thats the captive portal, goto 192.168.4.1 and see if it works,.

I think I got why it behaves like that. I have several other libraries working on connecting to WiFi in the first instance like WebServer.h, HTTPClient.h etc.

They are still connected when wm.resetSettings() is executed and wm.startConfigPortal or autoConnect is tried. This messes with the functionality because the other objects are still latched to the wifi. I don't know how this works out internally.

To confirm this , I used a new sketch with bare minimum code elements to test only WiFiManager. It works nicely every time.

Probably I need to find out how the other objects could be stopped before re-configuring WiFiManager and restarted on successful connection with new WiFi credentials.

Thank you for your time and efforts.

tablatronix commented 1 month ago

There is a issue in esp with releasing servers and reusing ports no solution afaik. So you cannot stop and restart another web object as the first iss never actually cleared

Suresh6060 commented 4 weeks ago

There is a issue in esp with releasing servers and reusing ports no solution afaik. So you cannot stop and restart another web object as the first iss never actually cleared

I used server.stop() server is WebServer server(80); declared earlier. There was another issue.

wm.resetSettings() only cleared the WiFi details stored by WiFiManager wm;

What was stored by WiFi.h still existed. WiFi.disconnect() only disconnects the network . The information(s) stored earlier still exist. This creates conflicts when wm is re-configured.

In order to synch both WiFi.disconnect(false, true) was used in the begin. This clears the WiFi cache too and wm sends th config with No AP set.

When the new AP is received, both wm and WiFi gets synchronised again and it works.

server.begin() was used to start the server after it was stopped.

Now the whole code works nicely as expected. The only glitch I find is in order to load the in built pages in the server, the esp2 had to reset again soon after config settings. That is fine as of now.

tablatronix commented 3 weeks ago

The credentials are the same as esp, we dont use flash