Credentials sticking around?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Credentials sticking around?

Post by eradicatore » Thu Jul 20, 2017 1:50 am

Hi,
I have a sort of weird question. So I am using a file "creds" on the nodemcu to store my wifi ssid and password. I have a case where I do os.remove() of that file on boot up if a button is pressed down, and I see that the file does get removed, but it also seems that then the wifi connection/credentials don't get wiped from the esp8266 somehow. I use the following functions in my code:

wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP

And when I reset my board and know that it no longer had the password information in the file, it seems the "isconnected()" function still returns true. So then I printed out wlan.ifconfig() to see and sure enough it's got valid information (same IP as when I was connected before, etc).

I've wondered if I need to remove power completely from the node mcu for 5-10 seconds? Or is there a way to make sure it wipes out ssid and password with a call to connect again?

Justin

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: Credentials sticking around?

Post by eradicatore » Thu Jul 20, 2017 3:02 am

Ok, so I may have worked around it with this code snipped. Before I added the active false and active true calls right before the last while loop, it would sit in the while loop even though I had set the ssid/pass to blank strings. But seems maybe after you do that take it down and back up and it will forget it's creds. It was weird that now in the serial terminal I always get a "ets task" line printed out. Not sure if that's something I should be worried about? Doesn't seem to cause any problems.

Code: Select all

   wifi = network.WLAN(network.STA_IF)
   wifiAP = network.WLAN(network.AP_IF)
   wifiAP.active(False)

   if not wifi.isconnected() or not creds.get('pass') or not creds.get('ssid'):
      if wifi.active() is False:
         wifi.active(True)
      wifi.connect("", "")
      wifi.active(False)
      #utime.sleep(1)
      wifi.active(True)
      while wifi.isconnected():
         print("how long will we be here?")
         utime.sleep(1)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Credentials sticking around?

Post by pythoncoder » Thu Jul 20, 2017 7:52 am

The ESP8266 "remembers" connection details in Flash memory. On power up it will automatically reconnect to the last network used (if it's available).
Peter Hinch
Index to my micropython libraries.

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: Credentials sticking around?

Post by eradicatore » Thu Jul 20, 2017 11:42 am

Thanks for the reply. Yea, I guess I sort of expected that, and of course most of the time we want that! Is there a "factory reset" type of operation for esp8266? I will go look on google. Anyway, what I have here is working for me to reset. Thanks again!

I can't say enough about how much I love uPython on the nodemcu. What a great platform to develop with! It gave me a perfect learning ground to finally move on from Perl to Python and after I got good tools to upload/download files it's been incredibly stable. Thanks for a great forum too!!

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: Credentials sticking around?

Post by kfricke » Thu Jul 20, 2017 2:14 pm

Please be aware that the ESP8266 does not only remember the last WiFi credentials, but also the last 5 or was it 6?). They are stored in a static section of flash memory. One does need to write these credentials using the connect(...) method at least those few times to erase them from flash.

The best way you can get to something like a factory reset is erasing the flash before reflashing the MCU completely.

Also there is the open myth, that this operation does not wear out your flash unnecessarily.

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: Credentials sticking around?

Post by eradicatore » Thu Jul 20, 2017 2:17 pm

Does the "erase_flash" command in the nodemcu esp8266 "getting started" instructions erase the esp8266's flash or just the node MCU? Does that wipe these last 5 or 6 credentials?

wipity
Posts: 10
Joined: Sat Sep 09, 2017 4:35 pm

Re: Credentials sticking around?

Post by wipity » Thu Dec 07, 2017 4:49 pm

hi,
is there anyone who knows exactly how many credentials are saved in the flash and where?
if only the last configuration is taken into account, why are so many saved?
is there a way to 'read' the others too? thanks for the answers and sorry for too many question!!

Andrea

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: Credentials sticking around?

Post by kfricke » Thu Dec 07, 2017 10:10 pm

is there anyone who knows exactly how many credentials are saved in the flash and where?
According to this link it may be 0x7e000h. But i do not know if that still is the same location today and with MicroPython aboard the flash.
if only the last configuration is taken into account, why are so many saved?
I can not find a reference now, but iirc the reason has something to do with ESP8266's behaviour to reconnect to known WLAN automatically on startup. Maybe all these configs are considered for reconnecting!?
is there a way to 'read' the others too? thanks for the answers and sorry for too many question!!
In section 3.2.19 of the Non_OS_SDK they tell that there are 5 readable using that API call.

Post Reply