The ESP32 with MICROPYTHON has 2 WI-FI interfaces
ACCESS POINT INTERFACE
STATION INTERFACE
Normally in a Home Network we have a Router which is connected to Internet Service Provider.
This Router can be Ethernet capable, wireless capable or both.Nowadays mostly Routers are WIFI enabled.Over WIFI you can connect your laptop,mobile or ESP32 to the Router to get internet access.
In this scenario the ROUTER acts as ACCESS POINT & the devices that connect to it act as CLIENTS.
In Micropython CLIENT is also called as STATION.
When a Client/Station connects with the Router , the Router provides an IP address to the device connected.
Suppose the IP of Router is 192.168.0.1 which is also called as GATEWAY IP (this IP is printed on the back of Router) , the Clients will be given IP in the range 192.168.0.xxx .
The last octet only will change on first come first basis.That means the IP offered by DHCP method is DYNAMIC which changes on next power on.
To make it STATIC you can open the Gateway IP on a browser to set up configuration of Router.Here you can note the MAC address of the CLIENT and set the IP to static , so that it will be always offered the same IP on every power ON.
In the second scenario STATION INTERFACE
the ESP32 device can act as ACCESS POINT instead of Router & other devices like mobile or ESP modules can connect with it over WI FI.
In this post we shall explore the ways by which ESP32 acts as a CLIENT/STATION and connect with an ACCESS POINT like Router over WIFI automatically on Power ON.
For Micropython IDE we use THONNY PYTHON
Refer my previous post on getting started with Thonny python
https://alselectro.wordpress.com/2020/02/09/micropython-on-esp32/
This interface can be managed using the network
module from MicroPython.
First import the network module.
Then create an object that represents this interface:
Create a WLAN network interface object. One of the Supported interfaces are
network.STA_IF
(station or client, connects to upstream WiFi access points)
Make that network interface active.
Scan for the available wireless networks using
station.scan()
Scanning is only possible on STA interface. Returns list of tuples with the information about surrounding WiFi access points:
The tuples returned here have the parameters of each available Wi-Fi connection. In particular, the first element in each tuple is the SSID name, the third element is the channel number, and the fourth is the RSSI or signal strength indicator.
(ssid, bssid, channel, RSSI, authmode, hidden)
ssid is the service set identifier
bssid is hardware address of an access point, in binary form, returned as bytes object. You can use ubinascii.hexlify()
to convert it to ASCII form.
RSSI signal strength in negative value
There are five values for authmode: 0- open ,1 –WEP , 2 –WPA-PSK , 3- WPA2-PSK ,4 WPA/WPA2-PSK
and two for hidden: 0 (false)-VISIBLE, 1- HIDDEN
You can check the connection using method isconnected()
As we have not yet connected it returns FALSE.
Connect to WI FI using your SSID name and its password.
ifconfig() method returns the parameters of the connection
IP Address allotted along with Subnet mask,Gateway IP & DNS Server
As we have tested the connection using Micropython Interpreter , we can implement auto connection to WIFI as follows :
Micropython executes
boot.py
on power up first.Then it looks out for
main.py
Under Thonny python IDE click File –> New
This file we shall save as boot.py
For intimation we shall include only a print statement
print(‘RUN : boot.py’)
Click on File –> Save As
You get 2 options 1. to store on your computer for later modification
2. On the Micropython device (ESP32) directly.
Provide the file name as boot.py and save it on MicroPython device.
Again File –> New
to create the second file main.py
Here we import module ConnectWifi & call the function connect()
The module & function have to be created by us.
For now save it on ESP32 as file main.py.
Remember not to change the file name, as Micropython will look to execute only main.py automatically.
Finally we have to create the file ConnectWifi.py
and create the function connect()
In this function replace the ssid,password with that of yours.
Save this file on to Micropython Device.
Download the .py files from here :
http://www.alselectro.com/files/wifi_code.zip
Finally click on F5 or the Green arrow button to execute.
ESP32 will auto connect to your SSID.
Video support :