import network
from machine import Pin
from time import sleep
ssid = "Mohamed"
password = "hello_world"
led = Pin(2, Pin.OUT)
led.off()
print("Connecting to WiFi...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
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...")
sleep(1)
# Control the LED based on the connection status
if wlan.isconnected():
# Turn LED on if connected
print("Wi-Fi is connected, turning LED ON.")
led.on()
sleep(2)
else:
# If connection failed, blink the LED
print("Failed to connect to Wi-Fi. Blinking LED.")
while True:
led.toggle()
sleep(0.5)