|
| 1 | +import time |
| 2 | +import terminalio |
| 3 | +import displayio |
| 4 | +import adafruit_imageload |
| 5 | +from adafruit_display_text import label |
| 6 | +from adafruit_magtag.magtag import MagTag |
| 7 | +from secrets import secrets |
| 8 | + |
| 9 | +# --| USER CONFIG |-------------------------- |
| 10 | +METRIC = False # set to True for metric units |
| 11 | +# ------------------------------------------- |
| 12 | + |
| 13 | +# ---------------------------- |
| 14 | +# Define various assets |
| 15 | +# ---------------------------- |
| 16 | +BACKGROUND_BMP = "/weather_bg.bmp" |
| 17 | +ICONS_LARGE_FILE = "/weather_icons_70px.bmp" |
| 18 | +ICONS_SMALL_FILE = "/weather_icons_20px.bmp" |
| 19 | +ICON_MAP = ("01", "02", "03", "04", "09", "10", "11", "13", "50") |
| 20 | +DAYS = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") |
| 21 | +MONTHS = ( |
| 22 | + "January", |
| 23 | + "February", |
| 24 | + "March", |
| 25 | + "April", |
| 26 | + "May", |
| 27 | + "June", |
| 28 | + "July", |
| 29 | + "Augsut", |
| 30 | + "September", |
| 31 | + "October", |
| 32 | + "November", |
| 33 | + "December", |
| 34 | +) |
| 35 | +magtag = MagTag() |
| 36 | + |
| 37 | +# ---------------------------- |
| 38 | +# Backgrounnd bitmap |
| 39 | +# ---------------------------- |
| 40 | +magtag.graphics.set_background(BACKGROUND_BMP) |
| 41 | + |
| 42 | +# ---------------------------- |
| 43 | +# Weather icons sprite sheet |
| 44 | +# ---------------------------- |
| 45 | +icons_large_bmp, icons_large_pal = adafruit_imageload.load(ICONS_LARGE_FILE) |
| 46 | +icons_small_bmp, icons_small_pal = adafruit_imageload.load(ICONS_SMALL_FILE) |
| 47 | + |
| 48 | +# ///////////////////////////////////////////////////////////////////////// |
| 49 | + |
| 50 | + |
| 51 | +def get_data_source_url(api="onecall", location=None): |
| 52 | + """Build and return the URL for the OpenWeather API.""" |
| 53 | + if api.upper() == "FORECAST5": |
| 54 | + URL = "https://api.openweathermap.org/data/2.5/forecast?" |
| 55 | + URL += "q=" + location |
| 56 | + elif api.upper() == "ONECALL": |
| 57 | + URL = "https://api.openweathermap.org/data/2.5/onecall?exclude=minutely,hourly,alerts" |
| 58 | + URL += "&lat={}".format(location[0]) |
| 59 | + URL += "&lon={}".format(location[1]) |
| 60 | + else: |
| 61 | + raise ValueError("Unknown API type: " + api) |
| 62 | + |
| 63 | + return URL + "&appid=" + secrets["openweather_token"] |
| 64 | + |
| 65 | + |
| 66 | +def get_latlon(): |
| 67 | + """Use the Forecast5 API to determine lat/lon for given city.""" |
| 68 | + magtag.url = get_data_source_url(api="forecast5", location=secrets["openweather_location"]) |
| 69 | + magtag.json_path = ["city"] |
| 70 | + raw_data = magtag.fetch() |
| 71 | + return raw_data["coord"]["lat"], raw_data["coord"]["lon"] |
| 72 | + |
| 73 | + |
| 74 | +def get_forecast(location): |
| 75 | + """Use OneCall API to fetch forecast and timezone data.""" |
| 76 | + resp = magtag.network.fetch(get_data_source_url(api="onecall", location=location)) |
| 77 | + json_data = resp.json() |
| 78 | + return json_data["daily"], json_data["timezone_offset"] |
| 79 | + |
| 80 | + |
| 81 | +def make_banner(x=0, y=0): |
| 82 | + """Make a single future forecast info banner group.""" |
| 83 | + day_of_week = label.Label(terminalio.FONT, text="DAY", color=0x000000) |
| 84 | + day_of_week.anchor_point = (0, 0.5) |
| 85 | + day_of_week.anchored_position = (0, 10) |
| 86 | + |
| 87 | + icon = displayio.TileGrid( |
| 88 | + icons_small_bmp, |
| 89 | + pixel_shader=icons_small_pal, |
| 90 | + x=25, |
| 91 | + y=0, |
| 92 | + width=1, |
| 93 | + height=1, |
| 94 | + tile_width=20, |
| 95 | + tile_height=20, |
| 96 | + ) |
| 97 | + |
| 98 | + day_temp = label.Label(terminalio.FONT, text="+100F", color=0x000000) |
| 99 | + day_temp.anchor_point = (0, 0.5) |
| 100 | + day_temp.anchored_position = (50, 10) |
| 101 | + |
| 102 | + group = displayio.Group(max_size=3, x=x, y=y) |
| 103 | + group.append(day_of_week) |
| 104 | + group.append(icon) |
| 105 | + group.append(day_temp) |
| 106 | + |
| 107 | + return group |
| 108 | + |
| 109 | + |
| 110 | +def temperature_text(tempK): |
| 111 | + if METRIC: |
| 112 | + return "{:3.0f}C".format(tempK - 273.15) |
| 113 | + else: |
| 114 | + return "{:3.0f}F".format(32.0 + 1.8 * (tempK - 273.15)) |
| 115 | + |
| 116 | + |
| 117 | +def wind_text(speedms): |
| 118 | + if METRIC: |
| 119 | + return "{:3.0f}m/s".format(speedms) |
| 120 | + else: |
| 121 | + return "{:3.0f}mph".format(2.23694 * speedms) |
| 122 | + |
| 123 | + |
| 124 | +def update_banner(banner, data): |
| 125 | + """Update supplied forecast banner with supplied data.""" |
| 126 | + banner[0].text = DAYS[time.localtime(data["dt"]).tm_wday][:3].upper() |
| 127 | + banner[1][0] = ICON_MAP.index(data["weather"][0]["icon"][:2]) |
| 128 | + banner[2].text = temperature_text(data["temp"]["day"]) |
| 129 | + |
| 130 | + |
| 131 | +def update_today(data, tz_offset=0): |
| 132 | + """Update today info banner.""" |
| 133 | + date = time.localtime(data["dt"]) |
| 134 | + sunrise = time.localtime(data["sunrise"] + tz_offset) |
| 135 | + sunset = time.localtime(data["sunset"] + tz_offset) |
| 136 | + |
| 137 | + today_date.text = "{} {} {}, {}".format( |
| 138 | + DAYS[date.tm_wday].upper(), |
| 139 | + MONTHS[date.tm_mon - 1].upper(), |
| 140 | + date.tm_mday, |
| 141 | + date.tm_year, |
| 142 | + ) |
| 143 | + today_icon[0] = ICON_MAP.index(data["weather"][0]["icon"][:2]) |
| 144 | + today_morn_temp.text = temperature_text(data["temp"]["morn"]) |
| 145 | + today_day_temp.text = temperature_text(data["temp"]["day"]) |
| 146 | + today_night_temp.text = temperature_text(data["temp"]["night"]) |
| 147 | + today_humidity.text = "{:3d}%".format(data["humidity"]) |
| 148 | + today_wind.text = wind_text(data["wind_speed"]) |
| 149 | + today_sunrise.text = "{:2d}:{:02d} AM".format(sunrise.tm_hour, sunrise.tm_min) |
| 150 | + today_sunset.text = "{:2d}:{:02d} PM".format(sunset.tm_hour - 12, sunset.tm_min) |
| 151 | + |
| 152 | + |
| 153 | +def go_to_sleep(): |
| 154 | + """Enter deep sleep for time needed.""" |
| 155 | + # compute current time offset in seconds |
| 156 | + hour, minutes, seconds = time.localtime()[3:6] |
| 157 | + seconds_since_midnight = 60 * (hour * 60 + minutes) + seconds |
| 158 | + # wake up 15 minutes after midnite |
| 159 | + seconds_to_sleep = (24 * 60 * 60 - seconds_since_midnight) + 15 * 60 |
| 160 | + print( |
| 161 | + "Sleeping for {} hours, {} minutes".format( |
| 162 | + seconds_to_sleep // 3600, (seconds_to_sleep // 60) % 60 |
| 163 | + ) |
| 164 | + ) |
| 165 | + magtag.exit_and_deep_sleep(seconds_to_sleep) |
| 166 | + |
| 167 | + |
| 168 | +# =========== |
| 169 | +# U I |
| 170 | +# =========== |
| 171 | +today_date = label.Label(terminalio.FONT, text="?" * 30, color=0x000000) |
| 172 | +today_date.anchor_point = (0, 0) |
| 173 | +today_date.anchored_position = (15, 13) |
| 174 | + |
| 175 | +today_icon = displayio.TileGrid( |
| 176 | + icons_large_bmp, |
| 177 | + pixel_shader=icons_small_pal, |
| 178 | + x=10, |
| 179 | + y=40, |
| 180 | + width=1, |
| 181 | + height=1, |
| 182 | + tile_width=70, |
| 183 | + tile_height=70, |
| 184 | +) |
| 185 | + |
| 186 | +today_morn_temp = label.Label(terminalio.FONT, text="+100F", color=0x000000) |
| 187 | +today_morn_temp.anchor_point = (0.5, 0) |
| 188 | +today_morn_temp.anchored_position = (118, 59) |
| 189 | + |
| 190 | +today_day_temp = label.Label(terminalio.FONT, text="+100F", color=0x000000) |
| 191 | +today_day_temp.anchor_point = (0.5, 0) |
| 192 | +today_day_temp.anchored_position = (149, 59) |
| 193 | + |
| 194 | +today_night_temp = label.Label(terminalio.FONT, text="+100F", color=0x000000) |
| 195 | +today_night_temp.anchor_point = (0.5, 0) |
| 196 | +today_night_temp.anchored_position = (180, 59) |
| 197 | + |
| 198 | +today_humidity = label.Label(terminalio.FONT, text="100%", color=0x000000) |
| 199 | +today_humidity.anchor_point = (0, 0.5) |
| 200 | +today_humidity.anchored_position = (105, 95) |
| 201 | + |
| 202 | +today_wind = label.Label(terminalio.FONT, text="99m/s", color=0x000000) |
| 203 | +today_wind.anchor_point = (0, 0.5) |
| 204 | +today_wind.anchored_position = (155, 95) |
| 205 | + |
| 206 | +today_sunrise = label.Label(terminalio.FONT, text="12:12 PM", color=0x000000) |
| 207 | +today_sunrise.anchor_point = (0, 0.5) |
| 208 | +today_sunrise.anchored_position = (45, 117) |
| 209 | + |
| 210 | +today_sunset = label.Label(terminalio.FONT, text="12:12 PM", color=0x000000) |
| 211 | +today_sunset.anchor_point = (0, 0.5) |
| 212 | +today_sunset.anchored_position = (130, 117) |
| 213 | + |
| 214 | +today_banner = displayio.Group(max_size=10) |
| 215 | +today_banner.append(today_date) |
| 216 | +today_banner.append(today_icon) |
| 217 | +today_banner.append(today_morn_temp) |
| 218 | +today_banner.append(today_day_temp) |
| 219 | +today_banner.append(today_night_temp) |
| 220 | +today_banner.append(today_humidity) |
| 221 | +today_banner.append(today_wind) |
| 222 | +today_banner.append(today_sunrise) |
| 223 | +today_banner.append(today_sunset) |
| 224 | + |
| 225 | +future_banners = [ |
| 226 | + make_banner(x=210, y=18), |
| 227 | + make_banner(x=210, y=39), |
| 228 | + make_banner(x=210, y=60), |
| 229 | + make_banner(x=210, y=81), |
| 230 | + make_banner(x=210, y=102), |
| 231 | +] |
| 232 | + |
| 233 | +city_name = label.Label( |
| 234 | + terminalio.FONT, text=secrets["openweather_location"], color=0x000000 |
| 235 | +) |
| 236 | +city_name.anchor_point = (0, 0) |
| 237 | +city_name.anchored_position = (15, 24) |
| 238 | + |
| 239 | +magtag.splash.append(today_banner) |
| 240 | +for future_banner in future_banners: |
| 241 | + magtag.splash.append(future_banner) |
| 242 | +magtag.splash.append(city_name) |
| 243 | + |
| 244 | +# =========== |
| 245 | +# M A I N |
| 246 | +# =========== |
| 247 | +magtag.get_local_time() |
| 248 | + |
| 249 | +print("Getting Lat/Lon...") |
| 250 | +latlon = get_latlon() |
| 251 | +print(secrets["openweather_location"]) |
| 252 | +print(latlon) |
| 253 | + |
| 254 | +print("Fetching forecast...") |
| 255 | +forecast_data, local_tz_offset = get_forecast(latlon) |
| 256 | + |
| 257 | +print("Updating...") |
| 258 | +update_today(forecast_data[0], local_tz_offset) |
| 259 | +for day, forecast in enumerate(forecast_data[1:6]): |
| 260 | + update_banner(future_banners[day], forecast) |
| 261 | + |
| 262 | +print("Refreshing...") |
| 263 | +time.sleep(magtag.display.time_to_refresh + 1) |
| 264 | +magtag.display.refresh() |
| 265 | +time.sleep(magtag.display.time_to_refresh + 1) |
| 266 | + |
| 267 | +print("Sleeping...") |
| 268 | +go_to_sleep() |
| 269 | +# entire code will run again after deep sleep cycle |
| 270 | +# similar to hitting the reset button |
0 commit comments