From f7d468c4115d9f94739e9578b7ecee4502b0e186 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 18 Oct 2024 11:57:57 -0700 Subject: [PATCH 1/4] fix bmp and updates --- .../openmeteo/bmps/weather_bg.bmp | Bin 5250 -> 5266 bytes MagTag/MagTag_Weather/openmeteo/code.py | 44 +++++++----------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/MagTag/MagTag_Weather/openmeteo/bmps/weather_bg.bmp b/MagTag/MagTag_Weather/openmeteo/bmps/weather_bg.bmp index 1fa88ebe765aa948b7d93bf5e20bac26becb413e..d3f3e53fcbfa52767a7ebfb8888bce338b780188 100755 GIT binary patch delta 245 zcmZqDoTSO`Ok&)7y?T;xd#@xvj*ew$^6Xf zjE}+OPavtr^n(#XF#{<-IFA9Og6UuVWC3PX<{xHYn(2oCNR)9Vn6%%_&&$rVc^!um zGy6X_{{R1fo|&A_rO5t|g&oY62eE6Oqq2YSK-7T5fox>uMIQsvCz@Pfb|Ckgw*uk8C z_LJXnC~z=?IsfN_Il*8~gFKj14B>#4F$PbTXIA6<2T=`FfH0?lABpq-IhX^nt~niv z(+E*^A8hE)`9K*p#?PC5c{Q0f@8eKn=2Ch7|HG5#|9?KWpZtqU4$K7#fw)mXF3a=( zpB0|}|B(*mE(dZC|No&1Rm}eXvr6Otp9)Y}_x~RhX8!-7WItJpS(^7>`u`rF;U9q_ PK<%eEoHxtzma+o?ACsbZ diff --git a/MagTag/MagTag_Weather/openmeteo/code.py b/MagTag/MagTag_Weather/openmeteo/code.py index 6acb1213e..f24bbfcb9 100644 --- a/MagTag/MagTag_Weather/openmeteo/code.py +++ b/MagTag/MagTag_Weather/openmeteo/code.py @@ -9,16 +9,13 @@ import adafruit_imageload from adafruit_display_text import label from adafruit_magtag.magtag import MagTag -# needed for NTP -import wifi -import socketpool -import adafruit_ntp # --| USER CONFIG |-------------------------- LAT = 47.6 # latitude LON = -122.3 # longitude TMZ = "America/Los_Angeles" # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones METRIC = False # set to True for metric units +CITY = None # optional # ------------------------------------------- # ---------------------------- @@ -95,7 +92,7 @@ def get_forecast(): URL += "&timeformat=unixtime" URL += f"&timezone={TMZ}" resp = magtag.network.fetch(URL) - return resp.json() + return resp def make_banner(x=0, y=0): @@ -205,26 +202,13 @@ def update_future(data): banner[2].text = temperature_text(t) -def get_ntp_time(offset): - """Use NTP to get current local time.""" - pool = socketpool.SocketPool(wifi.radio) - ntp = adafruit_ntp.NTP(pool, tz_offset=offset) - if ntp: - return ntp.datetime - else: - return None - - -def go_to_sleep(current_time): +def go_to_sleep(current_time_secs): """Enter deep sleep for time needed.""" - # compute current time offset in seconds - hour = current_time.tm_hour - minutes = current_time.tm_min - seconds = current_time.tm_sec - seconds_since_midnight = 60 * (hour * 60 + minutes) + seconds + # work in units of seconds + seconds_in_a_day = 24 * 60 * 60 three_fifteen = (3 * 60 + 15) * 60 # wake up 15 minutes after 3am - seconds_to_sleep = (24 * 60 * 60 - seconds_since_midnight) + three_fifteen + seconds_to_sleep = (seconds_in_a_day - current_time_secs) + three_fifteen print( "Sleeping for {} hours, {} minutes".format( seconds_to_sleep // 3600, (seconds_to_sleep // 60) % 60 @@ -240,7 +224,12 @@ def go_to_sleep(current_time): today_date.anchor_point = (0, 0) today_date.anchored_position = (15, 14) -location_name = label.Label(terminalio.FONT, text=f"({LAT},{LON})", color=0x000000) +location_name = label.Label(terminalio.FONT, color=0x000000) +if CITY: + location_name.text = f"{CITY[:16]} ({LAT:.1f},{LON:.1f})" +else: + location_name.text = f"({LAT},{LON})" + location_name.anchor_point = (0, 0) location_name.anchored_position = (15, 25) @@ -301,7 +290,8 @@ def go_to_sleep(current_time): # M A I N # =========== print("Fetching forecast...") -forecast_data = get_forecast() +resp_data = get_forecast() +forecast_data = resp_data.json() print("Updating...") update_today(forecast_data) @@ -313,6 +303,8 @@ def go_to_sleep(current_time): time.sleep(magtag.display.time_to_refresh + 1) print("Sleeping...") -go_to_sleep(get_ntp_time(forecast_data["utc_offset_seconds"]/3600)) +h, m, s = (int(t) for t in resp_data.headers['date'].split(" ")[4].split(':')) +current_time_secs = (h * 3600) + (m * 60) + (s) + forecast_data['utc_offset_seconds'] +go_to_sleep(current_time_secs) # entire code will run again after deep sleep cycle -# similar to hitting the reset button +# similar to hitting the reset button \ No newline at end of file From f57cb124b932f2aa2012de01320ab890c1d98325 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 18 Oct 2024 12:14:01 -0700 Subject: [PATCH 2/4] lint --- MagTag/MagTag_Weather/openmeteo/code.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MagTag/MagTag_Weather/openmeteo/code.py b/MagTag/MagTag_Weather/openmeteo/code.py index f24bbfcb9..8051f53f2 100644 --- a/MagTag/MagTag_Weather/openmeteo/code.py +++ b/MagTag/MagTag_Weather/openmeteo/code.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2024 Carter Nelson for Adafruit Industries # # SPDX-License-Identifier: MIT -# pylint: disable=redefined-outer-name, eval-used, wrong-import-order +# pylint: disable=redefined-outer-name, eval-used, wrong-import-order, unsubscriptable-object import time import terminalio @@ -307,4 +307,5 @@ def go_to_sleep(current_time_secs): current_time_secs = (h * 3600) + (m * 60) + (s) + forecast_data['utc_offset_seconds'] go_to_sleep(current_time_secs) # entire code will run again after deep sleep cycle -# similar to hitting the reset button \ No newline at end of file +# similar to hitting the reset button + From f6f6d30f246edb1d8c80638d1223cd93603cca30 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 18 Oct 2024 12:40:57 -0700 Subject: [PATCH 3/4] whatever linter --- MagTag/MagTag_Weather/openmeteo/code.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MagTag/MagTag_Weather/openmeteo/code.py b/MagTag/MagTag_Weather/openmeteo/code.py index 8051f53f2..dfb5bc08c 100644 --- a/MagTag/MagTag_Weather/openmeteo/code.py +++ b/MagTag/MagTag_Weather/openmeteo/code.py @@ -307,5 +307,4 @@ def go_to_sleep(current_time_secs): current_time_secs = (h * 3600) + (m * 60) + (s) + forecast_data['utc_offset_seconds'] go_to_sleep(current_time_secs) # entire code will run again after deep sleep cycle -# similar to hitting the reset button - +# similar to hitting the reset button \ No newline at end of file From 810c12d8d19fcad1fadfa9e82d71b9971dc34637 Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 18 Oct 2024 12:52:59 -0700 Subject: [PATCH 4/4] i dunno just use black i guess --- MagTag/MagTag_Weather/openmeteo/code.py | 48 ++++++++++++------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/MagTag/MagTag_Weather/openmeteo/code.py b/MagTag/MagTag_Weather/openmeteo/code.py index dfb5bc08c..932f90124 100644 --- a/MagTag/MagTag_Weather/openmeteo/code.py +++ b/MagTag/MagTag_Weather/openmeteo/code.py @@ -11,11 +11,11 @@ from adafruit_magtag.magtag import MagTag # --| USER CONFIG |-------------------------- -LAT = 47.6 # latitude -LON = -122.3 # longitude -TMZ = "America/Los_Angeles" # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones -METRIC = False # set to True for metric units -CITY = None # optional +LAT = 47.6 # latitude +LON = -122.3 # longitude +TMZ = "America/Los_Angeles" # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones +METRIC = False # set to True for metric units +CITY = None # optional # ------------------------------------------- # ---------------------------- @@ -58,15 +58,15 @@ # Map the above WMO codes to index of icon in 3x3 spritesheet WMO_CODE_TO_ICON = ( - (0,), # 0 = sunny - (1,), # 1 = partly sunny/cloudy - (2,), # 2 = cloudy - (3,), # 3 = very cloudy - (61, 63, 65), # 4 = rain - (51, 53, 55, 80, 81, 82), # 5 = showers - (95, 96, 99), # 6 = storms - (56, 57, 66, 67, 71, 73, 75, 77, 85, 86), # 7 = snow - (45, 48), # 8 = fog and stuff + (0,), # 0 = sunny + (1,), # 1 = partly sunny/cloudy + (2,), # 2 = cloudy + (3,), # 3 = very cloudy + (61, 63, 65), # 4 = rain + (51, 53, 55, 80, 81, 82), # 5 = showers + (95, 96, 99), # 6 = storms + (56, 57, 66, 67, 71, 73, 75, 77, 85, 86), # 7 = snow + (45, 48), # 8 = fog and stuff ) magtag = MagTag() @@ -85,8 +85,9 @@ # ///////////////////////////////////////////////////////////////////////// # helper functions + def get_forecast(): - URL = f"https://api.open-meteo.com/v1/forecast?latitude={LAT}&longitude={LON}&" + URL = f"https://api.open-meteo.com/v1/forecast?latitude={LAT}&longitude={LON}&" URL += "daily=weather_code,temperature_2m_max,temperature_2m_min" URL += ",sunrise,sunset,wind_speed_10m_max,wind_direction_10m_dominant" URL += "&timeformat=unixtime" @@ -165,10 +166,7 @@ def update_today(data): s = data["daily"]["time"][0] + data["utc_offset_seconds"] t = time.localtime(s) today_date.text = "{} {} {}, {}".format( - DAYS[t.tm_wday].upper(), - MONTHS[t.tm_mon - 1].upper(), - t.tm_mday, - t.tm_year + DAYS[t.tm_wday].upper(), MONTHS[t.tm_mon - 1].upper(), t.tm_mday, t.tm_year ) # weather icon w = data["daily"]["weather_code"][0] @@ -191,14 +189,14 @@ def update_future(data): """Update the future forecast info.""" for i, banner in enumerate(future_banners): # day of week - s = data["daily"]["time"][i+1] + data["utc_offset_seconds"] + s = data["daily"]["time"][i + 1] + data["utc_offset_seconds"] t = time.localtime(s) banner[0].text = DAYS[t.tm_wday][:3].upper() # weather icon - w = data["daily"]["weather_code"][i+1] + w = data["daily"]["weather_code"][i + 1] banner[1][0] = next(x for x, t in enumerate(WMO_CODE_TO_ICON) if w in t) # temperature - t = data["daily"]["temperature_2m_max"][i+1] + t = data["daily"]["temperature_2m_max"][i + 1] banner[2].text = temperature_text(t) @@ -303,8 +301,8 @@ def go_to_sleep(current_time_secs): time.sleep(magtag.display.time_to_refresh + 1) print("Sleeping...") -h, m, s = (int(t) for t in resp_data.headers['date'].split(" ")[4].split(':')) -current_time_secs = (h * 3600) + (m * 60) + (s) + forecast_data['utc_offset_seconds'] +h, m, s = (int(t) for t in resp_data.headers["date"].split(" ")[4].split(":")) +current_time_secs = (h * 3600) + (m * 60) + (s) + forecast_data["utc_offset_seconds"] go_to_sleep(current_time_secs) # entire code will run again after deep sleep cycle -# similar to hitting the reset button \ No newline at end of file +# similar to hitting the reset button