|
| 1 | +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries |
| 2 | + |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import os |
| 6 | +import time |
| 7 | +import wifi |
| 8 | +import microcontroller |
| 9 | +import board |
| 10 | +import neopixel |
| 11 | +import adafruit_connection_manager |
| 12 | +import adafruit_requests |
| 13 | +from adafruit_io.adafruit_io import IO_HTTP |
| 14 | +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff |
| 15 | + |
| 16 | +timezone = "America/New_York" |
| 17 | +color = 0xFF00FF |
| 18 | +# The time of the thing! |
| 19 | +EVENT_YEAR = 2024 |
| 20 | +EVENT_MONTH = 8 |
| 21 | +EVENT_DAY = 16 |
| 22 | +EVENT_HOUR = 0 |
| 23 | +EVENT_MINUTE = 0 |
| 24 | +# we'll make a python-friendly structure |
| 25 | +event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY, |
| 26 | + EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds |
| 27 | + -1, -1, False)) # we dont know day of week/year or DST |
| 28 | + |
| 29 | +print("Connecting to WiFi...") |
| 30 | +wifi.radio.connect( |
| 31 | + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 32 | +) |
| 33 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 34 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 35 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 36 | +io = IO_HTTP( |
| 37 | + os.getenv("AIO_USERNAME"), os.getenv("AIO_KEY"), requests |
| 38 | +) |
| 39 | + |
| 40 | +pixel_pin = board.SCL1 |
| 41 | +pixel_num = 16 |
| 42 | +pixels = neopixel.NeoPixel(pixel_pin, n = pixel_num, brightness=1, auto_write=True) |
| 43 | +pixel_length = 0 |
| 44 | +last_length = -1 |
| 45 | + |
| 46 | +refresh_clock = ticks_ms() |
| 47 | +refresh_timer = 3600 * 1000 # 1 hour |
| 48 | +first_run = True |
| 49 | +finished = False |
| 50 | + |
| 51 | +while True: |
| 52 | + if not finished: |
| 53 | + if ticks_diff(ticks_ms(), refresh_clock) >= refresh_timer or first_run: |
| 54 | + try: |
| 55 | + print("Getting time from internet!") |
| 56 | + now = time.struct_time(io.receive_time(timezone)) |
| 57 | + print(now) |
| 58 | + total_seconds = time.mktime(now) |
| 59 | + remaining = time.mktime(event_time) - total_seconds |
| 60 | + if remaining < 0: |
| 61 | + pixel_length = pixel_num + 1 |
| 62 | + finished = True |
| 63 | + else: |
| 64 | + if now.tm_mon == EVENT_MONTH: |
| 65 | + pixel_length = now.tm_mday % (pixel_num + 1) |
| 66 | + refresh_clock = ticks_add(refresh_clock, refresh_timer) |
| 67 | + except Exception as e: # pylint: disable=broad-except |
| 68 | + print("Some error occured, retrying via reset in 15 seconds! - ", e) |
| 69 | + time.sleep(15) |
| 70 | + microcontroller.reset() |
| 71 | + if last_length != pixel_length: |
| 72 | + if not pixel_length: |
| 73 | + pixels.fill(0x000000) |
| 74 | + else: |
| 75 | + for i in range(pixel_length): |
| 76 | + pixels[i] = color |
| 77 | + last_length = pixel_length |
| 78 | + first_run = False |
0 commit comments