The RedBear Duo is a tiny Wi-Fi and BLE hardware development kit that comes with Particle’s firmware by default. The allows us to use Particle’s development environment and enjoy its ease of use, community examples and available libraries.

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

Requirements

Step-by-Step

  1. Device Setup
  2. Claim your RedBear Duo in Particle IDE
  3. Create a new APP and include Ubidots Library 
  4. SUBSCRIBE to a Variable
  5. PUBLISH Values to a Device
  6. Summary  

1. Device Setup

1. Connect the RedBear Duo to a computer via USB port using an USB cable. 

2. By default the RedBear Duo brings "Setup Firmware" which let you check the RedBear Board version, Device ID of the board. Also, you will be able to setup the WiFi credentials.  

Based on your OS, please refer to the following steps:

  • Windows Users

1. Download and install the Silicon Labs CP2102 driver for Windows.
2. Go to Computer Management and look for Silicon Labs CP210x USB to UART Bridge under Ports (COM & LPT), and take note of the COM number in brackets.
3. Open PuTTY, select Serial for the connection, enter the COM port of Silicon and enter 115200 for the speed; then click on the Open button to complete this step.

NOTE: If you don't like Putty, feel free to choose other communications tools supported by Windows, such as HyperTerminal vs TeraTerm.

  • MacOSX Users

1. Install "screen" by following the instructions provided in this guide.
2. With screen properly installed, run the command below in the computers terminal. 

NOTE: The xxx  located at the end of the command refers to the Serial Port assigned to the Red. Based on this, you may replace the xxx with the port assigned to the board. 

screen /dev/ttyxxx
  • Linux Users

1. Install "screen" by following the instructions provided in this guide.
2. With screen properly installed, run the command below in the computers terminal. 

NOTE: The xxx  located at the end of the command refers to the Serial Port assigned to the Red. Based on this, you may replace the xxx with the port assigned to the board. 

screen /dev/ttyxxx

3. Now, write the following commands on the terminal to setup the board properly: 

  • [OPTIONAL] Type "v" to check the RedBear version. 
  • Type "i"  to print the Device ID; the Device ID is the one needed to claim the device in the Particle IDE, so please keep this in hand as it'll be used later.
  • Type "w" to set the WiFi credentials. At this point, the board will ask for the SSID (network name), security type and password, so please assign the credentials of the network you will be connecting to. 

NOTE: To be able to setup the WiFi credentials in the board, this should be done while the board is in "Listening Mode". To set the board to listening mode, press and hold the "SETUP" button for at least 3 seconds.

If the configuration of your WiFi network is entered properly the RedBear Duo will leave Listening Mode and try to connect to the Internet access point. The RGB LED should flash green if the connection is establish properly with the internet. If the Duo connects to the access point successfully, it will perform a soft reset and restart. Otherwise, the Duo will enter Listening Mode again and await you to re-enter the Wi-Fi credentials.

If you are using PuTTY or other serial terminal, when the Duo performs a reset, it will disconnect from the terminal, possibly then disconnecting from the correct serial port. If this happens you need to close the serial port, then press the board's RESET button, then re-open the serial port again to restore the communication with Duo.

2. Claim your RedBear Duo in Particle IDE

 1. Go to Particle IDE

2. Click on Devices –> ADD NEW DEVICE.

3. Assign the Device ID that you saved in the previous step (Device Setup). Then click on CLAIM A DEVICE.

Now, the RedBear Duo is added to the Particle IDE.

3. Create a new APP and include Ubidots Library

1. After claiming your RedBear Duo to the Particle IDE and setting up your Ubidots account, let’s go to Particle’s Web IDE and "Create New App."

For additional details in how to "Create New App," simply click here and follow the simple 3 step process. 

2. With the new app created, you must now include the Ubidots Library to the app created in order to be able to work with your RedBear devices and Ubidots.

To include the library, go to the library tab and find the Ubidots library set, specifically the UBIDOTSMQTT library. Make sure to add the last version library

Then, click on INCLUDE IN APP. And return to app previously created.

This library creates by default new Device using the RedBear Device ID of your RedBear hardware. The name of this device will be "Particle" by default, and the label will be your RedBear Device ID.

4. SUBSCRIBE to a Variable

To subscribe to a variable, you need to specify your 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 that it is subscribed to.

/****************************************
 * 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
 ****************************************/

Ubidots client(TOKEN, callback);

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

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

    // Uncomment this line if you have an Ubidots account
    //client.ubidotsSetBroker("industrial.api.ubidots.com");

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

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

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

5. PUBLISH Values to a Device

To publish values to Ubidots, you have to call first the add() function 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
 ****************************************/

Ubidots client(TOKEN, callback);

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

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

    // Uncomment this line if you have an Ubidots account
    //client.ubidotsSetBroker("industrial.api.ubidots.com");
}

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

    // 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("test-device"); // 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);
}

6. Summary

With this simple tutorial you are able to PUBLISH & SUBSCRIBE data to/from Ubidots with the ease of the Particle IDE and RedBear Duo board.

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  Happy Developing! :) 

Other readers have also found useful...

Did this answer your question?