Skip to content
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

how to erase previous credentials? #142

Closed
hixfield opened this issue Mar 28, 2016 · 37 comments
Closed

how to erase previous credentials? #142

hixfield opened this issue Mar 28, 2016 · 37 comments
Labels
Question User Question member to member support

Comments

@hixfield
Copy link

I have a button on my ESP, that is (via an interrupt service routine) ment to restore factory settings.
How can I clear the existing WIFI credentials, so that after reset WIFIManager "sees" no credentials and starts-up the AP for setting them?

failed
I found a reference to ESP.eraseConfig here esp8266/Arduino#1494 to but that just completely blocks the ESP and I needed to re-flash it! So I guess that is not the way to go...

@hixfield
Copy link
Author

I think I figured out a solution.
When the button is pressed I set a flag, and in the loop the flag is checked and then just a simple WiFi.disconnect() seams to push the WIFIManager in AP mode.
By the way, I found both the ESP.reset and ESP.restart to be unreliable, so I connected GPIO16 to the ESP RST pin, and using "software" I do a hardware reset like this. Yes... I am working with a flag, because the ESP seams to crash when I put the WiFi.disconnect() in the ISR itself...

This is part of my code:

void resetToFactoryDefaults() {
  WiFi.disconnect();
  delay(3000);
  hardwareReset();
}

void hardwareReset() {
  pinMode(g_nPinReset, OUTPUT);
  digitalWrite(g_nPinReset, false);
  delay(3000);
}

void isrResetToFactoryDefaults(void) {
  Serial.println("Resetting to factory defaults");
  g_bShouldResetToFactorySettings = true;
}

void setupWifi() {
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //exit after config instead of connecting
  wifiManager.setBreakAfterConfig(true);
  //tries to connect to last known settings if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP" with password "password" and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect()) {
    Serial.println("failed to connect, we should reset as see if it connects");
    hardwareReset();
  }
...
}

void loop(void) {
  if (g_bShouldResetToFactorySettings) resetToFactoryDefaults();
}

@tzapu
Copy link
Owner

tzapu commented Mar 28, 2016

hi,

as a general rule of thumb, don t do anything in interrupt callbacks except setting a flag, and process it in the loop. asking for trouble otherwise

WiFi.disconnect() will erase ssid/password

i use ESP.reset() sucessfully in a lot of projects, the key may be to add a delay(1000) after it. (also, not in a callback)

i hope this helps, cheers

@baruch
Copy link

baruch commented Apr 6, 2016

This ticket can probably be closed, maybe document it somewhere in the readme or provide an example.

@electron1979
Copy link

Only WiFi.disconnect(); worked for me!

Worked here too!

@trungkiendt9
Copy link

With button reset. It's done.
if (reset_level >500) {
WiFi.disconnect(true);
delay(2000);
ESP.reset();
}

@yo2lts
Copy link

yo2lts commented Dec 5, 2018

I have a Heltec esp wifi lora 32 I tried to delete the saved SSID settings but it does not work. I tried all the variants read above. I use the PIO with Atom but also on the Arduino IDE do the same. Can anyone help me how to make factorw reset? Thank you!

@electron1979
Copy link

electron1979 commented Dec 5, 2018

@yo2lts, I think something like what follows may work in Arduino IDE:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
void setup()
{
  WiFi.disconnect();
//  WiFiManager.resetSettings();
//  ESP.eraseConfig();
//  ESP.reset();
//  ESP.restart();
}
void loop()
{
  yield();
}

@tablatronix
Copy link
Collaborator

For factory reset you need to use esptool to erase flash also the arduino ide now has erase capability

@yo2lts
Copy link

yo2lts commented Dec 5, 2018

Multumesc, cred ca am reusit.
What is yield() ?
Thank You! Work!

@a-c-sreedhar-reddy
Copy link

a-c-sreedhar-reddy commented Feb 22, 2019

WiFi.disconnect(false,true);
This erases existing AP credentials.

@angus-grant
Copy link

None of that stuff worked. I searched for so long across so many resources.

I ended up digging around in the ESP code base and happened upon this little nugget

ESP_ERROR_CHECK(nvs_flash_erase());
nvs_flash_init();

