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