import network
from machine import Pin
from time import sleep
ssid = "Mohamed"
password = "hello_world"
led = Pin(1, Pin.OUT)
led.off()
print("Connecting to WiFi...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
max_wait = 0
while wlan.isconnected() == 0:
if wlan.isconnected():
print("Connected to Wi-Fi!")
break
max_wait += 1
print(f"Waiting for Wi-Fi connection... {max_wait}")
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)