This seems to have cleared my "AUTH_EXPIRE" credentials and got my WiFi working again. One caution is that it clears everything in flash. So if you are using preferences library, etc, it all gets wiped.

@tablatronix
Copy link
Collaborator

You should not have to nvs erase to clear credentials, your memory was probably corrupt or changed from an upgrade.

@angus-grant
Copy link

Well after a month and delayed shipping of product, I'll take any solution. :-|

The strange thing is that values I was saving with the preferences library would save into flash, and be re-loaded after next reboot. It was the WiFi which would simply not connect always giving AUTH_EXPIRE exception. So I think it was related to a WiFi problem and nor corrupt memory. But maybe the corruption was just with the WiFi details that were saved.

@tablatronix
Copy link
Collaborator

Yes if your partition is corrupt your wifi gets corrupted as things write into its memory space, so it might seem to work but some part of it is broken and messed up connections, I wish there was some kind of parity check here, also there was no protection for wifi struct schema when it was changed, I think there is now so upgrades wont break it as much. I always do a full erase if moving to a new version of sdk or lib.

@angus-grant
Copy link

Your info is interesting and something I'll remember when doing sdk or library upgrades..

But I changed nothing. One day it was working, the next day it wasn't...
Oh well, problem worked around/solved.

Thanks for your comments!

@qlalfdu
Copy link

qlalfdu commented Dec 17, 2019

WiFi.mode(WIFI_STA);
WiFi.disconnect(true,true);

@rahulujjnkr
Copy link

my simple logic is , if we insert the false ssid and password into the program, and we also reset the esp within a program. problem may be resolved. i had try it and working fine.
i use two pins D2 and D3 for this to reset the esp.
sorry for my English.... !!

/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/
this is the whole code....
/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/

#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
void SoftReset(void);
void espreset(void);
const int BUTTON = 4;
const int LED = 0;
int BUTTONstate = 0;
// Replace these with your WiFi network settings
const char* ssid = "faslsessid"; //replace this with your WiFi network name
const char* password = "falsepass"; //replace this with your WiFi network password

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode (BUTTON, INPUT);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();

//set custom ip for portal
wifiManager.setAPStaticIPConfig(IPAddress(192,168,33,1), IPAddress (192,168,33,1), IPAddress(255,255,255,0));

//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here  "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("WinLix Infotech AP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();


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

}

void loop() {
// put your main code here, to run repeatedly:
BUTTONstate = digitalRead(BUTTON);
if (BUTTONstate == HIGH)
{
digitalWrite(LED, HIGH);
SoftReset();
espreset();
}
else
{
digitalWrite(LED, LOW);
}

}

void SoftReset()
{
delay(5000);
Serial.begin(115200);

WiFi.begin(ssid, password);

Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
}
void espreset()
{
ESP.restart();
}

this is my first ever try with arduino programming.

@tablatronix
Copy link
Collaborator

Lol it says it right there in the code

//reset saved settings
//wifiManager.resetSettings();

@tablatronix tablatronix added the Question User Question member to member support label Feb 21, 2020
@lloydrichards
Copy link

lloydrichards commented Mar 9, 2020

I'm having a similar problem. wifiManager.resetSettings() does not reset the credentials.. I'm literally running the base example:

#if defined(ESP8266)
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#else
#include <WiFi.h>          //https://github.com/esp8266/Arduino
#endif

//needed for library
#include <DNSServer.h>
#if defined(ESP8266)
#include <ESP8266WebServer.h>
#else
#include <WebServer.h>
#endif
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager


void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);

    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;
    //reset saved settings
    wifiManager.resetSettings();
    
    //set custom ip for portal
    //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifiManager.autoConnect("AutoConnectAP");
    //or use this for auto generated name ESP + ChipID
    //wifiManager.autoConnect();

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

void loop() {
    // put your main code here, to run repeatedly:
    
}

And its still giving me this in console:

*WM: Connection result: *WM: 3 *WM: IP Address: *WM: 192.168.1.178 connected...yeey :)

Either something is wrong with the function or its still storing the creds somewhere else?

@tablatronix
Copy link
Collaborator

