New Ubidots Library using MQTT

Hey,

Just uploaded an MQTT library to publish and subscribe to Ubidots Cloud.

To use this library you will need to include MQTT library and UbidotsMQTT library.y.

Here some examples:

Publish:

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

// This #include statement was automatically added by the Particle IDE.
#include "UbidotsMQTT.h"
#define TOKEN "Your_Token_Here"  // Add here your Ubidots TOKEN
#define VARIABLE_IDENTIFIER_ONE "humidity" // Add a variable identifier, it must be in lowercase
#define VARIABLE_IDENTIFIER_TWO "temperature" // Add a variable identifier, it must be in lowercase
#define DATA_SOURCE_NAME "My_beautiful_device"

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() {
    Serial.begin(115200);
    while (client.connect());
    client.setDataSourceLabel(DATA_SOURCE_NAME);
}

void loop() {
    float value_one = analogRead(A0);
    float value_two = analogRead(A1);
    client.add(VARIABLE_IDENTIFIER_ONE, value_one);
    client.add(VARIABLE_IDENTIFIER_TWO, value_two);
    client.sendValues();
}

Subscribe:

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

// This #include statement was automatically added by the Particle IDE.
#include "UbidotsMQTT.h"
#define TOKEN "Your_Token_Here"  // Add here your Ubidots TOKEN
#define DATA_SOURCE_IDENTEFIER "23002e000551343530xxxxx" // Add the data source label of Ubidots Cloud
#define VARIABLE_IDENTIFIER "temperature" // Add a variable identifier, it must be in lowercase

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() {
    Serial.begin(115200);
    while (client.connect()) {
        client.getValueSubscribe(DATA_SOURCE_IDENTEFIER, VARIABLE_IDENTEFIER);
    }
}

void loop() {
    client.loop();
}

Best regards,
Metavix

2 Likes

Sorry for the late reply! Wanted to thank you for contributing! Did you build anything using the library?

@will Yes, i built a controller for air conditioner. It uses a subscribe method to get the value of temperature of variable on Ubidots Cloud, if it is higher than 25°C the air conditioner turn on automatically.

I used the subscribe example adding 3 new lines
in setup i added:

pinMode(1, OUTPUT);

and in the callback function i added:


if (p.toInt() > 25) {
digitalWrite(1, HIGH);
} else { 
digitalWrite(1, LOW);
}

Best regards,
Metavix

1 Like

That’s awesome! Thanks again for the share :slight_smile:

@Metavix @aguspg Can you tell me the benefits of using the MQTT Ubidots Library vs the old Ubidots library?

Does MQTT have lower data transfer rates which would be good when using the Electron where data can add up and get expensive?

I know MQTT is popular but have not found a reason to use it with the Photon just yet.

@RWB data transfer should be similar to TCP since MQTT runs on TCP/IP, but lower than HTTP. But the main reason to use MQTT is for applications that need to send data from the cloud to the device.

Imagine a cloud-controlled device to open/close a door remotely. In the case of HTTP, the device would have to continuously make GET requests to the server to see if there’s a change in a variable, and then take an action depending on the last reading. This takes a lot of requests and it’s not entirely a real-time interaction since it depends of the polling frequency. With MQTT, the device subscribes to the variable and only gets notified when there’s a change in the variable. This way, the connection between the device and the cloud is left open but data only travels when necessary, saving battery, network bandwidth and improving the real-time experience.

1 Like

Thanks @aguspg for the info, it does make perfect sense.

I have hear a lot of MQTT but now it looks like I have a reason to take a short course on it get a better understanding of how most people are using it.

Sounds like its much better for the Electron devices also.

1 Like

Hi everybody. One question why after being subcrition or publish a value . something happens that is disconnected from the server MQTT and stops transmitting only seen in serial zeros, after it reconnects

Why a can I Do ? this is my code

#include "MQTT.h"
#include "UbidotsMQTT.h"
#include "ESP8266WiFi.h"
#define WIFISSID "xxx"
#define PASSWORD "xxxxx"
#define TOKEN "xxxx"  // Ubidots TOKEN
#define DATA_SOURCE_IDENTIFIER "xxx" //data source ID, ir a Ubidots/source lado izquierdo esta el label del source
#define VARIABLE_IDENTIFIER "RELAY_SALA" // Add a variable identifier, it must be in lowercase

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() {
    Serial.begin(115200);
    delay(10);
    Serial.println("Connecting WiFi");
    client.wifiConnection(WIFISSID,PASSWORD);
    Serial.println("Connected");
    pinMode(D0,OUTPUT);
    
    while (client.connect()) {

//    client.setDataSourceLabel(DATA_SOURCE_IDENTIFIER);//PUBLICAR
    }
}

void loop() {

     float value= client.getValueSubscribe(DATA_SOURCE_IDENTIFIER, VARIABLE_IDENTIFIER);//SUSCRIBIR
      Serial.println(value);
      if (p.atoi()==1.00){
        digitalWrite(D0,HIGH);
        }
      else{
        digitalWrite(D0,LOW);
      }
 
    client.loop();
}

What does that String message(p); do? It seem superfluous.

Does this code even build?
You are using p inside loop() that’s not declared in loop() or globally.