• EUR€
  • £GBP
  • $USD

ESP32 MicroPython Tutorial: Connecting to a WiFi Network

DFRobot Jul 11 2017 1271

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


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.

1 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.

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.

1  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.

1   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.

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.
1  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.
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].

1  station.isconnected()
2  station.ifconfig()

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

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.

NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.
He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.

DFRobot supply lots of esp32 arduino tutorials  and esp32 projects for makers to learn.