Pico W Scan Networks, looking for Micro Python code

I have the following and it works.

import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print(wlan.scan())

The output isn’t the best though. Was wondering if anybody has something better and more refined? Something that gives a nice list format like the Pico Wireless example.

import picowireless
import time

picowireless.init()

picowireless.start_scan_networks()

while True:
    networks = picowireless.get_scan_networks()
    print("Found {} network(s)...".format(networks))
    for network in range(networks):
        ssid = picowireless.get_ssid_networks(network)
        print("{}: {}".format(network, ssid))
    print("\n")
    time.sleep(10)

scan() returns a tuple - (ssid, bssid, channel, RSSI, security, hidden) so you can do something like:

import network

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
networks = wlan.scan()

print("{} networks found:\n".format(len(networks)))
print ("{:<64} {:<8} {:<6})".format('ssid', 'channel', 'rssi'))
for net in networks:
    print ("{:<64} {:<8} {:<6}".format(net[0].decode(), net[2], net[3]))

Andy

1 Like

Thanks for that, will give it a go sometime tomorrow. In chill out mode at the moment, its just about 9 PM here. I’m self taught Python and Micro Python wise. I can usually tell what a block of code does, just not how it does it. ;)

Welcome aboard the Pimoroni Forums. =)

No worries… hopefully you can get why you need. BTW should have said scan() returns a list of tuples

I’ve done a fair bit of Python programming but new to MicroPython - finding the documentation a bit patchy but getting good at digging through GitHub!

Andy

Just gave it a try.

>>> %Run -c $EDITOR_CONTENT
5 networks found:

ssid                                                             channel  rssi  )
BELLALIANT568                                                    1        -95   
alphanumeric                                                     6        -52   
BELL815                                                          4        -69   
BELL003                                                          9        -76   
Brandan-2g_EXT                                                   11       -94   
>>>