import network
import time
import binascii
# -- Configuration ---
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = "" # Wokwi-GUEST has no password
# 1. Initialize the Wi-Fi Interface
# STA_IF stands for "Station Interface"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
def scan_networks():
print("Scanning for available Wi-Fi...")
networks = wlan.scan() # Returns list of tuples
print(f"Found {len(networks)} networks:")
for net in networks:
ssid = net[0].decode('utf-8')
rssi = net[3]
print(f" > {ssid:20} | Signal: {rssi} dBm")
print("-" * 30)
def connect_to_wifi():
print(f"Attempting to connect to: {WIFI_SSID}")
wlan.connect(WIFI_SSID, WIFI_PASS)
# Wait for connection with a timeout
max_wait = 10
while max_wait > 0:
if wlan.isconnected():
break
max_wait -= 1
print('Waiting for connection...')
time.sleep(1)
if wlan.isconnected():
print("Successfully Connected!")
# ifconfig returns (IP, Subnet, Gateway, DNS)
status = wlan.ifconfig()
print(f"IP Address: {status[0]}")
print(f"Gateway: {status[2]}")
else:
print("Connection Failed!")
# --- Execution ---
scan_networks()
connect_to_wifi()
# Keep the connection alive
while True:
if wlan.isconnected():
# Blink onboard LED to show heartbeat (GP-WL is internal)
# Note: In Pico W, LED is on the Wi-Fi chip, not GPIO 25
pass
time.sleep(5)