Hello,
I’m trying to use the MQTT library from within the webIDE, but with an IP address. Unfortunately I cannot get it to work.
#include "neopixel/neopixel.h"
// This #include statement was automatically added by the Spark IDE.
#include "MQTT/MQTT.h"
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D3
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812
byte ip[] = { 192, 168, 0, 22 };
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
MQTT client( ip , 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);
Serial.println("message recieved");
client.publish("sparkoutTopic","color recieved");
colorWipe(strip.Color(0, 255, 0), 50); // Green
}
void setup() {
Serial.begin(57600);
strip.begin();
strip.show();
delay(2000);
strip.setBrightness(10);
Serial.println("started");
// connect to the server
client.connect("sparkclient");
// publish/subscribe
if (client.isConnected()) {
Serial.println("connected");
client.publish("sparkoutTopic","hello world");
client.subscribe("display/cheerlights/newcolor");
colorWipe(strip.Color(255, 0, 0), 50); // Red
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void loop() {
if (client.isConnected())
client.loop();
}
I have a mosquitto MQTT broker, and a Node-red instance posting a new message to that topic every 30 seconds. However when I push the app to my spark core, the only message returned to the Serial port is the initial “started”. I’m assuming that the programme is therefore not connecting to the MQTT broker, but I’m at a loss as to where I am going wrong. Any suggestions would be gratefully appreciated.
Many thanks!