code.py
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.")