99import adafruit_imageload
1010from adafruit_display_text import label
1111from adafruit_magtag .magtag import MagTag
12- # needed for NTP
13- import wifi
14- import socketpool
15- import adafruit_ntp
1612
1713# --| USER CONFIG |--------------------------
1814LAT = 47.6 # latitude
1915LON = - 122.3 # longitude
2016TMZ = "America/Los_Angeles" # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
2117METRIC = 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
10198def 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):
240224today_date .anchor_point = (0 , 0 )
241225today_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+
244233location_name .anchor_point = (0 , 0 )
245234location_name .anchored_position = (15 , 25 )
246235
@@ -301,7 +290,8 @@ def go_to_sleep(current_time):
301290# M A I N
302291# ===========
303292print ("Fetching forecast..." )
304- forecast_data = get_forecast ()
293+ resp_data = get_forecast ()
294+ forecast_data = resp_data .json ()
305295
306296print ("Updating..." )
307297update_today (forecast_data )
@@ -313,6 +303,8 @@ def go_to_sleep(current_time):
313303time .sleep (magtag .display .time_to_refresh + 1 )
314304
315305print ("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