Skip to content

Commit 368ab7d

Browse files
committed
adding additional error handling & time math
1 parent 0c4256a commit 368ab7d

File tree

1 file changed

+110
-94
lines changed

1 file changed

+110
-94
lines changed

Star_Fragment_Lamp/code.py

Lines changed: 110 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import ssl
66
import time
7+
import microcontroller
78
import board
89
import wifi
910
import socketpool
@@ -41,16 +42,14 @@
4142
io = IO_HTTP(aio_username, aio_key, requests)
4243

4344
# function for making http requests with try/except
44-
def get_request(num_tries, ping):
45-
tries = num_tries
45+
def get_request(tries, ping):
4646
for i in range(tries):
4747
try:
4848
n = ping
49-
# print(now)
5049
except RuntimeError as e:
5150
print(e)
52-
time.sleep(2)
53-
if i < tries - 1: # i is zero indexed
51+
time.sleep(10)
52+
if i < tries - 1:
5453
continue
5554
raise
5655
break
@@ -82,19 +81,33 @@ def divide_time(z):
8281
string_time = z.split("-")
8382
clock_time = string_time[2].split("T")
8483
int_time = clock_time[1].split(":")
85-
return int(int_time[0]), int(int_time[1])
84+
event_time = time.struct_time(
85+
(int(string_time[0]), int(string_time[1]), int(clock_time[0]), int(int_time[0]), int(int_time[1]), 0, -1, -1, False)
86+
)
87+
# print(event_time)
88+
return event_time
8689

87-
rise_hour, rise_minute= divide_time(sunrise)
88-
set_hour, set_minute = divide_time(sunset)
90+
rise_time = divide_time(sunrise)
91+
set_time = divide_time(sunset)
8992

9093
# function that tracks how many hours/minutes until sunrise or sunset
91-
def sun_countdown(sun_hour, sun_minute):
92-
hours_until = sun_hour - now.tm_hour
93-
minutes_until = sun_minute - now.tm_min
94-
return hours_until, minutes_until
95-
96-
hours_until_sunset, mins_until_sunset = sun_countdown(set_hour, set_minute)
97-
hours_until_sunrise, mins_until_sunrise = sun_countdown(rise_hour, set_minute)
94+
def sun_countdown(sun_event):
95+
n = get_request(5, io.receive_time())
96+
remaining = time.mktime(sun_event) - time.mktime(n)
97+
# print(remaining)
98+
# calculate the seconds remaining
99+
secs_remaining = remaining % 60
100+
remaining //= 60
101+
# calculate the minutes remaining
102+
minutes_until = remaining % 60
103+
remaining //= 60
104+
# calculate the hours remaining
105+
hours_until = remaining % 24
106+
remaining //= 24
107+
return remaining, hours_until, minutes_until, n
108+
109+
total_until_rise, hours_until_sunrise, mins_until_sunrise, now = sun_countdown(rise_time)
110+
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
98111

99112
# red and yellow color percentage for neopixels
100113
percent_red = 0
@@ -108,7 +121,7 @@ def sun_countdown(sun_hour, sun_minute):
108121
pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
109122

110123
# check to see if the star fragment should be lit up on start-up
111-
if hours_until_sunset < 0:
124+
if total_until_set < 0:
112125
star_glow = True
113126
else:
114127
star_glow = False
@@ -125,94 +138,97 @@ def sun_countdown(sun_hour, sun_minute):
125138
looking_for_sunrise = False
126139

