|
| 1 | +""" |
| 2 | +`rgb_led.py` |
| 3 | +======================================================================= |
| 4 | +Control a NeoPixel RGB LED using Adafruit IO and Python |
| 5 | +
|
| 6 | +Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-color |
| 7 | +
|
| 8 | +Adafruit invests time and resources providing this open source code. |
| 9 | +Please support Adafruit and open source hardware by purchasing |
| 10 | +products from Adafruit! |
| 11 | +
|
| 12 | +Author(s): Brent Rubell for Adafruit Industries |
| 13 | +Copyright (c) 2023 Adafruit Industries |
| 14 | +Licensed under the MIT license. |
| 15 | +All text above must be included in any redistribution. |
| 16 | +
|
| 17 | +Dependencies: |
| 18 | + - Adafruit_Blinka |
| 19 | + (https://github.com/adafruit/Adafruit_Blinka) |
| 20 | + - Adafruit_CircuitPython_NeoPixel |
| 21 | + (https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel) |
| 22 | +""" |
| 23 | +import time |
| 24 | +import board |
| 25 | +import neopixel |
| 26 | +from Adafruit_IO import Client, Feed, RequestError |
| 27 | + |
| 28 | +# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18 |
| 29 | +# NeoPixels must be connected to D10, D12, D18 or D21 to work. |
| 30 | +pixel_pin = board.D18 |
| 31 | + |
| 32 | +# The number of NeoPixels |
| 33 | +num_pixels = 1 |
| 34 | + |
| 35 | +# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! |
| 36 | +ORDER = neopixel.GRB |
| 37 | + |
| 38 | +pixels = neopixel.NeoPixel( |
| 39 | + pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER |
| 40 | +) |
| 41 | + |
| 42 | +# Set to your Adafruit IO key. |
| 43 | +# Remember, your key is a secret, |
| 44 | +# so make sure not to publish it when you publish this code! |
| 45 | +ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' |
| 46 | + |
| 47 | +# Set to your Adafruit IO username. |
| 48 | +# (go to https://accounts.adafruit.com to find your username) |
| 49 | +ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME' |
| 50 | + |
| 51 | +# Create an instance of the REST client. |
| 52 | +aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) |
| 53 | + |
| 54 | +try: # if we have a 'color' feed |
| 55 | + color = aio.feeds('color') |
| 56 | +except RequestError: # create an `color` feed |
| 57 | + feed = Feed(name='color') |
| 58 | + color = aio.create_feed(feed) |
| 59 | + |
| 60 | +while True: |
| 61 | + # get the value of the Adafruit IO `color` feed |
| 62 | + color_val = aio.receive(color.key) |
| 63 | + # Print hex value |
| 64 | + print('Received Color HEX: ', color_val) |
| 65 | + pixels.fill(color_val.value) |
| 66 | + pixels.show() |
| 67 | + |
| 68 | + # let's sleep/wait so we don't flood adafruit io's servers with requests |
| 69 | + time.sleep(3) |
0 commit comments