9
9
import adafruit_imageload
10
10
from adafruit_display_text import label
11
11
from adafruit_magtag .magtag import MagTag
12
- # needed for NTP
13
- import wifi
14
- import socketpool
15
- import adafruit_ntp
16
12
17
13
# --| USER CONFIG |--------------------------
18
14
LAT = 47.6 # latitude
19
15
LON = - 122.3 # longitude
20
16
TMZ = "America/Los_Angeles" # https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
21
17
METRIC = False # set to True for metric units
18
+ CITY = None # optional
22
19
# -------------------------------------------
23
20
24
21
# ----------------------------
@@ -95,7 +92,7 @@ def get_forecast():
95
92
URL += "&timeformat=unixtime"
96
93
URL += f"&timezone={ TMZ } "
97
94
resp = magtag .network .fetch (URL )
98
- return resp . json ()
95
+ return resp
99
96
100
97
101
98
def make_banner (x = 0 , y = 0 ):
@@ -205,26 +202,13 @@ def update_future(data):
205
202
banner [2 ].text = temperature_text (t )
206
203
207
204
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 ):
219
206
"""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
225
209
three_fifteen = (3 * 60 + 15 ) * 60
226
210
# 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
228
212
print (
229
213
"Sleeping for {} hours, {} minutes" .format (
230
214
seconds_to_sleep // 3600 , (seconds_to_sleep // 60 ) % 60
@@ -240,7 +224,12 @@ def go_to_sleep(current_time):
240
224
today_date .anchor_point = (0 , 0 )
241
225
today_date .anchored_position = (15 , 14 )
242
226
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
+
244
233
location_name .anchor_point = (0 , 0 )
245
234
location_name .anchored_position = (15 , 25 )
246
235
@@ -301,7 +290,8 @@ def go_to_sleep(current_time):
301
290
# M A I N
302
291
# ===========
303
292
print ("Fetching forecast..." )
304
- forecast_data = get_forecast ()
293
+ resp_data = get_forecast ()
294
+ forecast_data = resp_data .json ()
305
295
306
296
print ("Updating..." )
307
297
update_today (forecast_data )
@@ -313,6 +303,8 @@ def go_to_sleep(current_time):
313
303
time .sleep (magtag .display .time_to_refresh + 1 )
314
304
315
305
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 )
317
309
# 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