Ubidots MQTT crashing

Hi all, I am able to compile and flash the code below, but I then receive the dreaded red flashing LED on my photon and have to do a reset. Any tips appreciated!
NOTE: I am able to read the sensor in a seperate file so everything wired OK. I have commented out my token etc.

project.properties
dependencies.Adafruit_DHT=0.0.2
dependencies.UbidotsMQTT=1.0.0
dependencies.MQTT=0.4.6

#include "Adafruit_DHT.h"
#include "UbidotsMQTT.h"

#define TOKEN "XXX"  // Add here your Ubidots TOKEN
#define VARIABLE_ONE "XXX" // A variable identifier, in lowercase without spaces, commas or special characters
#define VARIABLE_TWO "XXX" // A variable identifier, in lowercase without spaces, commas or special characters
#define DATA_SOURCE_NAME "my-particle-source" // A unique identifier for your data source, in lowercase without spaces, commas or special characters

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT11		// DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE);

void callback(char* topic, byte* payload, unsigned int length);
Ubidots client(TOKEN, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);
    Serial.write(payload, length);
    Serial.println(topic);
}

void setup() {
	dht.begin();
  while (client.connect());
  client.setDataSourceLabel(DATA_SOURCE_NAME);
}

void loop() {
	delay(5000);
	float h = dht.getHumidity();
	float t = dht.getTempCelcius();

	Particle.publish("Humidity", String(h));
  Particle.publish("Temperature", String(t));

  client.add(VARIABLE_ONE, h);
  client.add(VARIABLE_TWO, t);
  client.sendValues();
}

The MQTT library does not work.

You need to use the more up-to-date library called just Ubidots.

1 Like

Hi @robmarkcole,

At the moment we’re fixing some issues in our MQTT library. You can try with the other one called Ubidots as Ryan said.

If you need to use MQTT, you can follow this example using the MQTT library from Particle.

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>
#include <MQTT.h>

#define TOKEN "...."
#define CLIENT_ID "aksjdlkajdn"

//long temperature;
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT11		// DHT 22 (AM2302)

void callback(char* topic, byte* payload, unsigned int length);

MQTT client("things.ubidots.com", 1883, callback);
DHT dht(DHTPIN, DHTTYPE);

uint16_t qos2messageid = 0;

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);
    delay(1000);
}

void setup() {
    Serial.begin(9600);
    dht.begin();
    while(!client.isConnected()){
        Serial.println("Attemping to connect");
        client.connect(CLIENT_ID, TOKEN, NULL);
        delay(5000);
    }
    
    Serial.println("connected");
    
    uint8_t messageid;
    
    if (client.isConnected()) {
        uint16_t messageid;
        //client.publish("/v1.6/devices/sensor","{\"temp\":3}", MQTT::QOS1, &messageid);
    }
}

void loop() {
    
    while(!client.isConnected()){
        Serial.println("Attemping to connect");
        client.connect(CLIENT_ID, TOKEN, NULL);
        delay(5000);
    }
	
    float temp = dht.getTempCelcius();
	
    if (client.isConnected()){
        uint16_t messageid;
        String t ="{\"temperature\":";
        String value = String(temp); 
        t.concat(value);
        t.concat("}");
        client.publish("/v1.6/devices/sensor", t, MQTT::QOS1, &messageid);
        client.loop();
        delay(5000);
    }
}

Regards -
Maria C.

1 Like

Thanks Maria, that works! However can you explain CLIENT_ID “aksjdlkajdn” where does this come from? Also where do you view the prints to serial in the IDE?
Cheers
Robin

Hey @robmarkcole,

The CLIENT_ID is the MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name.

In my case I use the Serial monitor of the Arduino IDE, choosing the port of my particle device.