Skip to content

Commit ffe505d

Browse files
author
brentru
committed
add hourly updating
1 parent b29a183 commit ffe505d

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

PyPortal_Quarantine_Clock/code.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@
1313
from adafruit_display_text import label
1414
import adafruit_touchscreen
1515

16-
1716
try:
1817
from secrets import secrets
1918
except ImportError:
2019
print("""WiFi settings are kept in secrets.py, please add them there!
2120
the secrets dictionary must contain 'ssid' and 'password' at a minimum""")
2221
raise
2322

23+
# Descriptions of each hour
24+
# https://github.com/mwfisher3/QuarantineClock/blob/master/today.html
25+
time_name = ["midnight-ish", "late night", "late", "super late",
26+
"super early","really early","dawn","morning",
27+
"morning","mid-morning","mid-morning","late morning",
28+
"noon-ish","afternoon","afternoon","mid-afternoon",
29+
"late afternoon","early evening","early evening","dusk-ish",
30+
"evening","evening","late evening","late evening"]
31+
32+
2433
esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
2534
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
2635
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)
@@ -89,26 +98,30 @@ def wday_to_weekday_name(tm_wday):
8998
# Show group splash
9099
board.DISPLAY.show(splash)
91100

92-
# Obtain local time from Time API
93-
pyportal.get_local_time(secrets['timezone'])
94-
the_time = time.localtime()
95-
96-
# Convert tm_wday to name of day
97-
weekday = wday_to_weekday_name(the_time.tm_wday)
98-
# Set label_day
99-
label_day.text = "{}".format(weekday)
100-
101-
102-
# Time descriptions
103-
time_name = ["midnight-ish", "late night", "late", "super late",
104-
"super early","really early","dawn","morning",
105-
"morning","mid-morning","mid-morning","late morning",
106-
"noon-ish","afternoon","afternoon","mid-afternoon",
107-
"late afternoon","early evening","early evening","dusk-ish",
108-
"evening","evening","late evening","late evening"]
109-
110-
label_time.text = "({})".format(time_name[the_time.tm_hour])
111-
112-
101+
refresh_time = None
113102
while True:
114-
pass
103+
# only query the network time every hour
104+
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
105+
try:
106+
print("Getting new time from internet...")
107+
pyportal.get_local_time(secrets['timezone'])
108+
refresh_time = time.monotonic()
109+
except (ValueError, RuntimeError) as error:
110+
print("Failed to get data, retrying\n", e)
111+
wifi.reset()
112+
continue
113+
114+
# set the_time
115+
the_time = time.localtime()
116+
117+
# Convert tm_wday to name of day
118+
weekday = wday_to_weekday_name(the_time.tm_wday)
119+
120+
# set the day label's text
121+
label_day.text = "{}".format(weekday)
122+
123+
# set the time label's text
124+
label_time.text = "({})".format(time_name[the_time.tm_hour])
125+
126+
# update every 30s.
127+
time.sleep(30)

0 commit comments

Comments
 (0)