import network
import time
import urequests
# Wi-Fi Wokwi
SSID = "Wokwi-GUEST"
PASSWORD = ""
# ====== OpenWeatherMap ======
API_KEY = "9be2197f632d233f5c67f6a24a8165ce" # <-- mets ta clé ici
# Coordonnées approx de Fès (Maroc)
LAT = 34.0331
LON = -5.0003
URL = "https://api.openweathermap.org/data/2.5/weather?lat={}&lon={}&appid={}&units=metric&lang=fr".format(
LAT, LON, API_KEY
)
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
wlan.connect(SSID, PASSWORD)
print("Connexion Wi-Fi...")
t0 = time.ticks_ms()
while not wlan.isconnected():
if time.ticks_diff(time.ticks_ms(), t0) > 15000:
raise RuntimeError("Timeout Wi-Fi")
time.sleep(0.2)
print("✅ Connecté :", wlan.ifconfig())
def show_weather(data):
# Champs principaux
name = data.get("name", "Fès")
weather = (data.get("weather") or [{}])[0]
main = data.get("main", {})
wind = data.get("wind", {})
sys = data.get("sys", {})
print("\n==============================")
print("🌍 Ville :", name, "-", sys.get("country", "MA"))
print("📝 Description :", weather.get("description"))
print("🌡 Température :", main.get("temp"), "°C")
print("🤒 Ressentie :", main.get("feels_like"), "°C")
print("💧 Humidité :", main.get("humidity"), "%")
print("📈 Pression :", main.get("pressure"), "hPa")
print("🧭 Vent :", wind.get("speed"), "m/s")
print("==============================")
connect_wifi()
time.sleep(1)
while True:
try:
print("HTTPS GET:", URL)
r = urequests.get(URL)
# Si erreur HTTP, afficher le message de l'API
if r.status_code != 200:
print("Erreur HTTP", r.status_code)
print(r.text) # utile pour voir: clé invalide, quota, etc.
r.close()
else:
data = r.json()
r.close()
show_weather(data)
except Exception as e:
print(" Erreur:", e)
time.sleep(60)