From d92d3a66302ac6a40c34a12811c5a316ab832ef9 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 13 Aug 2024 21:28:45 -0400 Subject: [PATCH 1/2] adding Blinka NeoPixel countdown code adding Blinka NeoPixel countdown code. time is fetched from IO. when 16 or less days are remaining until the event, the neopixels light up one by one --- .../Blinka_NeoPixel_Countdown/code.py | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py diff --git a/CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py b/CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py new file mode 100644 index 000000000..780e1c5e4 --- /dev/null +++ b/CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py @@ -0,0 +1,78 @@ +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries + +# SPDX-License-Identifier: MIT + +import os +import time +import wifi +import microcontroller +import board +import neopixel +import adafruit_connection_manager +import adafruit_requests +from adafruit_io.adafruit_io import IO_HTTP +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff + +timezone = "America/New_York" +color = 0xFF00FF +# The time of the thing! +EVENT_YEAR = 2024 +EVENT_MONTH = 8 +EVENT_DAY = 16 +EVENT_HOUR = 0 +EVENT_MINUTE = 0 +# we'll make a python-friendly structure +event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY, + EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds + -1, -1, False)) # we dont know day of week/year or DST + +print("Connecting to WiFi...") +wifi.radio.connect( + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") +) +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) +requests = adafruit_requests.Session(pool, ssl_context) +io = IO_HTTP( + os.getenv("AIO_USERNAME"), os.getenv("AIO_KEY"), requests +) + +pixel_pin = board.SCL1 +pixel_num = 16 +pixels = neopixel.NeoPixel(pixel_pin, n = pixel_num, brightness=1, auto_write=True) +pixel_length = 0 +last_length = -1 + +refresh_clock = ticks_ms() +refresh_timer = 3600 * 1000 # 1 hour +first_run = True +finished = False + +while True: + if not finished: + if ticks_diff(ticks_ms(), refresh_clock) >= refresh_timer or first_run: + try: + print("Getting time from internet!") + now = time.struct_time(io.receive_time(timezone)) + print(now) + total_seconds = time.mktime(now) + remaining = time.mktime(event_time) - total_seconds + if remaining < 0: + pixel_length = pixel_num + 1 + finished = True + else: + if now.tm_mon == EVENT_MONTH: + pixel_length = now.tm_mday % (pixel_num + 1) + refresh_clock = ticks_add(refresh_clock, refresh_timer) + except Exception as e: # pylint: disable=broad-except + print("Some error occured, retrying via reset in 15 seconds! - ", e) + time.sleep(15) + microcontroller.reset() + if last_length != pixel_length: + if not pixel_length: + pixels.fill(0x000000) + else: + for i in range(pixel_length): + pixels[i] = color + last_length = pixel_length + first_run = False From b4aa60735bf281c98325dc978960ddd0dbaa6cd3 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 13 Aug 2024 21:31:41 -0400 Subject: [PATCH 2/2] Update code.py --- .../Blinka_NeoPixel_Countdown/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py b/CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py index 780e1c5e4..8a60f454d 100644 --- a/CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py +++ b/CircuitPython_Day_2024_Projects/Blinka_NeoPixel_Countdown/code.py @@ -1,5 +1,5 @@ # SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries - +# # SPDX-License-Identifier: MIT import os