How to Scan WiFi Networks Using MicroPython?

Estimated read time 2 min read

To scan WiFi networks using MicroPython on an ESP32 or ESP8266 board, you can use the network module provided by MicroPython. Here’s an example code snippet to get you started:

import network

# Initialize the WiFi interface
wifi = network.WLAN(network.STA_IF)

# Activate the WiFi interface
wifi.active(True)

# Scan for available WiFi networks
networks = wifi.scan()

# Print the details of each network
for network in networks:
    ssid = network[0].decode('utf-8')
    rssi = network[3]
    print("SSID: {}, RSSI: {}".format(ssid, rssi))

In this example, the network.WLAN() class is used to create a WiFi object named wifi. The STA_IF argument specifies that we want to use the WiFi station mode.

By calling wifi.active(True), we activate the WiFi interface.

Next, the wifi.scan() method is used to scan for available WiFi networks. The method returns a list of network information, where each network is represented as a tuple containing various details such as the SSID (network name), BSSID (MAC address), channel, and RSSI (signal strength).

The code then iterates over each network and extracts the SSID and RSSI. The SSID is decoded from bytes to a string using the decode('utf-8') method. Finally, the SSID and RSSI are printed for each network.

Keep in mind that this example assumes you have already connected your ESP32 or ESP8266 board to a WiFi network. If not, you will need to configure and connect to a WiFi network using the appropriate methods from the network.WLAN() class before scanning for networks.

Make sure you have the necessary MicroPython firmware installed on your board, and adjust the code if needed based on your specific board and configuration.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply