import network
import time

SSID = "Pilamat"
PASSWORD = "24563788"

def connect_wifi(timeout=20):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)

    if wlan.isconnected():
        return wlan

    print("Connexion à", SSID, "...")
    wlan.connect(SSID, PASSWORD)

    start = time.time()
    while not wlan.isconnected():
        if time.time() - start > timeout:
            raise RuntimeError("Échec de connexion (timeout)")
        time.sleep(0.5)

    print("Connecté !")
    print("Config réseau (IP, masque, passerelle, DNS) :", wlan.ifconfig())
    return wlan

wlan = connect_wifi()