Connect to Wifi

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Connect to Wifi

Post by guyd » Thu Aug 30, 2018 6:21 pm

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 ?

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

Re: Connect to Wifi

Post by pythoncoder » Fri Aug 31, 2018 6:11 am

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.
Peter Hinch
Index to my micropython libraries.

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: Connect to Wifi

Post by guyd » Fri Aug 31, 2018 6:56 am

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

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

Re: Connect to Wifi

Post by pythoncoder » Sun Sep 02, 2018 6:47 am

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).
Peter Hinch
Index to my micropython libraries.

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: Connect to Wifi

Post by guyd » Sun Sep 02, 2018 7:12 am

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

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Connect to Wifi

Post by kevinkk525 » Sun Sep 02, 2018 9:28 am

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

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

Re: Connect to Wifi

Post by pythoncoder » Tue Sep 04, 2018 5:27 pm

Indeed. There are also classes to handle switch debouncing under uasyncio in this repo.
Peter Hinch
Index to my micropython libraries.

Post Reply