scanning for wifi networks

CircuitPython 8.1 final was released this past Tuesday on 23 May. I’ve been experimenting with it since downloading images for my Adafruit ESP32-S3 Feather and Raspberry Pi Pico W. What I’ve been looking at is how WiFi is handled on both chips.

The first thing I always do when I get either a new device, or a new version of CircuitPython, or both, is to put a few lines of code onto the code.py file and then have the device execute it and print out the results via the REPL. I use Thonny as the serial interface, because I can also use the tool for developing MicroPython. The critical difference between MicroPython and CircuitPython is that MicroPython does not expose the device’s flash memory as a read/write drive on a hosting computer. CircuitPython does, which makes editing code a lot easier. Anyway…

This is the code.

import binascii as ba
import wifi
networks = {}
for network in wifi.radio.start_scanning_networks():
    if len(network.ssid) > 0 and network.ssid[0] != '\x00':
        networks[network.ssid] = network
wifi.radio.stop_scanning_networks()

for ssid in sorted(networks):
    print("ssid:",ssid, "rssi:",networks[ssid].rssi)

And here is a typical output via Thonny and the REPL.

ssid: ESP32S3-4EF0 rssi: -49
ssid: GuestNetwork rssi: -52
ssid: Hershey rssi: -98
ssid: Miller guest rssi: -94
ssid: NETGEAR04 rssi: -92
ssid: NETGEAR80 rssi: -90
ssid: SmartLife-EEFB rssi: -77

You may find older versions of this code that use an array instead of a dictionary as I did on line 3 of the source. And you many wonder about the conditional test on line 5. Using the older code available all over the web, I was getting duplicate access points as well as what appeared to be junk access points without a valid ssid. The dictionary eliminates duplicates. The conditional test looks to see if the ssid is filled full of binary zeroes instead ascii characters. Since a null-filled ssid is completely filled with binary zeroes, all I have to do is test the first character for a binary zero. If the ssid string is greater than zero and if the first character isn’t binary zero, then add it to the dictionary. When I then sort and print out the dictionary, I get a nice clean alphabetized listing.

I don’t know if the zeroed ssid string is a feature or a bug, but I don’t remember running into this with earlier releases.

circuitpython 8.1.0 final — update

Raspberry Pi Pico W
Raspberry Pi Pico W with headers plugged into a Treedix breakout board

I suppose I should have waited one day before writing about the release of CircuitPython 8.1 because today the developers released 8.1 final. I’ve since installed it onto the few boards I installed the release candidate on, and gave them a few simple tests. One feature I’ve noticed, at least on the Raspberry Pi Pico W, is how fast it appears to execute at the stock CPU clock of 125 MHz compared to earlier releases, especially the 7.x releases. I’ve also been able to change the operational frequency of the Pico W from 125 MHz to at least 250 MHz without it locking up. The CircuitPython code at 250 MHz runs blazingly fast.

One other new feature in 8.1 that has caught my eye is support for Espressif’s ESP-NOW on supported chips. I have enough ESP32-S3 chips to enable ESP-NOW and see how that works between all of them. If you want to know why I’m excited to try this in CircuitPython, you can read about ESP-NOW here: https://www.espressif.com/en/news/ESP-NOW

Right now I feel a bit like a little kid in a toy store (back when there were old-style brick-and-mortar toy stores).

CircuitPython download: https://circuitpython.org/downloads