Hi,
Don’t know if this is the correct location. I have an question since I am no programmer. I would like to use MQTT to set Neopixel LED strips. I have code that sets a simple color on the ledstrip. What I really want is set an animation depending on the MQTT message received. I will paste the code I have, which works. But if someone can help me out getting real strip animations in. I need there need to be some loop going on and that this may cause issues when “listening” for the next MQTT message. The thing is … I have no clue
// This #include statement was automatically added by the Particle ID
#include "MQTT.h"
#include "neopixel.h"
void callback(char* topic, byte* payload, unsigned int length);
/**
* if want to use IP address,
* byte server[] = { XXX,XXX,XXX,XXX };
* MQTT client(server, 1883, callback);
* want to use domain name,
* exp) iot.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started
* MQTT client("iot.eclipse.org", 1883, callback);
**/
byte server[] = { 192,168,1,5 };
MQTT client(server, 1883, callback);
#define PIXEL_PIN D2
#define PIXEL_COUNT 144
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int i;
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
//First animation if RED is send via MQTT. The actual function is between the 2 {} brackets
if (!strcmp(p, "PURPLE")) {
//The actual animation starts here
for(i=0; i < PIXEL_COUNT; i++) {
strip.setPixelColor(i, 180, 3, 180);
strip.show();
}
//The animation ends here
}
else if (!strcmp(p, "ORANGE")){
for(i=0; i < PIXEL_COUNT; i++) {
strip.setPixelColor(i, 200, 50, 5);
strip.show();
}
}
else if (!strcmp(p, "OFF")){
for(i=0; i < PIXEL_COUNT; i++) {
strip.setPixelColor(i, 0, 0, 0);
strip.show();
}
}
else
{
for(i=0; i < PIXEL_COUNT; i++) {
strip.setPixelColor(i, 0, 0, 0);
strip.show();
}
}
}
void setup() {
RGB.control(true);
strip.begin();
strip.show(); // Initialize all pixels to 'off
// connect to the server
client.connect("sparkclient");
// publish/subscribe
if (client.isConnected()) {
client.publish("photon/message","hello world");
client.subscribe("photon/color");
}
}
void loop() {
if (client.isConnected())
client.loop();
}