MicroPython & ESP8266 : Connect to WiFi Router

WiFi Oct 03, 2019

The network module is used to configure the WiFi connection. There are two WiFi interfaces, one for the station (when the ESP8266 connects to a router) and one for the access point (for other devices to connect to the ESP8266).

Make sure your ESP8266 is connected either to your computer or power source over its microUSB connection.
Run all the commands in REPL
Step 1: Create instances of these objects using:

 import network
 sta_if = network.WLAN(network.STA_IF)
 ap_if = network.WLAN(network.AP_IF)

You can check if the interfaces are active by:

 sta_if.active()
 False
 ap_if.active()
 True

Step 2: Activate the station interface:

 sta_if.active(True)

Step 3: Connect to your WiFi network:

 sta_if.connect('<your ESSID>', '<your password>')

Voila! You are now connected to your WiFi using NodeMCU.

To check if the connection is established use:

 sta_if.isconnected()

Once established you can check the IP address:

 sta_if.ifconfig()
Note :

Please do let us know if we are missing any topic or you want us to elaborate any topic, comment below and subscribe to be updated.