WiFi Networking Examples - Raspberry Pi Pico 2W

The following short Python programs will demonstrate essential operation of the Raspberry Pi Pico 2W board. These use only the onboard hardware and the serial console. Each can be run by copying the program into code.py on the CIRCUITPY drive offered by the board. The text can be pasted directly from this page, or each file can be downloaded from the CircuitPython sample code folder on this site.

Related Pages

Scan WiFi

This script will report all networks visible to the WiFi chip on the Pi Pico 2W.

Direct download: scan_wifi.py.

 1# scan_wifi.py
 2# https://learn.adafruit.com/networking-in-circuitpython/networking-with-the-wifi-module
 3
 4# to try this, copy it to the CIRCUITPY drive as code.py
 5
 6import wifi
 7networks = []
 8
 9print("Beginning WiFi network scan.")
10
11for network in wifi.radio.start_scanning_networks():
12    networks.append(network)
13
14wifi.radio.stop_scanning_networks()
15
16networks = sorted(networks, key=lambda net: net.rssi, reverse=True)
17
18for network in networks:
19    print("ssid:", network.ssid, "rssi:", network.rssi)
20
21print("scan_wifi done.")

Identify WiFi MAC

This script will report the 48 bit hardware network address of the WiFi chip on the Pi Pico 2W. At CMU this string is required to register the device for the campus network.

It is short enough it can be pasted directly into the command line via your terminal emulator.

The mac_address property behaves like a bytestring with six values. The string formatter converts each byte value into a two-digit hex number, the list comprehension builds a list of these values, and the join operation concatenates the strings into the conventional form separated by colons.

Direct download: identify_wifi_MAC.py.

 1# identify_wifi_MAC.py
 2
 3# Display the hardware address from the WiFi chip.
 4# This may be needed for requesting network access.
 5
 6# To try this, copy it to the CIRCUITPY drive as code.py, or just paste it at the REPL command line.
 7
 8import wifi
 9mac = ':'.join([("%02x" % b) for b in wifi.radio.mac_address])
10print("WiFi MAC:", mac)