WiFiManager icon indicating copy to clipboard operation
WiFiManager copied to clipboard

Try to reset memory when button is pressed

Open tomasbond opened this issue 5 years ago • 20 comments

Basic Infos

Hardware

WiFimanager Branch/Release: Development

Esp8266: Nodemcu v3

Hardware: ESP-12e

Core Version: 2.4.0, staging

Description

Trying to make the onboard flash run a reset on the wifi memory, running the wifiManager.resetSettings() function.

Settings in IDE

Module: NodeMcu, Wemos D1

Additional libraries:

Sketch


#include <Arduino.h>
#define button 0
void setup() {
pinMode(button, INPUT);
}

void loop() {
if (digitalRead(button) == HIGH) {
Serial.println("Memory reset");
wifiManager.resetSettings();
}

Debug Messages

Where should i put that code so when i press the flash button in the board it runs a memory reset and forget the ssid and password? tryied to put it here and doesnt work. On terminal i can see a New Client but nothing happens when the button is pressed

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients
  
if (digitalRead(button) == HIGH) {
Serial.println("Memory reset");
wifiManager.resetSettings();
}

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    
    while (client.connected()) {            // loop while the client's connected
      
      if (client.available()) {             // if there's bytes to read from the client,


tomasbond avatar Aug 21 '18 01:08 tomasbond

I think you have to reboot after erasing settings. I mean what do you expect to happen? What do the serial debug say? Do a printdiag and see that its empty

tablatronix avatar Aug 21 '18 01:08 tablatronix

Hi! Thanks for your reply! I used a Serial.print("DEBUG"); to see the loop action and the ESP serial stays in, New Client. As there is something avoiding the ESP enter again the loop. Right now im not at home but as soon as i get there ill update on this. My expectation was to add the resetSettings to a button and that should be run if the button is triggered. Of course module should restart to forget actual wifi connections (i think) and give a fresh new start. But the problem is that the code wont read the button and trigger the resetSettings function.

tomasbond avatar Aug 21 '18 17:08 tomasbond

did you set the pinmode in setup ?

tablatronix avatar Aug 21 '18 20:08 tablatronix

you have a while loop while (client.connected()) {
so yeah its gonna stay in there

tablatronix avatar Aug 21 '18 20:08 tablatronix

please close if solved

tablatronix avatar Aug 29 '18 16:08 tablatronix

HI All I'm emry beginner. i read all the conversation about reset button. But i cannot emigine the reset connect to. i m using huzzah esp8266. i run autoconnect on wifimanager library. Need help to show the connection reset button and code in arduino ide. Thanks.

emry78 avatar Apr 14 '19 00:04 emry78

I implemented this in conjunction with EasyButton.

// At the global level
#define FLASH_PIN 0
EasyButton resetButton(FLASH_PIN);
#define RESET_DURATION 5000

void setup() {
  resetButton.begin();
  // Allows me to set a custom duration for how long a user holds the reset button for.
  // If you want it to immediately reset, just use `resetButton.onPressed(reset);`
  // Here, `reset` is the `void reset()` function defined below. 
  // We're passing the callback function, which is called when the library detects the button is pressed for DURATION amount of ms. 
  resetButton.onPressedFor(RESET_DURATION, reset);

  // ... rest of your setup, along with WiFi manager
}

void reset() {
  Serial.println("FLASH was held for " + String(RESET_DURATION, DEC) + "ms.");
  Serial.println("Erasing stored WiFi credentials.");
  
  // clear WiFi creds.
  WiFiManager wifiManager;
  wifiManager.resetSettings();
   
  Serial.println("Restarting...");
  ESP.restart(); // builtin, safely restarts the ESP. 
}

void loop() {
  resetButton.read();
  
  // ... rest of your logic, including handling client-connections, etc.
}

zshift avatar Apr 03 '20 18:04 zshift

But on that note, this shouldn't be an issue for the WiFiManager. Restarting the ESP is independent of clearing settings. I recommend closing this issue.

zshift avatar Apr 03 '20 18:04 zshift

I'm trying to make the ESP8266 forget its wifi credentials - I've tried wifiManager.resetSettings(); and Wifi.disconnect(true); with delay(2000);
ESP.reset(); thrown in for good measure, but every time it comes back with the last successful connection details, saying: *WM: Using last saved values, should be faster

I've even tried setting them to nonsense values (using Wifi.begin("xxx", "xxx"); but it still goes back to the old ones on next reboot.

Is there a reliable way to nuke the credentials?

andysc avatar Nov 24 '21 17:11 andysc

That is supposed to work...

tablatronix avatar Nov 24 '21 17:11 tablatronix

oh, it looks like I'm hideously back-level! 0.15.0-beta !!! Sorry... I'll upgrade and report back

andysc avatar Nov 25 '21 09:11 andysc

should I be trying 2.00 or 0.16 ?? Are there breaking changes in 2.0 ?

andysc avatar Nov 25 '21 09:11 andysc

OK, here's my little test case:

#include <ESP8266WiFi.h>

#include <WiFiManager.h>
WiFiManager wifiManager;

void setup() {

  Serial.begin(9600);
  delay(500);

  Serial.print("Looking for ");
  Serial.print(WiFi.SSID().c_str());
  Serial.print(" / ");
  Serial.println(WiFi.psk().c_str());

  Serial.println("*** Resetting WiFi credentials ***");
  
  wifiManager.resetSettings();
  
  delay(2000);
  ESP.reset();
}

void loop() {

}

When I run that I get...

⸮⸮⸮ԅp⸮@H6⸮0⸮⸮Looking for xxx / xxx
*** Resetting WiFi credentials ***
*WM: settings invalidated
*WM: THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA.
?)⸮)⸮⸮̍!H⸮Looking for xxx / xxx
*** Resetting WiFi credentials ***
*WM: settings invalidated
*WM: THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA.
⸮⸮⸮ԅ⸮⸮@H⸮⸮⸮⸮⸮Looking for xxx / xxx

so it the resetSettings() doesn't seem to have any effect .

This is on v0.16.0

andysc avatar Nov 25 '21 09:11 andysc

On v2.0.5-beta ... it works great!

** Resetting WiFi credentials ***
*wm:[1] resetSettings 
*wm:[1] SETTINGS ERASED 
H!⸮⸮L⸮⸮⸮J(⸮⸮⸮	⸮Looking for  / 
*** Resetting WiFi credentials ***
*wm:[1] resetSettings 
*wm:[1] SETTINGS ERASED 
H⸮⸮ԅ⸮⸮@H⸮⸮⸮⸮⸮Looking for  / 
*** Resetting WiFi credentials ***
*wm:[1] resetSettings 
*wm:[1] SETTINGS ERASED 
⸮⸮⸮⸮U9⸮@H⸮9⸮⸮⸮⸮Looking for  / 

OK - you can close this now :)

andysc avatar Nov 25 '21 09:11 andysc

aha - I see there's some additional magic in this version...

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

Thanks :)

andysc avatar Nov 25 '21 09:11 andysc

looks like there's more to it than that, as just adding those lines into my sketch doesn't erase the credentials. Anyway... latest library seems to be the answer :)

andysc avatar Nov 25 '21 17:11 andysc

Cracked it :)

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

THAT resets the wifi credentials. (you do not need to restart the ESP afterwards for it to take effect)

andysc avatar Nov 25 '21 17:11 andysc

Not sure what you mean , are you trying to fix this in v0?

tablatronix avatar Nov 25 '21 18:11 tablatronix

yes, I was trying to work out what you did in v2 that makes it work, that didn't work in v0.

andysc avatar Nov 25 '21 20:11 andysc

The whole thing was reworked, so no idea. Did not know it was not working. Ill look and maybe patch it maybe. But its totally worth switching to v2 at this time if you have the flash

tablatronix avatar Nov 26 '21 01:11 tablatronix