The Pixel Trinkey does not have built-in WiFi, but that won't stop this board from joining the internet of things! Since its designed to be most at home in a USB port, it can be used alongside a desktop CPython script.
In this example, the Trinkey runs code that listens for color data over the Serial port. If a new value is sent, then that color is written to the NeoPixels connected to the terminal block. The CPython script listens for a new color value on the Adafruit IO feed. You can use the color picker module on an Adafruit IO dashboard to pick your color.
Adafruit IO Prep
Before running this script, you'll need to setup an Adafruit IO account. This getting started guide will help you through that process and help you to become familiar with how Adafruit IO works.
Connect your NeoPixels to the terminal block on the Pixel Trinkey:
- Board GND to NeoPixel GND (black wire)
- Board DATA to NeoPixel DATA IN (blue wire)
- Board +5V to NeoPixel VIN (red wire)
CPython Pixel Trinkey Adafruit IO Code
To run the script you will need a desktop or laptop computer with Python 3 installed.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import serial from Adafruit_IO import MQTTClient # Configuration com_port = 'COM123' # Adjust this to your COM port baud_rate = 115200 FEED_ID = 'pixel-feed' # Set to your Adafruit IO key. # Remember, your key is a secret, # so make sure not to publish it when you publish this code! ADAFRUIT_IO_KEY = 'your-aio-key' # Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = 'your-aio-username' print("Connecting to Adafruit IO...") # Initialize the serial connection ser = serial.Serial(com_port, baud_rate) # Define callback functions def connected(client): client.subscribe(FEED_ID) # pylint: disable=unused-argument, consider-using-sys-exit def subscribe(client, userdata, mid, granted_qos): # This method is called when the client subscribes to a new feed. print(f"Subscribed to {FEED_ID}") def disconnected(client): print('Disconnected from Adafruit IO!') exit(1) def message(client, feed_id, payload): print(f'Feed {feed_id} received new value: {payload}') color_value = payload.strip().replace('#', '0x') # Replace # with 0x ser.write(color_value.encode()) # Send the color value to the serial port # Initialize the Adafruit IO MQTT client mqtt_client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) # Setup the callback functions mqtt_client.on_connect = connected mqtt_client.on_disconnect = disconnected mqtt_client.on_message = message mqtt_client.on_subscribe = subscribe # Connect to Adafruit IO mqtt_client.connect() # Start a background thread to listen for MQTT messages mqtt_client.loop_blocking() # Keep the script running while True: try: time.sleep(1) except KeyboardInterrupt: print('Script interrupted by user') break # Close the serial connection when done ser.close()
Pixel Trinkey Code
The code running on the Trinkey uses the Serial port to read the incoming color values and write them to the NeoPixels. It has been compiled into a convenient .UF2 file for an RGB strip with 30 pixels. You can drag and drop the file onto the Trinkey. If you need to adjust the number or pixels or pixel type, you can edit the script here.
CPython Dependencies
First, you'll use pip
to install the Python libraries required to run the script:
pip install pyserial pip install adafruit-io
Customize the Script
Open the code.py script in your preferred text editor or IDE. At the top of the code, you'll need to update com_port
to match the COM port that your Trinkey is plugged into. Otherwise, the script will not work.
You'll also need to update FEED_ID
, ADAFRUIT_IO_USERNAME
and ADAFRUIT_IO_KEY
with your Adafruit IO feed name, username and key.
# Configuration com_port = 'COM123' # Adjust this to your COM port baud_rate = 115200 FEED_ID = 'pixel-feed' # Set to your Adafruit IO key. # Remember, your key is a secret, # so make sure not to publish it when you publish this code! ADAFRUIT_IO_KEY = 'your-aio-key' # Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = 'your-aio-username'
Update the COM port and Adafruit IO account information before running the script!
Run the Script
After adding your COM port and Adafruit IO account information, you can run the code.py script inside a terminal window on your computer with:
python code.py
Navigate to your feed on Adafruit IO. You can send color values to the feed that you are listening to in the CPython script.
Text editor powered by tinymce.