import network import urequests import time # --- CONFIGURATION --- WIFI_SSID = "YOUR_WIFI_SSID" WIFI_PASS = "YOUR_WIFI_PASSWORD" # Coordinates (e.g., London: lat=51.5074, lon=-0.1278) LATITUDE = "51.5074" LONGITUDE = "-0.1278" # Open-Meteo API URL for temperature, humidity, and weather code URL = f"http://api.open-meteo.com/v1/forecast?latitude={LATITUDE}&longitude={LONGITUDE}¤t_weather=true" def connect_wifi(): wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print(f"Connecting to Wi-Fi network: {WIFI_SSID}...") wlan.connect(WIFI_SSID, WIFI_PASS) timeout = 10 while not wlan.isconnected() and timeout > 0: print("Waiting for connection...") time.sleep(1) timeout -= 1 if wlan.isconnected(): print("Connected successfully!") print("Network status/IP:", wlan.ifconfig()) return True else: print("Failed to connect to Wi-Fi.") return False def fetch_weather(): print("Fetching weather data...") try: response = urequests.get(URL) if response.status_code == 200: data = response.json() current = data.get("current_weather", {}) temp = current.get("temperature") windspeed = current.get("windspeed") weathercode = current.get("weathercode") print("\n--- WEATHER DATA RECEIVED ---") print(f"Temperature: {temp} °C") print(f"Wind Speed: {windspeed} km/h") print(f"Weather Code: {weathercode}") print("-----------------------------\n") else: print(f"Error: Server responded with status code {response.status_code}") response.close() except Exception as e: print("Error fetching data:", e) # --- RUN TEST --- if connect_wifi(): fetch_weather()