|
| 1 | +import ssl |
| 2 | +import board |
| 3 | +import neopixel |
| 4 | +import adafruit_requests |
| 5 | +import socketpool |
| 6 | +import wifi |
| 7 | +from adafruit_io.adafruit_io import IO_HTTP |
| 8 | +from adafruit_pixel_framebuf import PixelFramebuffer |
| 9 | +# adafruit_circuitpython_adafruitio usage with native wifi networking |
| 10 | + |
| 11 | +# Neopixel matrix configuration |
| 12 | +PIXEL_PIN = board.IO6 |
| 13 | +PIXEL_WIDTH = 12 |
| 14 | +PIXEL_HEIGHT = 12 |
| 15 | + |
| 16 | +# secrets.py has SSID/password and adafruit.io |
| 17 | +try: |
| 18 | + from secrets import secrets |
| 19 | +except ImportError: |
| 20 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 21 | + raise |
| 22 | +AIO_USERNAME = secrets["aio_username"] |
| 23 | +AIO_KEY = secrets["aio_key"] |
| 24 | + |
| 25 | +# LED matrix creation |
| 26 | +PIXELS = neopixel.NeoPixel( |
| 27 | + PIXEL_PIN, PIXEL_WIDTH * PIXEL_HEIGHT, brightness=0.5, auto_write=False, |
| 28 | +) |
| 29 | + |
| 30 | +PIXEL_FRAMEBUF = PixelFramebuffer( |
| 31 | + PIXELS, |
| 32 | + PIXEL_WIDTH, |
| 33 | + PIXEL_HEIGHT, |
| 34 | + alternating=True, |
| 35 | + rotation=1, |
| 36 | + reverse_x=True |
| 37 | + ) |
| 38 | + |
| 39 | +# Adafruit.io feeds setup |
| 40 | +QUOTE_FEED = "sign-quotes.signtext" |
| 41 | +COLOR_FEED = "sign-quotes.signcolor" |
| 42 | +CURRENT_TEXT = "Merry Christmas!" |
| 43 | +CURRENT_COLOR = 0xFFFFFF |
| 44 | + |
| 45 | +# Helper function to get updated data from Adafruit.io |
| 46 | +def update_data(): |
| 47 | + global CURRENT_TEXT, CURRENT_COLOR |
| 48 | + print("Updating data from Adafruit IO") |
| 49 | + try: |
| 50 | + quote_feed = IO.get_feed(QUOTE_FEED) |
| 51 | + quotes_data = IO.receive_data(quote_feed["key"]) |
| 52 | + CURRENT_TEXT = quotes_data["value"] |
| 53 | + color_feed = IO.get_feed(COLOR_FEED) |
| 54 | + color_data = IO.receive_data(color_feed["key"]) |
| 55 | + CURRENT_COLOR = int(color_data["value"][1:], 16) |
| 56 | + # pylint: disable=broad-except |
| 57 | + except Exception as error: |
| 58 | + print(error) |
| 59 | + |
| 60 | + |
| 61 | +# Connect to WiFi |
| 62 | +print("Connecting to %s" % secrets["ssid"]) |
| 63 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 64 | +print("Connected to %s!" % secrets["ssid"]) |
| 65 | + |
| 66 | +# Setup Adafruit IO connection |
| 67 | +POOL = socketpool.SocketPool(wifi.radio) |
| 68 | +REQUESTS = adafruit_requests.Session(POOL, ssl.create_default_context()) |
| 69 | +# Initialize an Adafruit IO HTTP API object |
| 70 | +IO = IO_HTTP(AIO_USERNAME, AIO_KEY, REQUESTS) |
| 71 | + |
| 72 | + |
| 73 | +while True: |
| 74 | + update_data() |
| 75 | + print("Displaying", CURRENT_TEXT, "in", hex(CURRENT_COLOR)) |
| 76 | + |
| 77 | + for i in range(12 * len(CURRENT_TEXT) + PIXEL_WIDTH): |
| 78 | + PIXEL_FRAMEBUF.fill(0x000000) |
| 79 | + PIXEL_FRAMEBUF.pixel(0, 0, 0x000000) |
| 80 | + PIXEL_FRAMEBUF.text(CURRENT_TEXT, PIXEL_WIDTH - i, 3, CURRENT_COLOR) |
| 81 | + PIXEL_FRAMEBUF.display() |
0 commit comments