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 using the GetOnline service provided by Computing Services.
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)
Ping Test¶
This script will connect to the campus CMU-DEVICE WiFi network and measure ping times to the main campus web server. This local network does not require a password but does require registration of the device WiFi MAC using GetOnline.
Direct download: ping_test.py.
1# ping_test.py
2# to try this, copy it to the CIRCUITPY drive as code.py
3
4import time
5import wifi
6import ipaddress
7import socketpool
8
9# name of the host to ping
10ping_host = "www.cmu.edu"
11
12# fetch the device address from the WiFi module
13mac = ':'.join([("%02x" % b) for b in wifi.radio.mac_address])
14print("device WiFi MAC:", mac)
15
16print("Attempting to connect to the campus CMU-DEVICE WiFi.")
17wifi.radio.connect(ssid="CMU-DEVICE")
18
19print("device IP addr:", wifi.radio.ipv4_address)
20
21# use DNS to find the host address
22pool = socketpool.SocketPool(wifi.radio)
23addr = pool.getaddrinfo(ping_host, port=80)
24
25# extract the host address field from the resulting list of tuples
26host_ip = addr[0][4][0]
27
28print("pinging", ping_host, "at", host_ip)
29ip = ipaddress.ip_address(host_ip)
30
31while True:
32 print("ping time:", wifi.radio.ping(ip))
33 time.sleep(5)