Skip to content

Commit 0ba59ff

Browse files
authored
Merge pull request #2433 from adafruit/star_fragment_update
star fragment update
2 parents db9408c + 4692dca commit 0ba59ff

File tree

1 file changed

+154
-98
lines changed

1 file changed

+154
-98
lines changed

Star_Fragment_Lamp/code.py

Lines changed: 154 additions & 98 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
@@ -40,24 +41,32 @@
4041
# io HTTP for getting the time from the internet
4142
io = IO_HTTP(aio_username, aio_key, requests)
4243

44+
def reset_on_error(delay, error):
45+
print("Error:\n", str(error))
46+
print("Resetting microcontroller in %d seconds" % delay)
47+
time.sleep(delay)
48+
microcontroller.reset()
49+
4350
# function for making http requests with try/except
44-
def get_request(num_tries, ping):
45-
tries = num_tries
51+
def get_request(tries, ping):
4652
for i in range(tries):
4753
try:
4854
n = ping
49-
# print(now)
50-
except RuntimeError as e:
51-
print(e)
52-
time.sleep(2)
53-
if i < tries - 1: # i is zero indexed
55+
except Exception as error:
56+
print(error)
57+
time.sleep(10)
58+
if i < tries - 1:
5459
continue
5560
raise
5661
break
5762
return n
5863

5964
# get the time on start-up
60-
now = get_request(5, io.receive_time())
65+
# pylint: disable=broad-except
66+
try:
67+
now = get_request(5, io.receive_time())
68+
except Exception as e:
69+
reset_on_error(10, e)
6170
print(now)
6271
today = now.tm_mday
6372

@@ -74,27 +83,54 @@ def sun_clock():
7483
return _rise, _set
7584

7685
# initial API call
77-
sunrise, sunset = sun_clock()
86+
try:
87+
sunrise, sunset = sun_clock()
88+
except Exception as e:
89+
reset_on_error(10, e)
90+
91+
print(sunrise)
92+
print(sunset)
7893

7994
# the sunrise/sunset time is returned as a JSON aka a string
8095
# this function chops up the string to get the hours and minutes as integers
8196
def divide_time(z):
8297
string_time = z.split("-")
8398
clock_time = string_time[2].split("T")
8499
int_time = clock_time[1].split(":")
85-
return int(int_time[0]), int(int_time[1])
100+
event_time = time.struct_time(
101+
(int(string_time[0]), int(string_time[1]), int(clock_time[0]), int(int_time[0]),
102+
int(int_time[1]), 0, -1, -1, False)
103+
)
104+
# print(event_time)
105+
return event_time
86106

87-
rise_hour, rise_minute= divide_time(sunrise)
88-
set_hour, set_minute = divide_time(sunset)
107+
rise_time = divide_time(sunrise)
108+
set_time = divide_time(sunset)
89109

90110
# 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)
111+
def sun_countdown(sun_event):
112+
n = get_request(5, io.receive_time())
113+
remaining = time.mktime(sun_event) - time.mktime(n)
114+
r = remaining
115+
# print(remaining)
116+
# calculate the seconds remaining
117+
secs_remaining = remaining % 60 # pylint: disable=unused-variable
118+
remaining //= 60
119+
# calculate the minutes remaining
120+
minutes_until = remaining % 60
121+
remaining //= 60
122+
# calculate the hours remaining
123+
hours_until = remaining % 24
124+
remaining //= 24
125+
return r, hours_until, minutes_until, n
126+
try:
127+
total_until_rise, hours_until_sunrise, mins_until_sunrise, now = sun_countdown(rise_time)
128+
except Exception as e:
129+
reset_on_error(10, e)
130+
try:
131+
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
132+
except Exception as e:
133+
reset_on_error(10, e)
98134

99135
# red and yellow color percentage for neopixels
100136
percent_red = 0
@@ -107,11 +143,24 @@ def sun_countdown(sun_hour, sun_minute):
107143

108144
pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
109145

146+
print(total_until_set)
110147
# check to see if the star fragment should be lit up on start-up
111-
if hours_until_sunset < 0:
148+
if total_until_set < 0:
149+
print("star glow true")
112150
star_glow = True
151+
percent_red = 255
152+
percent_yellow = 125
153+
# turn neopixels on using RGB values
154+
pixels.fill((percent_red, percent_yellow, 0))
155+
pixels.show()
113156
else:
157+
print("star glow false")
114158
star_glow = False
159+
percent_red = 0
160+
percent_yellow = 0
161+
# turn neopixels on using RGB values
162+
pixels.fill((percent_red, percent_yellow, 0))
163+
pixels.show()
115164

