MQTT (from webIDE) with IP address not working


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"

#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() { 

    // connect to the server

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("sparkoutTopic","hello world");
        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);;
void loop() {
    if (client.isConnected())

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!

read my post:
I’ve the same kind of issue! it seems that the Web IDE build does not compile correctly today…

Perhaps some expert could help?

:slight_smile: If I add


into my setup() it works immediately.

So I guess I either need to find out why its failing to connect to the spark cloud, and find a simple way of re-connecting to the cloud whilst I develop the code.

Ought not the two lines Adafruit… and MQTT… be inside setup()?

They aren’t within the setup() in the library examples.

But I tried moving them into the setup() the program won’t compile with “‘client’ was not declared in this scope” errors etc.

Given that the programme runs if I use Spark.disconnect(); it would appear to be that the connection or attempt to connect to the Spark cloud is somehow blocking the MQTT connection?

Did you ever get this working? I am having issues also with MQTT not talking to my mosquitto broker.

Perfectly honest I ditched using the spark due to the poor level of support available and the multiple issues I was encountering (it’s supposed to be fun).

I found I had to to turn the spark cloud connection off to get MQTT to work reliably. The problem then is turning the spark cloud connection back on again to enable to flash a new programme onto the spark. Life is too short!

I hear you, there are some frustrating things I have encountered also. I figured out that Serial was breaking my MQTT, once I removed that it is working fine.

1 Like