Nearly all the devices we use today — from smartphones, tablets, wearables, and even many of our appliances — connect to the Internet via a home (or office) WiFi network. Internet access to smart devices is typically managed through a web interface or mobile app.
Similarly, when we’re out and about, we’ll access WiFi for our mobile devices by connecting to a specific network and inputting the password. However, managing Internet access can be more challenging with self-built Internet of things (IoT) devices. This is because these credentials are usually hard-coded in IoT devices.
But what if a MicroPython port as simple as ESP32 could serve as a universal WiFi manager? This would allow all IoT-connected devices (including our smartphones, laptops, and wearables) to connect to random WiFi networks — without inputting passwords each time.
A WiFi manager configured on ESP32 will scan for the available networks and store the credentials for current and future use, saving time and energy.
Components required
- ESP32 or ESP8266 board x1
Prerequisites
You must have uploaded MicroPython firmware to ESP32/ESP8266 and installed uPyCraft IDE or Thonny IDE on your computer. Click here to learn how to install uPyCraft IDE and upload MicroPython firmware to ESP32/ESP8266.
Circuit connections
There are no connections required. It’s only necessary to set up and configure ESP32/ESP8266 to work as a WiFi manager.
The MicroPython library
For this project, we’ll use the WiFi manager library built by Tayfunulu. It’s available on Github via this link. The source code of the library is below.
To upload the WiFi manager library to ESP32/ESP8266, connect the ESP board to your computer via a Micro-USB cable. Open uPyCraft IDE and navigate to File->New. Copy the WiFi manager library’s source code to the new file and click the save button.
Name the new file ‘WiFiManager.py,’ and save it by clicking OK.
Click the ‘Download and Run’ button to upload the library to the ESP board.
The MicroPython script
The WiFi manager library must be implemented in the main application code in the MicroPython firmware. To do so, create a new file by navigating to File->New and save it as ‘main.py.’
Copy the code below to main.py. Then, upload it to the ESP board by clicking the ‘Download and Run’ button.
How the WiFi manager works
When the ESP board boots for the first time after the code is uploaded, it’s set as a WiFi access point. This point is seen as the ‘WiFiManager,’ which can be connected to a computer or mobile.
The password to connect with the WiFiManager is ‘tayfunulu.’ The password and SSID of the ESP WiFiManager can be changed by modifying the WiFiManager.py.
After connecting to the WiFiManager, open its dashboard by going to the IP address 192.168.4.1. The available WiFi connections will be displayed on the webpage. Select a WiFi connection and enter its network key to configure the connection with the ESP board. After the correct credentials are entered, the WiFi connection can be accessed by any device by connecting to the WiFiManager.
The code
The main application code imports the WiFiManager library. The available WiFi connections are scanned by calling the WiFiManager.get_connection() method. If there is no WiFi network available, the message “Could not initialize the network connection” is printed to the console. Otherwise, the WiFi connections will be accessible on 192.168.4.1.
When the ESP board first boots as an access point, it leaves a socket open — which can crash the ESP board. To prevent this, the socket connection is managed by a ‘try-except’ exception handling routine.
The main application code then hosts the webpage for connecting with an available WiFi connection, as well as a webpage that controls the LED on the ESP board.
Testing the WiFi manager
After uploading the main application code, reset the ESP board by pressing the RESET button. The ESP WiFiManager will be activated, and a message will be printed to the MicroPython console.
Next, open the WiFi settings on your computer or smartphone and connect to the WiFiManager network.
Once you’ve connected to the WiFiManager network, open the URL 192.168.4.1 in a web browser. You’ll see the available WiFi network connections. Select one and enter its password. Then, save the credentials to the WiFiManager by clicking the submit button.
The ESP board is now connected to the WiFi network. Any other devices you have can also directly access all the available WiFi networks by connecting to the WiFiManager without putting in a password or hard coding the credentials in the application code.
Conclusion
The WiFi manager built in this project saves time, making WiFi easily accessible to all of your smart devices, including laptops and smartphones. So, you will no longer have to input the username and password into each one; nor will you have to hard code the WiFi credentials into each device. A WiFi manager configured on ESP32 scans for the available networks and stores the credentials for current and future use.
You may also like:
Filed Under: Electronic Projects, ESP8266
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.