peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
575 stars 126 forks source link

When I change the ssid it keeps connecting to the old network if it is still in range #37

Closed goatchurchprime closed 2 years ago

goatchurchprime commented 4 years ago

The function wifi_connect() has the line:

 s.connect()  # ESP8266 remembers connection.

Only after this times out does it then call:

s.connect(self._ssid, self._wifi_pw)

Is there a way to check the name of the connection that the ESP8266 remembered, and compare it to the ssid that the programmer has asked for before assuming that it hasn't changed?

peterhinch commented 4 years ago

This is by design to minimise flash wear. If you specify an SSID and PW these are written to flash, causing wear. If you want to change this behaviour so that it only connects to the specified network, I suggest you change the line you referenced to match ESP32 behaviour.

There may be a way to meet your needs and reduce wear by storing the last connection details in a file (whenever this changes), and doing a specified connect when the requested credentials don't match the stored ones.

I don't plan to change the code as I believe it suits the majority of applications.

goatchurchprime commented 4 years ago

But is there a way to read the stored SSID to compare it with what's put into the config? This would not produce any wear.

peterhinch commented 4 years ago

I don't know of any. You could ask in the forum.

kevinkk525 commented 4 years ago

I also checked and sadly didn't find an option to read the ssid of the current connection.. A workaround would be to manually connect to the network you want before starting the mqtt.connect(). It will make a write to the flash but a single write to the flash every time the device resets shouldn't cause enough wear to destroy the flash within a few years.