Interesting. The first time I tried it everything worked as expected, scanned, connected, scanned again and things turned up. The second time it didn't, it took some time to do the scan but nothing was reported in the second scan. This was my code -First run -Second run -But, if I put that "After connecting" scan into a loop it does then work on the second and subsequent scans -Looks like there is some sort of issue which probably needs reporting but it seems there may be a Robert the Bruce workaround; if at first it doesn't succeed, returns no results, try and try again.
Code:
import networkimport timeimport secretswlan=network.WLAN(network.STA_IF)wlan.active(True)def FixSsid(ssid): if ssid == b"\x00" * len(ssid): return "<00> * {}".format(len(ssid)) s = "" for b in ssid: if b < 0x20 or b > 0xFE: s += "<{:02X}>".format(b) else: s += chr(b) s = "X" * len(s) # Obfuscate the ssid return sdef HexBssid(bssid): s = "" for b in bssid: s += "{:02X}:".format(b) return s[:-1]def BarRssi(rssi): n = 90 + rssi if n < 0: n = 0 return "*" * (int(n/15.0+0.5)+1)def Scan(desc): print(desc) accesspoints = wlan.scan() for ap in accesspoints: ssid, bssid, chan, rssi, security, hidden = ap print("{:<16} {:<7} {} {:>2} {:>3} {} {}".format( FixSsid(ssid), BarRssi(rssi), HexBssid(bssid), chan, rssi, security, hidden))def Connect(): print("Connecting to router") wlan.connect(secrets.SSID, secrets.PASSWORD) while wlan.status() != 3: print(" waiting") time.sleep(1) print("Connected to router") info = wlan.ifconfig() desc = ["IP Address", "Netmask", "Gateway", "DNS"] for n in range(4): print(" {:<10} : {}".format(desc[n], info[n]))Scan("Before connecting to router ...")Connect()Scan("After connecting to router ...")
Code:
pi@Pi3B:~/pico/micropython/ports/picopython/pico_w $ mptool run wifi_scan_connect_scan.pyExecuting 'wifi_scan_connect_scan.py' on the deviceBefore connecting to router ...XXXXXXXXXXX **** E4:F4:C6:B1:3C:40 4 -38 7 9XXXXXXXXXX ** 20:B0:01:AB:FB:6F 1 -81 5 2XXXXXXXXXXXXXX ** CC:2D:21:76:1B:31 1 -79 7 1XXXXXXXXXXXXXX * 64:66:24:1A:0E:7F 1 -86 5 2XXXXXXX * 7A:B7:BD:75:8F:A4 1 -90 0 1XXXXXXXXX * A4:05:D6:24:0C:0C 1 -89 5 1XXXXXXXXX **** E4:57:40:45:26:03 6 -44 5 7XXXXXXXXXXX *** 50:6A:03:52:5E:E8 6 -59 5 7XXXXX ** B0:95:75:68:6A:51 6 -73 7 6XXXXXX * 04:D9:F5:2C:50:60 8 -89 5 1XXXXXXXXX * 64:FD:96:69:3B:D0 11 -92 5 2Connecting to router waiting waiting waiting waiting waiting waiting waitingConnected to router IP Address : 192.168.0.210 Netmask : 255.255.255.0 Gateway : 192.168.0.1 DNS : 192.168.0.1After connecting to router ...XXXXXXXXXXX ***** E4:F4:C6:B1:3C:40 4 -37 7 9XXXXXXXXXXX *** 50:6A:03:52:5E:E8 6 -59 5 5XXXXXXXXX **** E4:57:40:45:26:03 6 -45 5 4XXXXX ** B0:95:75:68:6A:51 6 -69 7 2XXXXXXXXX * 64:FD:96:69:3B:D0 11 -93 5 2pi@Pi3B:~/pico/micropython/ports/picopython/pico_w $
Code:
pi@Pi3B:~/pico/micropython/ports/picopython/pico_w $ mptool run wifi_scan_connect_scan.pyExecuting 'wifi_scan_connect_scan.py' on the deviceBefore connecting to router ...XXXXXXXXXXX ***** E4:F4:C6:B1:3C:40 4 -37 7 6XXXXXXXXX **** E4:57:40:45:26:03 6 -45 5 7XXXXX ** B0:95:75:68:6A:51 6 -68 7 2XXXXXXXXXXX *** 50:6A:03:52:5E:E8 6 -59 5 4XXXXXXXXX * 64:FD:96:69:3B:D0 11 -94 5 2XXXXXXXXXX ** 20:B0:01:AB:FB:6F 1 -81 5 1Connecting to routerConnected to router <-- Instant connection IP Address : 192.168.0.210 Netmask : 255.255.255.0 Gateway : 192.168.0.1 DNS : 192.168.0.1After connecting to router ... <-- Nothing reportedpi@Pi3B:~/pico/micropython/ports/picopython/pico_w $
Code:
Scan("Before connecting to router ...")Connect()while True: Scan("After connecting to router ...")
Code:
pi@Pi3B:~/pico/micropython/ports/picopython/pico_w $ mptool run wifi_scan_connect_scan.py...Connecting to routerConnected to router <-- Instant connection IP Address : 192.168.0.210 Netmask : 255.255.255.0 Gateway : 192.168.0.1 DNS : 192.168.0.1After connecting to router ... <-- Nothing reportedAfter connecting to router ...XXXXXXXXXXX ***** E4:F4:C6:B1:3C:40 4 -37 7 8XXXXXXXXX **** E4:57:40:45:26:03 6 -46 5 5XXXXXXXXXXX *** 50:6A:03:52:5E:E8 6 -62 5 3XXXXXXXXX * 18:35:D1:4D:52:11 11 -94 5 1After connecting to router ...XXXXXXXXXXX **** E4:F4:C6:B1:3C:40 4 -38 7 8etc
Statistics: Posted by hippy — Wed Jan 10, 2024 11:26 am