Skip to content

Commit 864049b

Browse files
authored
Merge pull request #2218 from caternuson/sunrise_lamp
Add code for Sunrise Lamp
2 parents a1304de + 0330cf5 commit 864049b

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import ssl
7+
import board
8+
import rtc
9+
import wifi
10+
import socketpool
11+
import adafruit_requests
12+
import neopixel
13+
14+
# --| User Config |--------------------------------
15+
WAKE_UP_HOUR = 7 # alarm time hour (24hr)
16+
WAKE_UP_MIN = 30 # alarm time minute
17+
SLEEP_COLOR = (0, 25, 150) # sleepy time color as tuple
18+
WAKEUP_COLOR = (255, 100, 0) # wake up color as tuple
19+
FADE_STEPS = 100 # wake up fade animation steps
20+
FADE_DELAY = 0.1 # wake up fade animation speed
21+
NEO_PIN = board.SCK # neopixel pin
22+
NEO_CNT = 12 # neopixel count
23+
# -------------------------------------------------
24+
25+
# Set up NeoPixels
26+
pixels = neopixel.NeoPixel(NEO_PIN, NEO_CNT)
27+
28+
# Get wifi details and more from a secrets.py file
29+
try:
30+
from secrets import secrets
31+
except ImportError:
32+
print("WiFi secrets are kept in secrets.py, please add them there!")
33+
raise
34+
35+
# Setup AIO time query URL
36+
TIME_URL = "https://io.adafruit.com/api/v2/"
37+
TIME_URL += secrets["aio_username"]
38+
TIME_URL += "/integrations/time/strftime?x-aio-key="
39+
TIME_URL += secrets["aio_key"]
40+
TIME_URL += "&fmt=%25Y%3A%25m%3A%25d%3A%25H%3A%25M%3A%25S"
41+
42+
# Connect to local network
43+
try:
44+
wifi.radio.connect(secrets["ssid"], secrets["password"])
45+
except ConnectionError:
46+
print("Wifi failed to connect.")
47+
while True:
48+
pixels.fill(0)
49+
time.sleep(0.5)
50+
pixels.fill(0x220000)
51+
time.sleep(0.5)
52+
53+
print("Wifi connected.")
54+
55+
# Get current time using AIO
56+
print("Fetching time.")
57+
pool = socketpool.SocketPool(wifi.radio)
58+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
59+
response = [int(x) for x in requests.get(TIME_URL).text.split(":")]
60+
response += [0, 0, -1]
61+
rtc.RTC().datetime = time.struct_time(response)
62+
63+
# Fill with sleepy time colors
64+
pixels.fill(SLEEP_COLOR)
65+
66+
# Wait for wake up time
67+
now = time.localtime()
68+
print("Current time: {}:{}".format(now.tm_hour, now.tm_min))
69+
print("Waiting for alarm {}:{}".format(WAKE_UP_HOUR, WAKE_UP_MIN))
70+
while not (now.tm_hour == WAKE_UP_HOUR and now.tm_min == WAKE_UP_MIN):
71+
# just sleep until next time check
72+
time.sleep(30)
73+
now = time.localtime()
74+
75+
# Sunrise animation
76+
print("Waking up!")
77+
r1, g1, b1 = SLEEP_COLOR
78+
r2, g2, b2 = WAKEUP_COLOR
79+
dr = (r2 - r1) / FADE_STEPS
80+
dg = (g2 - g1) / FADE_STEPS
81+
db = (b2 - b1) / FADE_STEPS
82+
83+
for _ in range(FADE_STEPS):
84+
r1 += dr
85+
g1 += dg
86+
b1 += db
87+
pixels.fill((int(r1), int(g1), int(b1)))
88+
time.sleep(FADE_DELAY)
89+
90+
print("Done.")
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import rtc
8+
import socketpool
9+
import wifi
10+
import adafruit_ntp
11+
import neopixel
12+
13+
# --| User Config |--------------------------------
14+
TZ_OFFSET = -7 # time zone offset in hours from UTC
15+
WAKE_UP_HOUR = 7 # alarm time hour (24hr)
16+
WAKE_UP_MIN = 30 # alarm time minute
17+
SLEEP_COLOR = (0, 25, 150) # sleepy time color as tuple
18+
WAKEUP_COLOR = (255, 100, 0) # wake up color as tuple
19+
FADE_STEPS = 100 # wake up fade animation steps
20+
FADE_DELAY = 0.1 # wake up fade animation speed
21+
NEO_PIN = board.SCK # neopixel pin
22+
NEO_CNT = 12 # neopixel count
23+
# -------------------------------------------------
24+
25+
# Set up NeoPixels
26+
pixels = neopixel.NeoPixel(NEO_PIN, NEO_CNT)
27+
28+
# Get wifi details and more from a secrets.py file
29+
try:
30+
from secrets import secrets
31+
except ImportError:
32+
print("WiFi secrets are kept in secrets.py, please add them there!")
33+
raise
34+
35+
# Connect to local network
36+
try:
37+
wifi.radio.connect(secrets["ssid"], secrets["password"])
38+
except ConnectionError:
39+
print("Wifi failed to connect.")
40+
while True:
41+
pixels.fill(0)
42+
time.sleep(0.5)
43+
pixels.fill(0x220000)
44+
time.sleep(0.5)
45+
46+
print("Wifi connected.")
47+
48+
# Get current time using NTP
49+
print("Fetching time from NTP.")
50+
pool = socketpool.SocketPool(wifi.radio)
51+
ntp = adafruit_ntp.NTP(pool, tz_offset=TZ_OFFSET)
52+
rtc.RTC().datetime = ntp.datetime
53+
54+
# Fill with sleepy time colors
55+
pixels.fill(SLEEP_COLOR)
56+
57+
# Wait for wake up time
58+
now = time.localtime()
59+
print("Current time: {:2}:{:02}".format(now.tm_hour, now.tm_min))
60+
print("Waiting for alarm {:2}:{:02}".format(WAKE_UP_HOUR, WAKE_UP_MIN))
61+
while not (now.tm_hour == WAKE_UP_HOUR and now.tm_min == WAKE_UP_MIN):
62+
# just sleep until next time check
63+
time.sleep(30)
64+
now = time.localtime()
65+
66+
# Sunrise animation
67+
print("Waking up!")
68+
r1, g1, b1 = SLEEP_COLOR
69+
r2, g2, b2 = WAKEUP_COLOR
70+
dr = (r2 - r1) / FADE_STEPS
71+
dg = (g2 - g1) / FADE_STEPS
72+
db = (b2 - b1) / FADE_STEPS
73+
74+
for _ in range(FADE_STEPS):
75+
r1 += dr
76+
g1 += dg
77+
b1 += db
78+
pixels.fill((int(r1), int(g1), int(b1)))
79+
time.sleep(FADE_DELAY)
80+
81+
print("Done.")

0 commit comments

Comments
 (0)