ESP32 MicroPython: Connecting to a WiFi Network

The objective of this post is to explain how to connect to a WiFi network using MicroPython on the ESP32.


Introduction

The objective of this post is to explain how to connect to a WiFi network using MicroPython on the ESP32. The procedure shown here is based on the guide provided for the ESP8266, on the MicroPython documentation website, which I encourage you to read.

We are going to execute the Python code by sending commands to the prompt, so we can see things step by step. If you haven’t yet configured MicroPython on the ESP32, please check this previous post.

I will be using Putty to establish the serial connection to the Python prompt, but you can use other software that allows this kind of connection.


The code

First of all, we will need to import the network module, in order to access all the functions needed for establishing the connection to the WiFi Network.

import network

Once you import the module, some information is printed on the console, as shown in figure 1. It won’t be needed for this tutorial, but I will leave it here for illustration purposes.

ESP32 MicroPython Importing network module

Figure 1 – Importing the MicroPython network module.

Since we are going to connect to a WiFi network, our device will operate in station mode. So, we need to create an instance of the station WiFi interface [1]. To do it, we just need to call the constructor of the WLAN class and pass as its input the identifier of the interface we want. In this case, we will use the network.STA_IF interface.

station = network.WLAN(network.STA_IF)

Now we will activate the network interface by calling the active method on our station object and passing True as input, since it accepts Boolean values.

station.active(True)

Again, after executing this command, you should get an output on the command line, indicating that we are in station mode and that the interface was started, as shown in figure 2.

ESP32 MicroPython activating station mode

Figure 2 – Activating station mode.

Finally we will use the connect method to connect to the WiFi network. This method receives as input both the SSID (network name) and the password.

station.connect("YourNetworkName", "YourNetworkPassword")

It will again print some information to the console. Take in consideration that the connection may take a while. Also note that the “>>>>” is not printed once the connection is established, so it may seem that the device is still processing after the last message is printed, as shown in figure 3. You can hit enter to continue normally.

ESP32 MicroPython connecting to WiFi Network

Figure 3 – Connecting to the WiFi network.

Finally, we will confirm the connection by calling the isconnected method, which returns true if the device is connected to a WiFi network [2]. We are also going to call the ifconfig method, which returns the IP address, subnet mask, gateway and DNS as output parameters [2].

station.isconnected()
station.ifconfig()

Check bellow in figure 4 the final output for these commands, which indicate that we are correctly connected to a WiFi network.

ESP32 MicroPython print local IP

Figure 4 – Confirming the connection to the WiFi network.

Note that the IP assigned to the ESP32 is local, so we can’t use it to receive connections from outside our network without portforwarding the router.


References

[1] http://docs.micropython.org/en/v1.8.7/esp8266/esp8266/tutorial/network_basics.html

[2] https://docs.micropython.org/en/latest/esp8266/library/network.html#module-network

26 thoughts on “ESP32 MicroPython: Connecting to a WiFi Network”

  1. Pingback: ESP32 / ESP8266 MicroPython: Automatic connection to WiFi | techtutorialsx

  2. Pingback: ESP32 / ESP8266 MicroPython: Automatic connection to WiFi | techtutorialsx

  3. Pingback: ESP32 / ESP8266 MicroPython: HTTP GET Requests | techtutorialsx

  4. Pingback: ESP32 / ESP8266 MicroPython: HTTP GET Requests | techtutorialsx

  5. Pingback: ESP32 / ESP8266 MicroPython: HTTP POST Requests | techtutorialsx

  6. Pingback: ESP32 / ESP8266 MicroPython: HTTP POST Requests | techtutorialsx

  7. Pingback: ESP32 Arduino: Getting started with WiFi | techtutorialsx

  8. Pingback: ESP32 Arduino: Getting started with WiFi | techtutorialsx

  9. Pingback: ESP32 MicroPython: HTTP Webserver with Picoweb | techtutorialsx

  10. Pingback: ESP32 MicroPython: HTTP Webserver with Picoweb | techtutorialsx

Leave a Reply to FarhanCancel reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading