I have the simple example from the MQTT library running, but when I attempt to add Neopixel support into the callback function, I get an error, specifically when I call strip.show();
Code:
#include "neopixel/neopixel.h"
#include "MQTT/MQTT.h"
#define PIXEL_PIN D2
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void callback(char* topic, byte* payload, unsigned int length);
byte server[] = {000,000,000,00};
MQTT client(server, 1883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message.equals("2"))
// RGB.color(255, 0, 0);
rainbow(1);
strip.show();
else if (message.equals("1"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
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("outTopic","hello world");
client.subscribe("rooms/10");
}
}
void loop() {
if (client.isConnected())
client.loop();
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Error Output:
mqtttest.cpp: In function 'void callback(char*, byte*, unsigned int)':
mqtttest.cpp:25:15: warning: converting to non-pointer type 'char' from NULL [-Wconversion-null]
byte server[] = {184,169,164,30};
^
mqtttest.cpp:31:5: error: 'else' without a previous 'if'
memcpy(p, payload, length);
^
make[1]: *** [../build/target/user/platform-6mqtttest.o] Error 1
make: *** [user] Error 2