tablatronix commented Mar 10, 2020

esp8266?

Do you have serial logs?

@tablatronix tablatronix reopened this Mar 10, 2020
@lloydrichards
Copy link

on ESP32 Dev Module. The serial logs are at the bottom there:

*WM: Connection result:
*WM: 3
*WM: IP Address:
*WM: 192.168.1.178
connected...yeey :)

My workaround currently is to set up a manual config for the AP and attach to a button, but this means having to connect everytime which is frustrating

@litamin
Copy link

litamin commented Mar 11, 2020

Having same issue as lloydrichards here, running on a ESP32 Doit-devkit-v1.
wifiManager.resetSettings() does not do what it is supposed to do (to reset wifi credentials).

Should mention that I'm using https://github.com/zhouhan0126/WIFIMANAGER-ESP32 library, because to my understanding the original Tzapu library cannot run on ESP32?

@Daemach
Copy link

Daemach commented Mar 11, 2020 via email

@tablatronix
Copy link
Collaborator

Development branch does

@litamin
Copy link

litamin commented Mar 12, 2020

Cool, so is there a way to reset Wifi credentials or not?
wifiManager.resetSettings() is not doing the job.

@tablatronix
Copy link
Collaborator

I have not had time to look into it, could be a bug, try erase flash also

@lloydrichards
Copy link

If it helps for debugging, I was finding that even when I flashed a new program and the went back to something including the wifimanager it would still have my credentials saved when using wifiManager.autoConnect(). This doesn't seem to affect wifiManager.startConfigPortal(), but i'm guessing that cause its just overwriting the credentials. Might give some experimenting to calling wifiManager.autoConnect() to start and then wifiManager.startConfigPortal() when i need to override

@tablatronix
Copy link
Collaborator

the ESP saves your credentials not WM

@tablatronix
Copy link
Collaborator

Works for me

*WM: [1] SETTINGS ERASED 
*WM: [3] WiFi station enable 
*WM: [3] enableSTA PERSISTENT ON 

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v8b899c12
~ld

 Starting
*WM: [1] getCoreVersion():          2_5_2
*WM: [1] system_get_sdk_version():  2.2.1(cfd48f3)
*WM: [1] system_get_boot_version(): 31
*WM: [1] getFreeHeap():             49088
Mode: STA
PHY mode: N
Channel: 1
AP id: 0
Status: 0
Auto connect: 1
SSID (0): 
Passphrase (0): 
BSSID set: 0
YES
SSID: 
PASS: 

@tablatronix
Copy link
Collaborator

Let me try esp32

@tablatronix
Copy link
Collaborator

Also works

*WM: [1] resetSettings 
*WM: [3] WiFi station enable 
*WM: [1] SETTINGS ERASED 
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8740
load:0x40080400,len:5800
entry 0x4008069c

 Starting
*WM: [1] Free heap:        210252
*WM: [1] ESP-IDF version:  v3.2.2-132-g7dd492319-dirty
Mode: STA
Channel: 1
SSID (0): 
Passphrase (0): 
BSSID set: 0
YES
SSID: 
PASS: 

@tablatronix
Copy link
Collaborator

I cannot reproduce, I need you to erase flash, and try again and I will need more information

@rodrigok1
Copy link

How to get the saved SSID and password?

@peperoca116
Copy link

To get the credentials you need the following:

WiFi.begin(); // Mandatory
delay(1000);
String TEMP_Ssid = WiFi.SSID();
String TEMP_Pass = WiFi.psk();

Done! You get credentials without the need to store them neither in spiffs nor in EEPROM

@neklauss
Copy link

The libraries built in WifiManager::erase() method did the trick for me.

@CplSyx
Copy link

CplSyx commented Feb 9, 2023

For factory reset you need to use esptool to erase flash also the arduino ide now has erase capability

wifiManager.resetSettings() was not working for me (ESP8266) as autoConnect would recover the credentials - however erasing the flash via the IDE has solved the issue.

@tablatronix
Copy link
Collaborator

Interesting, it should erase them, but maybe there was an esp bug in some version you were using

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question User Question member to member support
Projects
None yet
Development

No branches or pull requests