127140
while True:
128-
# if it's daytime
129-
if not star_glow:
130-
# every 15 minutes...
131-
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
132-
first_run = False
133-
# get the time from IO
134-
now = get_request(5, io.receive_time())
135-
print(now)
136-
print("pinging Open-Meteo")
137-
sunrise, sunset = sun_clock()
138-
hours_until_sunset, mins_until_sunset = sun_countdown(set_hour, set_minute)
139-
print("%d hour(s) until sunset" % hours_until_sunset)
140-
print("%d minutes(s) until sunset" % mins_until_sunset)
141-
print(sunset)
142-
print()
143-
# less than an hour until sunset...
144-
if hours_until_sunset == 0:
145-
# check every minute
146-
time_check = 60000
147-
# map color to ramp up in brightness over the course of the final hour
148-
percent_red = simpleio.map_range(mins_until_sunset, 59, 0, 0, 255)
149-
percent_yellow = simpleio.map_range(mins_until_sunset, 59, 0, 0, 125)
150-
# if the sun has set..
151-
if mins_until_sunset < 1:
152-
percent_red = 255
153-
percent_yellow = 125
154-
time_check = 900000
155-
star_glow = True
156-
print("star is glowing")
157-
# otherwise just keep checking every 15 minutes
158-
else:
159-
time_check = 900000
160-
percent_red = 0
161-
percent_yellow = 0
162-
# reset clock
163-
clock = ticks_add(clock, time_check)
164-
# if it's nighttime...
165-
else:
166-
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
167-
now = get_request(5, io.receive_time())
168-
# check to see if it's past midnight by seeing if the date has changed
169-
# includes some logic if you are starting up the project in the very early morning hours
170-
if today != now.tm_mday or (first_run and now.tm_hour < rise_hour):
171-
today = now.tm_mday
172-
looking_for_sunrise = True
173-
# begin tracking the incoming sunrise
174-
if looking_for_sunrise:
141+
try:
142+
# if it's daytime
143+
if not star_glow:
144+
# every 15 minutes...
145+
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
146+
first_run = False
175147
print("pinging Open-Meteo")
176148
sunrise, sunset = sun_clock()
177-
hours_until_sunrise, mins_until_sunrise = sun_countdown(rise_hour, rise_minute)
178-
print("%d hour(s) until sunrise" % hours_until_sunrise)
179-
print("%d minutes(s) until sunrise" % mins_until_sunrise)
180-
print(sunrise)
181-
print(now)
149+
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
150+
print("%d hour(s) until sunset" % hours_until_sunset)
151+
print("%d minutes(s) until sunset" % mins_until_sunset)
152+
print(sunset)
182153
print()
183154
# less than an hour until sunset...
184-
if hours_until_sunrise == 0:
155+
if hours_until_sunset == 0:
185156
# check every minute
186157
time_check = 60000
187-
# map color to decrease brightness over the course of the final hour
188-
percent_red = simpleio.map_range(mins_until_sunrise, 59, 0, 255, 0)
189-
percent_yellow = simpleio.map_range(mins_until_sunrise, 59, 0, 125, 0)
190-
# if the sun has risen..
191-
if mins_until_sunrise < 1:
192-
percent_red = 0
193-
percent_yellow = 0
158+
# map color to ramp up in brightness over the course of the final hour
159+
percent_red = simpleio.map_range(mins_until_sunset, 59, 0, 0, 255)
160+
percent_yellow = simpleio.map_range(mins_until_sunset, 59, 0, 0, 125)
161+
# if the sun has set..
162+
if mins_until_sunset < 1:
163+
percent_red = 255
164+
percent_yellow = 125
194165
time_check = 900000
195-
star_glow = False
196-
looking_for_sunrise = False
197-
print("star is off")
166+
star_glow = True
167+
print("star is glowing")
168+
# otherwise just keep checking every 15 minutes
169+
else:
170+
time_check = 900000
171+
percent_red = 0
172+
percent_yellow = 0
173+
# reset clock
174+
clock = ticks_add(clock, time_check)
175+
# if it's nighttime...
176+
else:
177+
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
178+
first_run = False
179+
if today != now.tm_mday or (first_run and now.tm_hour < rise_time.tm_hour):
180+
today = now.tm_mday
181+
looking_for_sunrise = True
182+
# begin tracking the incoming sunrise
183+
if looking_for_sunrise:
184+
print("pinging Open-Meteo")
185+
sunrise, sunset = sun_clock()
186+
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(rise_time)
187+
print("%d hour(s) until sunrise" % hours_until_sunrise)
188+
print("%d minutes(s) until sunrise" % mins_until_sunrise)
189+
print(sunrise)
190+
print(now)
191+
print()
192+
# less than an hour until sunset...
193+
if hours_until_sunrise == 0:
194+
# check every minute
195+
time_check = 60000
196+
# map color to decrease brightness over the course of the final hour
197+
percent_red = simpleio.map_range(mins_until_sunrise, 59, 0, 255, 0)
198+
percent_yellow = simpleio.map_range(mins_until_sunrise, 59, 0, 125, 0)
199+
# if the sun has risen..
200+
if mins_until_sunrise < 1:
201+
percent_red = 0
202+
percent_yellow = 0
203+
time_check = 900000
204+
star_glow = False
205+
looking_for_sunrise = False
206+
print("star is off")
207+
# otherwise just keep checking every 15 minutes
208+
# and keep neopixels on
209+
else:
210+
time_check = 900000
211+
percent_red = 255
212+
percent_yellow = 125
198213
# otherwise just keep checking every 15 minutes
199214
# and keep neopixels on
200215
else:
216+
now = get_request(5, io.receive_time())
217+
print("not looking for sunrise")
218+
print(now)
219+
print()
201220
time_check = 900000
202221
percent_red = 255
203222
percent_yellow = 125
204-
# otherwise just keep checking every 15 minutes
205-
# and keep neopixels on
206-
else:
207-
print("not looking for sunrise")
208-
print(now)
209-
print()
210-
time_check = 900000
211-
percent_red = 255
212-
percent_yellow = 125
213-
first_run = False
214-
# reset clock
215-
clock = ticks_add(clock, time_check)
216-
# turn neopixels on using RGB values
217-
pixels.fill((percent_red, percent_yellow, 0))
218-
pixels.show()
223+
first_run = False
224+
# reset clock
225+
clock = ticks_add(clock, time_check)
226+
# turn neopixels on using RGB values
227+
pixels.fill((percent_red, percent_yellow, 0))
228+
pixels.show()
229+
# pylint: disable=broad-except
230+
except Exception as e:
231+
print("Error:\n", str(e))
232+
print("Resetting microcontroller in 10 seconds")
233+
time.sleep(10)
234+
microcontroller.reset()

0 commit comments

Comments
 (0)