Skip to main content
All CollectionsConnect your Devices
Connect a Particle Device to Ubidots over MQTT
Connect a Particle Device to Ubidots over MQTT

Learn to setup a Particle Device to SUBSCRIBE/PUBLISH using Ubidots Library in the Particle IDE over MQTT.

Sergio M avatar
Written by Sergio M
Updated over 2 months ago

Particle devices are compact and easy-to-code hardware development kits that provide everything you need to build a connected project. They combine an ARM microcontroller, a communication chip (WiFi, GPRS, or 3G), and a web IDE with tons of community examples and libraries.

By following this guide you will be able to PUBLISH and SUBSCRIBE data to/from Ubidots using any Particle Device module in just a couple of minutes!    

Requirements

  • Micro USB cable

  • Particle SIM card with data plan (if using a Particle Electron)

  • An Ubidots account

Step-by-Step

  1. Device Setup

  2. SUBSCRIBE to a Variable

  3. PUBLISH Values to a Device

1. Device Setup

1.- Setting up your Particle device:

If you are using a Particle Electron, please follow these steps to set up the Particle data plan. Particle Electron steps.

If you are using a Particle Core you have two ways to set it up:

If you are using a Particle Photon you have two ways to set it up:

2.- After claiming your Particle Device and setting up your Ubidots account, let’s go to Particle’s Web IDE.

In the Particle’s Web IDE create a New App and set the name. For additional details to creating a New App, head over to this article and return once complete.

Go to the library tab.

In contributed library write Ubidots and select the UBIDOTSMQTT library. Make sure to add the last version library (1.1.3)

Click on INCLUDE IN APP. And return to “MYAPP.ino

This library creates by default a new Device. The name of this device will be "Particle" by default, and his label will be your Particle Core ID.

2. SUBSCRIBE to a Variable

To subscribe to a variable, first, define your Ubidot's account token, then specify the device and variable labels as input arguments for the ubidotsSubscribe()  function. The incoming value will be returned by the MQTT library in the payload variable, by default the subscribed function only gets the last value of the variable subscribed. Retain value feature is only available for core platform users.

/****************************************
 * Include Libraries
 ****************************************/

#include "UbidotsMQTT.h"

/****************************************
 * Define Constants
 ****************************************/

#ifndef TOKEN
#define TOKEN "Your TOKEN"  // Add here your Ubidots TOKEN
#endif


/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    Serial.println("payload obtained from server:");
    for (int i=0;i<length;i++) {
        Serial.print((char)payload[i]); // prints the answer of the broker for debug purpose
    }
    // Some stuff to make with the payload obtained
        //
   //
    Serial.println();
}

/****************************************
 * Instances
 ****************************************/

UbidotsMQTT client(TOKEN, callback);

/****************************************
 * Main Functions
 ****************************************/

void setup() {
    Serial.begin(115200);

    // Uncomment this line to set up the Ubidots Broker
    //client.ubidotsSetBroker("industrial.api.ubidots.com");

    client.connect();

    if(client.isConnected()){
        // Insert as first parameter the device label to subscribe and as second the variable label
        client.ubidotsSubscribe("device-label", "variable-label");
    }
}

void loop() {
    if(!client.isConnected()){
        client.connect();
    }

    // Client loop for publishing and to maintain the connection
    client.loop();
    delay(1000);
}

PUBLISH Values to a Device

To publish values to Ubidots, you have to call first the add()  function first for storing the values to send (max of 5 values per request), so insert as parameters for the add() function the variable label, the context and the timestamp (these last two are optionals):

/****************************************
 * Include Libraries
 ****************************************/

#include "UbidotsMQTT.h"

/****************************************
 * Define Constants
 ****************************************/

#ifndef TOKEN
#define TOKEN "Your TOKEN"  // Add here your Ubidots TOKEN
#endif


/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    Serial.println("payload obtained from server:");
    for (int i=0;i<length;i++) {
        Serial.print((char)payload[i]); // prints the answer of the broker for debug purpose
    }
    // Some stuff to make with the payload obtained
        //
   //
    Serial.println();
}


/****************************************
 * Instances
 ****************************************/

UbidotsMQTT client(TOKEN, callback);

/****************************************
 * Main Functions
 ****************************************/

void setup() {
    Serial.begin(115200);

    // Uncomment this line to set up the Ubidots Broker
    //client.ubidotsSetBroker("industrial.api.ubidots.com");

    client.connect();
}

void loop() {
    if(!client.isConnected()){
        client.connect();
    }

    // Publish routine, if the device and variables are not created they will be created
    float value = 1;
    Serial.println("Sending value");
    client.add("test-var-1", value); // Insert as first parameter your variable label
    client.add("test-var-2", value, "\"lat\":10.302, \"lng\":2.9384"); //Adds value with context
    client.add("test-var-3", value, NULL, 1492445109); // Adds value with custom timestamp
    client.ubidotsPublish("device-label"); // Insert your device label where the values will be stored in Ubidots

    // Client loop for publishing and to maintain the connection
    client.loop();
    delay(1000);
}
Did this answer your question?