import network
import time
from machine import Pin
# Wi-Fi credentials from wokwi.toml
ssid = "Wokwi-SSID"
password = "Wokwi-PASS"
# Set up LED pin
led = Pin(2, Pin.OUT)
# Turn the LED on as an initial state
led.value(1)
# Connect to Wi-Fi
print("Connecting to WiFi...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connection to be established
max_wait = 10
while max_wait > 0:
if wlan.isconnected():
print("Connected to Wi-Fi!")
break
max_wait -= 1
print("Waiting for Wi-Fi connection...")
time.sleep(1)
# Control the LED based on the connection status
if wlan.isconnected():
# Turn LED off if connected
print("Wi-Fi is connected, turning LED ON.")
led.on()
time.sleep(2)
else:
# If connection failed, blink the LED
print("Failed to connect to Wi-Fi. Blinking LED.")
while True:
led.toggle()
time.sleep(0.5)