|
| 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.") |
0 commit comments