Hi,
I bought an ESP8266, after some time I'm using ESP32.
On ESP32 I start wifi during main.py.
In ESP8266 ( after running same script successfully connecting to the internet ), I noticed that even if I don't run it on boot, port is still connected.
why is that ?
how can I control it ?
Connect to Wifi
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Connect to Wifi
That is how the ESP8266 works. It saves the details of the last connection in flash. On boot it attempts to reconnect to that network. This is a function of the hardware/vendor code, not of MicroPython.
The feature was widely criticised on security grounds (credentials can apparently be accessed) which is probably why Espressif abandoned it on ESP32.
The feature was widely criticised on security grounds (credentials can apparently be accessed) which is probably why Espressif abandoned it on ESP32.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Connect to Wifi
thank you.
I want to be sure I understand the connection procedure, since on boot I run my connect_to_wifi file during main.py - it asks a for a static ip, so when the other takes places' before or after ? my concern here IP address conflicts
Guy
I want to be sure I understand the connection procedure, since on boot I run my connect_to_wifi file during main.py - it asks a for a static ip, so when the other takes places' before or after ? my concern here IP address conflicts
Guy
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Connect to Wifi
I'm not entirely sure I understand your problem, so apologies if the following misses the point.
On boot an ESP8266 will try to reconnect to the last network it used. You need to check whether this succeeded and, if so, whether it connected to the right network. If it's on the wrong network, disconnect. Then try to connect to the right LAN (then check for success).
On boot an ESP8266 will try to reconnect to the last network it used. You need to check whether this succeeded and, if so, whether it connected to the right network. If it's on the wrong network, disconnect. Then try to connect to the right LAN (then check for success).
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Connect to Wifi
Hi Peter,
My apologies for my poor English, I'm not a native speaker
I'll try to explain more clearly- I design a Iot device including an ESP32 /ESP8266 ( didn't decide yet, what is best for the job ), using MicroPython. So far- the code is running as expected ( 2 relays work together to control a shade go up and down ), using both a physical button ( 3 states : up, down, off ), together with MQTT for remote commands ( that can additionally run scheduled tasks ).
Now- when a non-nominal scenario occurs, such as:
1) broker resets ( local ) and back up ( or not...)
2) wifi gets disconnected
3) power down, causing not all boot together when all is ready
My goal:
I have to make the code designed to such losses, to regain wifi and MQTT , and meanwhile physical operation using the switch MUST be unaffected.
So I'm trying to find a way :
1) connect to the internet ( I'm guessing that not during main.py - since I have to check it over and over again during every loop ), and to be sure that I'm not causing some "double parking" of IP's since one was connected in esp8266 ( kind of automatically ), and other during my program
2) same for MQTT
3) all without affecting performance of physical switch
My apologies for my poor English, I'm not a native speaker
I'll try to explain more clearly- I design a Iot device including an ESP32 /ESP8266 ( didn't decide yet, what is best for the job ), using MicroPython. So far- the code is running as expected ( 2 relays work together to control a shade go up and down ), using both a physical button ( 3 states : up, down, off ), together with MQTT for remote commands ( that can additionally run scheduled tasks ).
Now- when a non-nominal scenario occurs, such as:
1) broker resets ( local ) and back up ( or not...)
2) wifi gets disconnected
3) power down, causing not all boot together when all is ready
My goal:
I have to make the code designed to such losses, to regain wifi and MQTT , and meanwhile physical operation using the switch MUST be unaffected.
So I'm trying to find a way :
1) connect to the internet ( I'm guessing that not during main.py - since I have to check it over and over again during every loop ), and to be sure that I'm not causing some "double parking" of IP's since one was connected in esp8266 ( kind of automatically ), and other during my program
2) same for MQTT
3) all without affecting performance of physical switch
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Connect to Wifi
You might want to have a look at Peter's mqtt_as libraray which keeps the esp connected to wifi and mqtt and will automatically reconnect. You have to use uasyncio to use the library: https://github.com/peterhinch/micropython-mqtt
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Connect to Wifi
Indeed. There are also classes to handle switch debouncing under uasyncio in this repo.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.