In this new tutorial dedicated to MicroPython firmware, we will learn how to connect an ESP8266 or ESP32 module to a WiFi network. At first, we will see how to connect manually using the REPL interpreter. Then we’ll see how to create a login script that automatically handles the connection when the module starts. If you are on Windows, I advise you to install and use the uPiCraft IDE presented previously. It is a code editor dedicated to the MicroPython language. uPiCraft can develop and manage its code on all development boards supported by MicroPython (pyboard, WiPy, ESP8266, ESP32, STM32 …). To make the MicroPython accessible to beginners, I used uPiCraft in all MicroPython tutorials. You can also use screen on macOS / Linux or Putty on Windows.
A little reading before you start
If you are new to MicroPython, I advise you to read these articles which will help you to take in hand this language:
- Introducing MicroPython, How to Install the rshell Tool to Access the REPL Interpreter
- Presentation of the uPiCraft IDE dedicated to MicroPython
Manual connection to a WiFi network from the REPL shell
Connect the ESP8266 / ESP32 card and run uPiCraft. Go to the Tools then Serial menu and choose the COM port to which the card is connected. Connect to the map to launch the REPL interpreter by clicking on the connection icon.
When REPL is started, the command prompt is symbolized by >>>. The files are listed under the Device in the left sidebar.
We start by importing the Network module
The WLAN class of the network module contains all the network management methods. The ESP8266 / ESP module can act as an access point (network.AP_IF mode) or as a client (network.STA_IF mode). We will create an instance of the WiFi object of type network.STA_IF. run
station = network.WLAN(network.STA_IF)
The WiFi interface must then be enabled using the active method (True / False).
All that remains is to connect to a WiFi network using the connect() method. The connect () method needs the network name as well as the password. In return, the function returns the IP address assigned by the DHCP server to the module.
>>> station.connect("nomReseau", "MoteDePasseReseau") >>> stationscandone state: 0 -> 2 (b0) state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 8 cnt connected with X72, channel 1 dhcp client start... .isip:192.168.1.81,mask:255.255.255.0,gw:192.168.1.1
There are several methods used to test (anywhere in your scripts) the state of the WiFi connection:
- isconnected(), whether the module is connected to the network
- ifconfig(), retrieves the IP address of the module, the IP address of the DHCP router
Assign a fixed IP to a MicroPython ESP8266 / ESP32 module
When there are many connected objects, it is best to assign a fixed IP address. It will be easier to access a WEB interface, carry out maintenance operations with WebREPL. For this, we use the ifconfig() method. The IP address, the subnet, the DHCP server and the DNS server are passed to him (the one of Google will do, 220.127.116.11). Then we connect with the method connect(ssid, password) simply.
>>> station.disconnect() >>> station.status() >>> station.ifconfig(('192.168.1.10', '255.255.255.0', '192.168.1.1', '18.104.22.168')) >>> station.connect()
There you go.
MicroPython script for automatic connection
Now, we just have to automate all that. We could just write the login code in the boot.py script. It is better to deport the code in a dedicated script. Maintenance will be easier and it will be easier to use in other scripts.
Create a new script and paste the code below. Modify the ip, subnet, gateway, dns, ssid and password constants. Save the script as ConnectWifi.py (for example)
def connect(): import network ip = '192.168.1.10' subnet = '255.255.255.0' gateway = '192.168.1.1' dns = '22.214.171.124' ssid = "yourSSID" password = "yourPassword" station = network.WLAN(network.STA_IF) if station.isconnected() == True: print("Already connected") return station.active(True) station.ifconfig((ip,subnet,gateway,dns)) station.connect() while station.isconnected() == False: pass print("Connection successful") print(station.ifconfig()) def disconnect(): import network station = network.WLAN(network.STA_IF) station.disconnect() station.active(False)
Upload the script to the map (Tools -> Download). From the REPL interpreter, execute import ConnectWifi then ConnectWifi.connect() . The disconnect() method allows you to disconnect the WiFi module from your scripts. Now, we can very simply launch the WiFi connection from anywhere like this
import ConnectWifi ConnectWiFi.connect()
For example, if you want to start the connection at startup, just open the boot.py script and add the two previous commands.
Resources used to write this article:
- ESP32 MicroPython project with several DS18B20 probes, publish the measurements to Domoticz using the HTTP JSON API
- OLED display SSD1306 in MicroPython, example with a I2C digital barometer BME280 (or BMP180)
- MicroPython tutorial, manage WiFi connection at startup on ESP8266 / ESP32
- uPiCraft, a MicroPython IDE dedicated to the development of IoT based on ESP8266, ESP32, microbit, pyBoard
- How to reinstall the MicroPython firmware on an ESP8266 or ESP32 with the esptool.py script
- Unpacking the Wemos ESP32 Lolin32 Lite, testing the firmware MicroPython with a Raspberry Pi 3