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

Advertisements

12 Replies to “ESP32 MicroPython: Connecting to a WiFi Network”

  1. Nice tutorial. But for me “import network” doesn’t do anything.Where the problem might be ?
    >>>
    >>>
    >>> import network
    >>> print (“Hello World From ESP32 MicroPython”)
    Hello World From ESP32 MicroPython
    >>> import network
    >>>

    Liked by 1 person

    1. Hi!

      Thank you very much for the feedback 🙂

      I’ve not been using MicroPython for a while, but at the time I’ve written this tutorial the import would print some information, as illustrated.

      Nonetheless, things most likely have changed in newer versions on MicroPython, so they may have removed those prints.

      If you follow the rest of the tutorial, are you able to connect to the network?

      Best regards,
      Nuno Santos

      Like

  2. I followed the above and it worked perfectly. I also didnt get the output that was described in your article, but when I checked the status with station.ifconfig() it reported the correct IP address and was pingable from another machine on the same network.

    Great article.

    Thanks

    Liked by 1 person

    1. Hi!

      Thank you very much for the feedback 🙂

      Most likely those verbose outputs were removed from newer versions of MicroPython.

      I’ve got to find some time to go back to MicroPython, hopefully there’s plenty of new stuff to catch up with 🙂

      Best regards,
      Nuno Santos

      Like

  3. Hello Everyone,
    I am new to esp32 and micropython.I am try to connect my esp32 to raspberry pi(working as AP).But when I run command “import network” nothing happens…..Please,help to resolve this issue.

    Liked by 1 person

    1. Hi!

      I’ve written this tutorial a while ago and at that time, when importing the network module, it would print some stuff to the console.

      Nonetheless, the usual behavior when importing a module is not printing anything to the console, so most likely in new versions of MicroPython importing the network module doesn’t output anything.

      If you proceed with the tutorial, doesn’t it work?

      Best regards,
      Nuno Santos

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s