116165
# ticks time tracker
117166
clock = ticks_ms()
@@ -125,94 +174,101 @@ def sun_countdown(sun_hour, sun_minute):
125174
looking_for_sunrise = False
126175

127176
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:
177+
try:
178+
# if it's daytime
179+
if not star_glow:
180+
# every 15 minutes...
181+
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
175182
print("pinging Open-Meteo")
176183
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)
184+
(total_until_set, hours_until_sunset,
185+
mins_until_sunset, now) = sun_countdown(set_time)
181186
print(now)
187+
print("%d hour(s) until sunset" % hours_until_sunset)
188+
print("%d minutes(s) until sunset" % mins_until_sunset)
189+
print(sunset)
190+
print(percent_red)
182191
print()
183192
# less than an hour until sunset...
184-
if hours_until_sunrise == 0:
193+
if hours_until_sunset in (0, 23):
185194
# check every minute
186-
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
195+
time_check = 300000
196+
# map color to ramp up in brightness over the course of the final hour
197+
percent_red = simpleio.map_range(mins_until_sunset, 59, 0, 0, 255)
198+
percent_yellow = simpleio.map_range(mins_until_sunset, 59, 0, 0, 125)
199+
# if the sun has set..
200+
if total_until_set < 0:
201+
percent_red = 255
202+
percent_yellow = 125
194203
time_check = 900000
195-
star_glow = False
196-
looking_for_sunrise = False
197-
print("star is off")
204+
star_glow = True
205+
print("star is glowing")
206+
# otherwise just keep checking every 15 minutes
207+
else:
208+
time_check = 900000
209+
percent_red = 0
210+
percent_yellow = 0
211+
if first_run:
212+
first_run = False
213+
else:
214+
# reset clock
215+
clock = ticks_add(clock, time_check)
216+
# if it's nighttime...
217+
else:
218+
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
219+
if today != now.tm_mday or (first_run and now.tm_hour < rise_time.tm_hour):
220+
today = now.tm_mday
221+
looking_for_sunrise = True
222+
# begin tracking the incoming sunrise
223+
if looking_for_sunrise:
224+
print("pinging Open-Meteo")
225+
sunrise, sunset = sun_clock()
226+
(total_until_rise, hours_until_sunrise,
227+
mins_until_sunrise, now) = sun_countdown(rise_time)
228+
print(now)
229+
print("%d hour(s) until sunrise" % hours_until_sunrise)
230+
print("%d minutes(s) until sunrise" % mins_until_sunrise)
231+
print(sunrise)
232+
print(now)
233+
print()
234+
# less than an hour until sunset...
235+
if hours_until_sunrise in (0, 23):
236+
# check every minute
237+
time_check = 300000
238+
# map color to decrease brightness over the course of the final hour
239+
percent_red = simpleio.map_range(mins_until_sunrise, 59, 0, 255, 0)
240+
percent_yellow = simpleio.map_range(mins_until_sunrise, 59, 0, 125, 0)
241+
# if the sun has risen..
242+
if total_until_rise < 0:
243+
percent_red = 0
244+
percent_yellow = 0
245+
time_check = 900000
246+
star_glow = False
247+
looking_for_sunrise = False
248+
print("star is off")
249+
# otherwise just keep checking every 15 minutes
250+
# and keep neopixels on
251+
else:
252+
time_check = 900000
253+
percent_red = 255
254+
percent_yellow = 125
198255
# otherwise just keep checking every 15 minutes
199256
# and keep neopixels on
200257
else:
258+
now = get_request(5, io.receive_time())
259+
print("not looking for sunrise")
260+
print(now)
261+
print()
201262
time_check = 900000
202263
percent_red = 255
203264
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()
265+
if first_run:
266+
first_run = False
267+
else:
268+
# reset clock
269+
clock = ticks_add(clock, time_check)
270+
# turn neopixels on using RGB values
271+
pixels.fill((percent_red, percent_yellow, 0))
272+
pixels.show()
273+
except Exception as e:
274+
reset_on_error(10, e)

0 commit comments

Comments
 (0)