Skip to content

adding python AIO example #2825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include <Adafruit_NeoPixel.h>

#define NUMPIXELS 30
Adafruit_NeoPixel neostrip(NUMPIXELS, PIN_DATA, NEO_GRB + NEO_KHZ800);

char colorBuffer[9]; // Buffer to hold the incoming color data (0x000000)
int bufferIndex = 0; // Index for the buffer

void setup() {
Serial.begin(115200);

neostrip.begin();
neostrip.setBrightness(25);
neostrip.show();
memset(colorBuffer, 0, sizeof(colorBuffer)); // Clear the buffer
}

void loop() {
// Check if data is available on the serial port
while (Serial.available() > 0) {
char incomingByte = Serial.read();

// Check if the incoming byte is part of the color data
if (isxdigit(incomingByte) || incomingByte == 'x') {
colorBuffer[bufferIndex++] = incomingByte; // Add the byte to the buffer
}

// If the buffer is full, process the color data
if (bufferIndex == 8) {
colorBuffer[8] = '\0'; // Null-terminate the string

// Convert the hex string to a 32-bit integer
uint32_t color = strtoul(colorBuffer, NULL, 16);

// Extract RGB values from the color
uint8_t r = (color >> 16) & 0xFF;
uint8_t g = (color >> 8) & 0xFF;
uint8_t b = color & 0xFF;

// Set the NeoPixels to the received color
for (int i = 0; i < NUMPIXELS; i++) {
neostrip.setPixelColor(i, neostrip.Color(r, g, b));
}
neostrip.show();

// Clear the buffer and reset the bufferIndex
memset(colorBuffer, 0, sizeof(colorBuffer));
bufferIndex = 0;
}
}
}
Binary file not shown.
68 changes: 68 additions & 0 deletions Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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()
Loading