# SPDX-License-Identifier: MIT # Adafruit CircuitPython Wi-Fi example. Requires adafruit_requests in /lib. # In settings.toml, set CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD. # This is a network example, not a weather sensor or an emergency alert system. import os import ssl import time import wifi import socketpool import adafruit_requests URL = "https://adafruit.ai/weather/current.json" POLL_SECONDS = 600 pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) retry_seconds = 60 while True: try: if not wifi.radio.connected: wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) with requests.get(URL, timeout=20) as response: if response.status_code != 200: raise RuntimeError("Weather HTTP {}".format(response.status_code)) data = response.json() if data.get("schemaVersion") != 1: raise RuntimeError("Unknown weather schema") print("Source:", data.get("stationName"), data.get("stationId")) print("Observed:", data.get("observedAt"), "Status:", data.get("status")) if data.get("temperatureF") is None: print("Temperature is unavailable; do not replace it with zero.") else: print("Temperature:", data["temperatureF"], "F") print("Alerts:", data.get("alertsStatus"), data.get("alertCount")) # Update your screen here. Keep the source timestamp and stale label visible. del data retry_seconds = 60 time.sleep(POLL_SECONDS) except (OSError, ValueError, RuntimeError) as error: print("Weather unavailable:", error) # Keep the last display only if it is visibly marked as old data. time.sleep(retry_seconds) retry_seconds = min(retry_seconds * 2, 3600)