Skip to content

Commit 8dd940d

Browse files
committed
adding python AIO example
Adding color picker example for Pixel Trinkey. Trinkey runs an arduino sketch listening for a color value on the COM port. Python desktop sketch listens for a new color from an AIO feed
1 parent 0997d95 commit 8dd940d

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
7+
#define NUMPIXELS 30
8+
Adafruit_NeoPixel neostrip(NUMPIXELS, PIN_DATA, NEO_GRB + NEO_KHZ800);
9+
10+
char colorBuffer[9]; // Buffer to hold the incoming color data (0x000000)
11+
int bufferIndex = 0; // Index for the buffer
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
16+
neostrip.begin();
17+
neostrip.setBrightness(25);
18+
neostrip.show();
19+
memset(colorBuffer, 0, sizeof(colorBuffer)); // Clear the buffer
20+
}
21+
22+
void loop() {
23+
// Check if data is available on the serial port
24+
while (Serial.available() > 0) {
25+
char incomingByte = Serial.read();
26+
27+
// Check if the incoming byte is part of the color data
28+
if (isxdigit(incomingByte) || incomingByte == 'x') {
29+
colorBuffer[bufferIndex++] = incomingByte; // Add the byte to the buffer
30+
}
31+
32+
// If the buffer is full, process the color data
33+
if (bufferIndex == 8) {
34+
colorBuffer[8] = '\0'; // Null-terminate the string
35+
36+
// Convert the hex string to a 32-bit integer
37+
uint32_t color = strtoul(colorBuffer, NULL, 16);
38+
39+
// Extract RGB values from the color
40+
uint8_t r = (color >> 16) & 0xFF;
41+
uint8_t g = (color >> 8) & 0xFF;
42+
uint8_t b = color & 0xFF;
43+
44+
// Set the NeoPixels to the received color
45+
for (int i = 0; i < NUMPIXELS; i++) {
46+
neostrip.setPixelColor(i, neostrip.Color(r, g, b));
47+
}
48+
neostrip.show();
49+
50+
// Clear the buffer and reset the bufferIndex
51+
memset(colorBuffer, 0, sizeof(colorBuffer));
52+
bufferIndex = 0;
53+
}
54+
}
55+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import serial
7+
from Adafruit_IO import Client, MQTTClient
8+
9+
# Configuration
10+
com_port = 'COM123' # Adjust this to your COM port
11+
baud_rate = 115200
12+
FEED_ID = 'pixel-feed'
13+
# Set to your Adafruit IO key.
14+
# Remember, your key is a secret,
15+
# so make sure not to publish it when you publish this code!
16+
ADAFRUIT_IO_KEY = 'your-aio-key'
17+
18+
# Set to your Adafruit IO username.
19+
# (go to https://accounts.adafruit.com to find your username)
20+
ADAFRUIT_IO_USERNAME = 'your-aio-username'
21+
22+
print("Connecting to Adafruit IO...")
23+
# Create an instance of the REST client.
24+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
25+
26+
# Initialize the serial connection
27+
ser = serial.Serial(com_port, baud_rate)
28+
29+
# Define callback functions
30+
def connected(client):
31+
client.subscribe(FEED_ID)
32+
# pylint: disable=unused-argument, consider-using-sys-exit
33+
def subscribe(client, userdata, mid, granted_qos):
34+
# This method is called when the client subscribes to a new feed.
35+
print(f"Subscribed to {FEED_ID}")
36+
37+
def disconnected(client):
38+
print('Disconnected from Adafruit IO!')
39+
exit(1)
40+
41+
def message(client, feed_id, payload):
42+
print(f'Feed {feed_id} received new value: {payload}')
43+
color_value = payload.strip().replace('#', '0x') # Replace # with 0x
44+
ser.write(color_value.encode()) # Send the color value to the serial port
45+
46+
# Initialize the Adafruit IO MQTT client
47+
mqtt_client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
48+
49+
# Setup the callback functions
50+
mqtt_client.on_connect = connected
51+
mqtt_client.on_disconnect = disconnected
52+
mqtt_client.on_message = message
53+
mqtt_client.on_subscribe = subscribe
54+
55+
# Connect to Adafruit IO
56+
mqtt_client.connect()
57+
58+
# Start a background thread to listen for MQTT messages
59+
mqtt_client.loop_blocking()
60+
61+
# Keep the script running
62+
while True:
63+
try:
64+
time.sleep(1)
65+
except KeyboardInterrupt:
66+
print('Script interrupted by user')
67+
break
68+
69+
# Close the serial connection when done
70+
ser.close()

0 commit comments

Comments
 (0)