MQTT (from webIDE) with IP address not working

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!

read my post: http://community.spark.io/t/is-there-an-issue-currently-with-web-ide-build/8644
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

Spark.disconnect();

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