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 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
    if len(network.ssid) > 0 and network.ssid[0] != '\x00':
        networks[network.ssid] = network

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.