Merge pull request #2433 from adafruit/star_fragment_update
star fragment update
This commit is contained in:
commit
0ba59ff228
1 changed files with 154 additions and 98 deletions
|
|
@ -4,6 +4,7 @@
|
|||
import os
|
||||
import ssl
|
||||
import time
|
||||
import microcontroller
|
||||
import board
|
||||
import wifi
|
||||
import socketpool
|
||||
|
|
@ -40,24 +41,32 @@ location = "America/New York"
|
|||
# io HTTP for getting the time from the internet
|
||||
io = IO_HTTP(aio_username, aio_key, requests)
|
||||
|
||||
def reset_on_error(delay, error):
|
||||
print("Error:\n", str(error))
|
||||
print("Resetting microcontroller in %d seconds" % delay)
|
||||
time.sleep(delay)
|
||||
microcontroller.reset()
|
||||
|
||||
# function for making http requests with try/except
|
||||
def get_request(num_tries, ping):
|
||||
tries = num_tries
|
||||
def get_request(tries, ping):
|
||||
for i in range(tries):
|
||||
try:
|
||||
n = ping
|
||||
# print(now)
|
||||
except RuntimeError as e:
|
||||
print(e)
|
||||
time.sleep(2)
|
||||
if i < tries - 1: # i is zero indexed
|
||||
except Exception as error:
|
||||
print(error)
|
||||
time.sleep(10)
|
||||
if i < tries - 1:
|
||||
continue
|
||||
raise
|
||||
break
|
||||
return n
|
||||
|
||||
# get the time on start-up
|
||||
now = get_request(5, io.receive_time())
|
||||
# pylint: disable=broad-except
|
||||
try:
|
||||
now = get_request(5, io.receive_time())
|
||||
except Exception as e:
|
||||
reset_on_error(10, e)
|
||||
print(now)
|
||||
today = now.tm_mday
|
||||
|
||||
|
|
@ -74,7 +83,13 @@ def sun_clock():
|
|||
return _rise, _set
|
||||
|
||||
# initial API call
|
||||
sunrise, sunset = sun_clock()
|
||||
try:
|
||||
sunrise, sunset = sun_clock()
|
||||
except Exception as e:
|
||||
reset_on_error(10, e)
|
||||
|
||||
print(sunrise)
|
||||
print(sunset)
|
||||
|
||||
# the sunrise/sunset time is returned as a JSON aka a string
|
||||
# this function chops up the string to get the hours and minutes as integers
|
||||
|
|
@ -82,19 +97,40 @@ def divide_time(z):
|
|||
string_time = z.split("-")
|
||||
clock_time = string_time[2].split("T")
|
||||
int_time = clock_time[1].split(":")
|
||||
return int(int_time[0]), int(int_time[1])
|
||||
event_time = time.struct_time(
|
||||
(int(string_time[0]), int(string_time[1]), int(clock_time[0]), int(int_time[0]),
|
||||
int(int_time[1]), 0, -1, -1, False)
|
||||
)
|
||||
# print(event_time)
|
||||
return event_time
|
||||
|
||||
rise_hour, rise_minute= divide_time(sunrise)
|
||||
set_hour, set_minute = divide_time(sunset)
|
||||
rise_time = divide_time(sunrise)
|
||||
set_time = divide_time(sunset)
|
||||
|
||||
# function that tracks how many hours/minutes until sunrise or sunset
|
||||
def sun_countdown(sun_hour, sun_minute):
|
||||
hours_until = sun_hour - now.tm_hour
|
||||
minutes_until = sun_minute - now.tm_min
|
||||
return hours_until, minutes_until
|
||||
|
||||
hours_until_sunset, mins_until_sunset = sun_countdown(set_hour, set_minute)
|
||||
hours_until_sunrise, mins_until_sunrise = sun_countdown(rise_hour, set_minute)
|
||||
def sun_countdown(sun_event):
|
||||
n = get_request(5, io.receive_time())
|
||||
remaining = time.mktime(sun_event) - time.mktime(n)
|
||||
r = remaining
|
||||
# print(remaining)
|
||||
# calculate the seconds remaining
|
||||
secs_remaining = remaining % 60 # pylint: disable=unused-variable
|
||||
remaining //= 60
|
||||
# calculate the minutes remaining
|
||||
minutes_until = remaining % 60
|
||||
remaining //= 60
|
||||
# calculate the hours remaining
|
||||
hours_until = remaining % 24
|
||||
remaining //= 24
|
||||
return r, hours_until, minutes_until, n
|
||||
try:
|
||||
total_until_rise, hours_until_sunrise, mins_until_sunrise, now = sun_countdown(rise_time)
|
||||
except Exception as e:
|
||||
reset_on_error(10, e)
|
||||
try:
|
||||
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
|
||||
except Exception as e:
|
||||
reset_on_error(10, e)
|
||||
|
||||
# red and yellow color percentage for neopixels
|
||||
percent_red = 0
|
||||
|
|
@ -107,11 +143,24 @@ PIN = board.A3 # This is the default pin on the NeoPixel Driver BFF.
|
|||
|
||||
pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
|
||||
|
||||
print(total_until_set)
|
||||
# check to see if the star fragment should be lit up on start-up
|
||||
if hours_until_sunset < 0:
|
||||
if total_until_set < 0:
|
||||
print("star glow true")
|
||||
star_glow = True
|
||||
percent_red = 255
|
||||
percent_yellow = 125
|
||||
# turn neopixels on using RGB values
|
||||
pixels.fill((percent_red, percent_yellow, 0))
|
||||
pixels.show()
|
||||
else:
|
||||
print("star glow false")
|
||||
star_glow = False
|
||||
percent_red = 0
|
||||
percent_yellow = 0
|
||||
# turn neopixels on using RGB values
|
||||
pixels.fill((percent_red, percent_yellow, 0))
|
||||
pixels.show()
|
||||
|
||||
# ticks time tracker
|
||||
clock = ticks_ms()
|
||||
|
|
@ -125,94 +174,101 @@ time_check = 900000
|
|||
looking_for_sunrise = False
|
||||
|
||||
while True:
|
||||
# if it's daytime
|
||||
if not star_glow:
|
||||
# every 15 minutes...
|
||||
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
|
||||
first_run = False
|
||||
# get the time from IO
|
||||
now = get_request(5, io.receive_time())
|
||||
print(now)
|
||||
print("pinging Open-Meteo")
|
||||
sunrise, sunset = sun_clock()
|
||||
hours_until_sunset, mins_until_sunset = sun_countdown(set_hour, set_minute)
|
||||
print("%d hour(s) until sunset" % hours_until_sunset)
|
||||
print("%d minutes(s) until sunset" % mins_until_sunset)
|
||||
print(sunset)
|
||||
print()
|
||||
# less than an hour until sunset...
|
||||
if hours_until_sunset == 0:
|
||||
# check every minute
|
||||
time_check = 60000
|
||||
# map color to ramp up in brightness over the course of the final hour
|
||||
percent_red = simpleio.map_range(mins_until_sunset, 59, 0, 0, 255)
|
||||
percent_yellow = simpleio.map_range(mins_until_sunset, 59, 0, 0, 125)
|
||||
# if the sun has set..
|
||||
if mins_until_sunset < 1:
|
||||
percent_red = 255
|
||||
percent_yellow = 125
|
||||
time_check = 900000
|
||||
star_glow = True
|
||||
print("star is glowing")
|
||||
# otherwise just keep checking every 15 minutes
|
||||
else:
|
||||
time_check = 900000
|
||||
percent_red = 0
|
||||
percent_yellow = 0
|
||||
# reset clock
|
||||
clock = ticks_add(clock, time_check)
|
||||
# if it's nighttime...
|
||||
else:
|
||||
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
|
||||
now = get_request(5, io.receive_time())
|
||||
# check to see if it's past midnight by seeing if the date has changed
|
||||
# includes some logic if you are starting up the project in the very early morning hours
|
||||
if today != now.tm_mday or (first_run and now.tm_hour < rise_hour):
|
||||
today = now.tm_mday
|
||||
looking_for_sunrise = True
|
||||
# begin tracking the incoming sunrise
|
||||
if looking_for_sunrise:
|
||||
try:
|
||||
# if it's daytime
|
||||
if not star_glow:
|
||||
# every 15 minutes...
|
||||
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
|
||||
print("pinging Open-Meteo")
|
||||
sunrise, sunset = sun_clock()
|
||||
hours_until_sunrise, mins_until_sunrise = sun_countdown(rise_hour, rise_minute)
|
||||
print("%d hour(s) until sunrise" % hours_until_sunrise)
|
||||
print("%d minutes(s) until sunrise" % mins_until_sunrise)
|
||||
print(sunrise)
|
||||
(total_until_set, hours_until_sunset,
|
||||
mins_until_sunset, now) = sun_countdown(set_time)
|
||||
print(now)
|
||||
print("%d hour(s) until sunset" % hours_until_sunset)
|
||||
print("%d minutes(s) until sunset" % mins_until_sunset)
|
||||
print(sunset)
|
||||
print(percent_red)
|
||||
print()
|
||||
# less than an hour until sunset...
|
||||
if hours_until_sunrise == 0:
|
||||
if hours_until_sunset in (0, 23):
|
||||
# check every minute
|
||||
time_check = 60000
|
||||
# map color to decrease brightness over the course of the final hour
|
||||
percent_red = simpleio.map_range(mins_until_sunrise, 59, 0, 255, 0)
|
||||
percent_yellow = simpleio.map_range(mins_until_sunrise, 59, 0, 125, 0)
|
||||
# if the sun has risen..
|
||||
if mins_until_sunrise < 1:
|
||||
percent_red = 0
|
||||
percent_yellow = 0
|
||||
time_check = 300000
|
||||
# map color to ramp up in brightness over the course of the final hour
|
||||
percent_red = simpleio.map_range(mins_until_sunset, 59, 0, 0, 255)
|
||||
percent_yellow = simpleio.map_range(mins_until_sunset, 59, 0, 0, 125)
|
||||
# if the sun has set..
|
||||
if total_until_set < 0:
|
||||
percent_red = 255
|
||||
percent_yellow = 125
|
||||
time_check = 900000
|
||||
star_glow = False
|
||||
looking_for_sunrise = False
|
||||
print("star is off")
|
||||
star_glow = True
|
||||
print("star is glowing")
|
||||
# otherwise just keep checking every 15 minutes
|
||||
else:
|
||||
time_check = 900000
|
||||
percent_red = 0
|
||||
percent_yellow = 0
|
||||
if first_run:
|
||||
first_run = False
|
||||
else:
|
||||
# reset clock
|
||||
clock = ticks_add(clock, time_check)
|
||||
# if it's nighttime...
|
||||
else:
|
||||
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
|
||||
if today != now.tm_mday or (first_run and now.tm_hour < rise_time.tm_hour):
|
||||
today = now.tm_mday
|
||||
looking_for_sunrise = True
|
||||
# begin tracking the incoming sunrise
|
||||
if looking_for_sunrise:
|
||||
print("pinging Open-Meteo")
|
||||
sunrise, sunset = sun_clock()
|
||||
(total_until_rise, hours_until_sunrise,
|
||||
mins_until_sunrise, now) = sun_countdown(rise_time)
|
||||
print(now)
|
||||
print("%d hour(s) until sunrise" % hours_until_sunrise)
|
||||
print("%d minutes(s) until sunrise" % mins_until_sunrise)
|
||||
print(sunrise)
|
||||
print(now)
|
||||
print()
|
||||
# less than an hour until sunset...
|
||||
if hours_until_sunrise in (0, 23):
|
||||
# check every minute
|
||||
time_check = 300000
|
||||
# map color to decrease brightness over the course of the final hour
|
||||
percent_red = simpleio.map_range(mins_until_sunrise, 59, 0, 255, 0)
|
||||
percent_yellow = simpleio.map_range(mins_until_sunrise, 59, 0, 125, 0)
|
||||
# if the sun has risen..
|
||||
if total_until_rise < 0:
|
||||
percent_red = 0
|
||||
percent_yellow = 0
|
||||
time_check = 900000
|
||||
star_glow = False
|
||||
looking_for_sunrise = False
|
||||
print("star is off")
|
||||
# otherwise just keep checking every 15 minutes
|
||||
# and keep neopixels on
|
||||
else:
|
||||
time_check = 900000
|
||||
percent_red = 255
|
||||
percent_yellow = 125
|
||||
# otherwise just keep checking every 15 minutes
|
||||
# and keep neopixels on
|
||||
else:
|
||||
now = get_request(5, io.receive_time())
|
||||
print("not looking for sunrise")
|
||||
print(now)
|
||||
print()
|
||||
time_check = 900000
|
||||
percent_red = 255
|
||||
percent_yellow = 125
|
||||
# otherwise just keep checking every 15 minutes
|
||||
# and keep neopixels on
|
||||
else:
|
||||
print("not looking for sunrise")
|
||||
print(now)
|
||||
print()
|
||||
time_check = 900000
|
||||
percent_red = 255
|
||||
percent_yellow = 125
|
||||
first_run = False
|
||||
# reset clock
|
||||
clock = ticks_add(clock, time_check)
|
||||
# turn neopixels on using RGB values
|
||||
pixels.fill((percent_red, percent_yellow, 0))
|
||||
pixels.show()
|
||||
if first_run:
|
||||
first_run = False
|
||||
else:
|
||||
# reset clock
|
||||
clock = ticks_add(clock, time_check)
|
||||
# turn neopixels on using RGB values
|
||||
pixels.fill((percent_red, percent_yellow, 0))
|
||||
pixels.show()
|
||||
except Exception as e:
|
||||
reset_on_error(10, e)
|
||||
|
|
|
|||
Loading…
Reference in a new issue