Skip to content

Commit f7d468c

Browse files
committed
fix bmp and updates
1 parent 045afc1 commit f7d468c

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed
Binary file not shown.

MagTag/MagTag_Weather/openmeteo/code.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
import adafruit_imageload
1010
from adafruit_display_text import label
1111
from adafruit_magtag.magtag import MagTag
12-
# needed for NTP
13-
import wifi
14-
import socketpool
15-
import adafruit_ntp
1612

1713
# --| USER CONFIG |--------------------------
1814
LAT = 47.6 # latitude
1915
LON = -122.3 # longitude
2016
TMZ = "America/Los_Angeles" # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
2117
METRIC = False # set to True for metric units
18+
CITY = None # optional
2219
# -------------------------------------------
2320

2421
# ----------------------------
@@ -95,7 +92,7 @@ def get_forecast():
9592
URL += "&timeformat=unixtime"
9693
URL += f"&timezone={TMZ}"
9794
resp = magtag.network.fetch(URL)
98-
return resp.json()
95+
return resp
9996

10097

10198
def make_banner(x=0, y=0):
@@ -205,26 +202,13 @@ def update_future(data):
205202
banner[2].text = temperature_text(t)
206203

207204

208-
def get_ntp_time(offset):
209-
"""Use NTP to get current local time."""
210-
pool = socketpool.SocketPool(wifi.radio)
211-
ntp = adafruit_ntp.NTP(pool, tz_offset=offset)
212-
if ntp:
213-
return ntp.datetime
214-
else:
215-
return None
216-
217-
218-
def go_to_sleep(current_time):
205+
def go_to_sleep(current_time_secs):
219206
"""Enter deep sleep for time needed."""
220-
# compute current time offset in seconds
221-
hour = current_time.tm_hour
222-
minutes = current_time.tm_min
223-
seconds = current_time.tm_sec
224-
seconds_since_midnight = 60 * (hour * 60 + minutes) + seconds
207+
# work in units of seconds
208+
seconds_in_a_day = 24 * 60 * 60
225209
three_fifteen = (3 * 60 + 15) * 60
226210
# wake up 15 minutes after 3am
227-
seconds_to_sleep = (24 * 60 * 60 - seconds_since_midnight) + three_fifteen
211+
seconds_to_sleep = (seconds_in_a_day - current_time_secs) + three_fifteen
228212
print(
229213
"Sleeping for {} hours, {} minutes".format(
230214
seconds_to_sleep // 3600, (seconds_to_sleep // 60) % 60
@@ -240,7 +224,12 @@ def go_to_sleep(current_time):
240224
today_date.anchor_point = (0, 0)
241225
today_date.anchored_position = (15, 14)
242226

243-
location_name = label.Label(terminalio.FONT, text=f"({LAT},{LON})", color=0x000000)
227+
location_name = label.Label(terminalio.FONT, color=0x000000)
228+
if CITY:
229+
location_name.text = f"{CITY[:16]} ({LAT:.1f},{LON:.1f})"
230+
else:
231+
location_name.text = f"({LAT},{LON})"
232+
244233
location_name.anchor_point = (0, 0)
245234
location_name.anchored_position = (15, 25)
246235

@@ -301,7 +290,8 @@ def go_to_sleep(current_time):
301290
# M A I N
302291
# ===========
303292
print("Fetching forecast...")
304-
forecast_data = get_forecast()
293+
resp_data = get_forecast()
294+
forecast_data = resp_data.json()
305295

306296
print("Updating...")
307297
update_today(forecast_data)
@@ -313,6 +303,8 @@ def go_to_sleep(current_time):
313303
time.sleep(magtag.display.time_to_refresh + 1)
314304

315305
print("Sleeping...")
316-
go_to_sleep(get_ntp_time(forecast_data["utc_offset_seconds"]/3600))
306+
h, m, s = (int(t) for t in resp_data.headers['date'].split(" ")[4].split(':'))
307+
current_time_secs = (h * 3600) + (m * 60) + (s) + forecast_data['utc_offset_seconds']
308+
go_to_sleep(current_time_secs)
317309
# entire code will run again after deep sleep cycle
318-
# similar to hitting the reset button
310+
# similar to hitting the reset button

0 commit comments

Comments
 (0)