Created
January 3, 2026 16:51
-
-
Save adeekshith/17550f3661b6df726e0108d6a0e1521a to your computer and use it in GitHub Desktop.
Weather dashboard for M5Stack Core basic 2.7 that loads from weather.org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from m5stack import * | |
| from m5ui import * | |
| from uiflow import * | |
| import urequests | |
| import wifiCfg | |
| import ujson | |
| import gc | |
| import time | |
| # ================== CONFIG ================== | |
| WARMUP_URL = "https://api.weather.gov/gridpoints/ILX/95,71/forecast" | |
| WEATHER_URL = "https://api.weather.gov/gridpoints/FWD/89,110/forecast" | |
| REFRESH_INTERVAL = 900 | |
| # ============================================ | |
| SCREEN_W = 320 | |
| # ---------- COLORS ---------- | |
| TEXT_COLOR = 0xFFFFFF | |
| STATUS_COLOR = 0xAAAAAA | |
| NORMAL_DK = 0x222222 | |
| def bg_for_temp(temp_f): | |
| if temp_f <= 32: | |
| return 0x001133 | |
| elif temp_f <= 42: | |
| return 0x002255 | |
| elif temp_f <= 52: | |
| return 0x003377 | |
| elif temp_f <= 62: | |
| return 0x004455 | |
| elif temp_f <= 72: | |
| return 0x003300 | |
| elif temp_f <= 82: | |
| return 0x552200 | |
| else: | |
| return 0x660000 | |
| # ---------- DRAW HELPERS ---------- | |
| def set_bg(color): | |
| setScreenColor(color) | |
| def clear_rect(x, y, w, h, color): | |
| lcd.fillRect(x, y, w, h, color) | |
| def print_centered(text, y, font, color, bg): | |
| lcd.font(font) | |
| w = lcd.textWidth(text) | |
| x = (SCREEN_W - w) // 2 | |
| if x < 0: | |
| x = 0 | |
| lcd.setTextColor(color, bg) | |
| lcd.print(text, x, y) | |
| def clear_main(bg): | |
| clear_rect(0, 0, SCREEN_W, 200, bg) | |
| def clear_status(bg): | |
| clear_rect(0, 200, SCREEN_W, 40, bg) | |
| # ---------- WIFI ---------- | |
| set_bg(NORMAL_DK) | |
| clear_status(NORMAL_DK) | |
| print_centered("Connecting WiFi...", 210, lcd.FONT_Default, STATUS_COLOR, NORMAL_DK) | |
| wifiCfg.autoConnect(lcdShow=True) | |
| clear_status(NORMAL_DK) | |
| print_centered("WiFi connected", 210, lcd.FONT_Default, STATUS_COLOR, NORMAL_DK) | |
| # ---------- NETWORK WARMUP ---------- | |
| def warmup_request(): | |
| try: | |
| headers = { | |
| "User-Agent": "M5Stack-Core2 Weather Dashboard", | |
| "Accept": "application/geo+json", | |
| "Connection": "close" | |
| } | |
| r = urequests.get(WARMUP_URL, headers=headers) | |
| r.close() | |
| del r | |
| gc.collect() | |
| wait_ms(200) | |
| except Exception as e: | |
| print("Warmup ignored:", e) | |
| # ---------- WEATHER ---------- | |
| def update_weather(): | |
| clear_status(NORMAL_DK) | |
| print_centered("Updating...", 210, lcd.FONT_Default, STATUS_COLOR, NORMAL_DK) | |
| try: | |
| headers = { | |
| "User-Agent": "M5Stack-Core2 Weather Dashboard", | |
| "Accept": "application/geo+json", | |
| "Connection": "close" | |
| } | |
| warmup_request() | |
| r = urequests.get(WEATHER_URL, headers=headers) | |
| data = ujson.loads(r.text) | |
| r.close() | |
| del r | |
| period = data["properties"]["periods"][0] | |
| temp = period["temperature"] | |
| unit = period["temperatureUnit"] | |
| short_fc = period["shortForecast"] | |
| name = period["name"] | |
| print("Forecast:", name, period["startTime"]) | |
| bg = bg_for_temp(temp) | |
| set_bg(bg) | |
| clear_main(bg) | |
| clear_status(bg) | |
| print_centered(str(temp) + "°" + unit, 45, | |
| lcd.FONT_DejaVu56, TEXT_COLOR, bg) | |
| print_centered(short_fc, 125, | |
| lcd.FONT_DejaVu24, TEXT_COLOR, bg) | |
| print_centered(name, 210, | |
| lcd.FONT_Default, STATUS_COLOR, bg) | |
| gc.collect() | |
| except Exception as e: | |
| clear_status(NORMAL_DK) | |
| print_centered("Update failed", 210, | |
| lcd.FONT_Default, STATUS_COLOR, NORMAL_DK) | |
| print("ERROR:", e) | |
| gc.collect() | |
| # ---------- MAIN LOOP ---------- | |
| update_weather() | |
| last_update = time.time() | |
| while True: | |
| if time.time() - last_update > REFRESH_INTERVAL: | |
| update_weather() | |
| last_update = time.time() | |
| wait_ms(500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment