
Timezone: Europe/London
Development takes place in two locations, and remote units are placed in some
other locations with access to an internet connection via wi-fi, so I needed a way to connect to any abailable wi-fi network that I had a password for.
I also needed this functionality to be seamless, in that I did not want to hardcode the connection function and end up with different code versions depending on location.
This was also important as units are often moved from on location to another, or brought into the office for upgrades, and I wanted them to continue working constantly
wherever they were without any user intervention.
So this is what I came up with.
The available SSID and Password pairs are stored in two tuples.
SSID = ('blank', 'TP-Link_AA0B', 'SKYZKJYK', 'FRITZ!Box 7530 SX', 'NOVA_U2G9_A2', 'Gigaclear_2C42')
PASS = ('blank', '12345678', 'AAAAAAA', '5432154321', 'BBBBBBBB', 'abcdefgh')
The first string in each is 'blank' so the connection function can return 0 if no connection found.
The first line of the function finds the number of possible SSIDs it could connect to. It scans for all SSIDs in range every 2 seconds 10 times.
On each pass, it checks the SSID of all those found against the list in the tuple SSID. If there is a match, it tries for 30 seconds to connect to it using the corresponding password from the tuple PASS.
Using this function, the only thing you need to change in your code are the possible SSIDs you could connect to in the two tuples at the head of the code, and numerous units could connect to any known router regardless of location, all using the same code.
IDE editors for programming ESP boards.
Micropython editors
Micropython functionality and libraries.
Micropython.org
Find the uPython firmware for your board.
uPython firmware for development boards.
ESP32 uPython modules and function examples.
Quick reference for the ESP32
Scanning and connecting to multiple SSIDs
Using an SD card reader breakout board
OTA Updating running software without interuption
Future articles
Communication between ESPs
Using an Adafruit GPS module
Hassle free web communication with urequests.py.