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 pre-configured libraries.

By following this guide you will be able to POST and GET 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. Send (POST) Values to Ubidots
  5. Send (POST) Values to Ubidots with Context
  6. Send (POST) Values with Timestamp
  7. Receive (GET) Values from Ubidots
  8. Receive (GET) Values from Ubidots with Context
  9. Special functionality
    - Obtain timestamp
    - Change Device Label
    - Change Device Name
    - Use TCP method to Send (POST) Values
  10. 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's 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, this will be used later in our setup.
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 to gain access to the RedBear terminal. 

NOTE: The xxx  located at the end of the command refers to the Serial Port assigned to the RedBear board. Based on this, you are to 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 to gain access to the RedBear terminal. . 

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

screen /dev/ttyxxx


3. Now, write the following commands in the RedBear 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 it in hand for 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 to the board, this should take place in "Listening Mode". To set the board to listening mode keep 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's 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 in the Particle IDE and set 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 next need to add the Ubidots Library to the app previously created to be able to work with your RedBear hardware and Ubidots.

To include the library, go to the library tab (noted in the image below) and find the Ubidots library set, specifically the UBIDOTSMQTT library. Make sure to add the last version library

Note that there is also an MQTT version of Ubidots communication with RedBear. For this tutorial, we will be using the TCP/UDP "Ubidots" library. If you prefer to utilize MQTT protocols please reference this guide for instructions on how to do so.

Click on INCLUDE IN APP. And return to the app previously created. With the library added to the App we can now begin coding in the Particle IDE.

4. Send (POST) Values to Ubidots

You can send values to Ubidots using the variable label, also you can send values with timestamp and context.

Sending values using variable label

Using the following sample code you, Ubidots will automatically create a new variable with the variable label assigned in the code.

IMPORTANT NOTE: The Ubidots library allows you send 5 values maximum. If you need to POST more values, simply add the line ubidots.add("variable_name", value); to your code with the parameters needed.

Also, you can find the below example code on the Particle IDE. Go to Included Libraries search for and click on UBIDOTS and select UbidotsSendValues.cpp, then copy and paste it into the Particle IDE.

Do not forget add your Ubidots TOKEN where indicated.

// This example is to get the last value of variable from the Ubidots API

// This example is to save multiple variables to the Ubidots API with TCP method

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

#include "Ubidots.h"


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

#define TOKEN "Your_Token"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);


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

//Put here your auxiliar functions


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

void setup() {
    Serial.begin(115200);
    //ubidots.setDebug(true); //Uncomment this line for printing debug messages
}

void loop() {
    float value1 = analogRead(A0);
    float value2 = analogRead(A1);
    float value3 = analogRead(A2);
    ubidots.add("Variable_Name_One", value1);  // Change for your variable name
    ubidots.add("Variable_Name_Two", value2);
    ubidots.add("Variable_Name_Three", value3);
    ubidots.sendAll();
    delay(5000);
}

5. Send (POST) Values to Ubidots with Context

The following example is to send one value with context to Ubidots, it will create the variable automatically with the label assign by you on the code.

Also, you can find the below sample code in the Particle IDE. Go to Included Libraries to search for and click on UBIDOTS. Then select UbidotsSendValuesWithContext.cpp, copy and paste the sample code into the Particle IDE of your new App.

Do not forget add your Ubidots TOKEN where indicated.

// This example is to save multiple variables with context to the Ubidots API with TCP method


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

#include "Ubidots.h"


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

#define TOKEN "Your_Token"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);


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

//Put here your auxiliar functions


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

void setup() {
    Serial.begin(115200);
    //ubidots.setDebug(true); //Uncomment this line for printing debug messages
}
void loop() {
    float value1 = analogRead(A0);
    char context[25];
    sprintf(context, "lat=1.2343$lng=132.1233"); //Sends latitude and longitude for watching position in a map
    ubidots.add("Variable_Name_One", value1, context);  // Change for your variable name
    ubidots.sendAll();
    delay(5000);
}

6. Send (POST) Values with Timestamp

The following example is to send one value with timestamp to Ubidots, it will create the variable automatically with the label assign by you on the code.

Also, you can find the sample code below in the Particle IDE. Go to Included Libraries, then search for and click on UBIDOTS. Then select UbidotsSendValuesWithTimeStamp.cpp, copy and pasting it into the Particle IDE of your new app.

Do not forget add your Ubidots TOKEN where indicated.

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

#include "Ubidots.h"


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

#define TOKEN "Your Ubidots TOKEN"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);


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

//

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


void setup() {
    Serial.begin(115200);
    //ubidots.setDebug(true); //Uncomment this line for printing debug messages
}


void loop() {
    float value1 = analogRead(A0);
    unsigned long t = ubidots.ntpUnixTime(); // calculates your actual timestamp in SECONDS

    ubidots.add("test-1", value1);  // Change the first argumento for your var's label
    ubidots.add("test-2", value1, NULL, t-20000);  // Sends a value with a custom timestamp
    ubidots.add("test-3", value1);

    // Sends variables 'test-1' and 'test-2' with your actual timestamp,
    // variable 'test-2' will be send with its custom timestamp
    ubidots.sendAll(t);
    delay(5000);
}

7. Receive (GET) Values from Ubidots

This library let you GET the last value of a variable from Ubidots using the variable label or variable ID, also you can get the context value if needed.

  • To GET the last value using the variable ID you should use the following line:
getValue(VAR_ID)
  • To get the last value using the variable label you should use the following line:
getValueWithDatasource(DEVICE_LABEL, VAR_LABEL)

 
The following example shows how to GET last value of a variable in your Ubidots account.

Do not forget add your Ubidots TOKEN, the device and variable label. or variable ID where indicated in the sample code.

// This example is to GET the last value of variable from the Ubidots API

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

#include "Ubidots.h"

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

#define TOKEN "...."  // Put here your Ubidots TOKEN
#define VAR_ID "58d9153e762542576b721820"  // Put here your data source name
#define VAR_LABEL "...." // Put here your variable api label
#define DEVICE_LABEL "...." // Put here your device api label

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

//Put here your auxiliar functions


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


Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    //ubidots.setDebug(true); //Uncomment this line for printing debug messages
}
void loop() {
    float value1;
    value1 = ubidots.getValue(VAR_ID);
    float value2;
    value2 = ubidots.getValueWithDatasource(DEVICE_LABEL, VAR_LABEL);
    if(value1!=NULL){
      Serial.println(value1);
    }
    if(value2!=NULL){
      Serial.println(value2);
    }
    delay(5000);
}

8. Receive (GET) Values from Ubidots with Context

getVarContext(VARIABLE_ID)

The following example is to GET the context of a variable from Ubidots.

To get the context from a variable stored in Ubidots, go to Included Libraries and click on UBIDOTS and select UbidotsGetContext.cpp and copy and paste it into the Particle IDE of your new app.

Do not forget add your Ubidots TOKEN and variable ID where indicated in the sample code.

// This example is to get the last value of variable from the Ubidots API

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

#include "Ubidots.h"

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

#define TOKEN "...."  // Put here your Ubidots TOKEN
#define VAR_ID "58d9153e762542576b721820"  // Put here your data source name

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

//Put here your auxiliar functions


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


Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    //ubidots.setDebug(true); //Uncomment this line for printing debug messages
}
void loop() {
    char* context;
    ubidots.getVarContext(VAR_ID);
    Serial.println(context);
    delay(5000);
}

9. Special functionality

Obtain timestamp

If you need to obtain the timestamp you can use the NTP server made by Francesco Potorti ported to this library, simply add to your code the following routine:

Get timestamp

unsigned long timestamp = ubidots.ntpUnixTime(); // calculates your actual timestamp in SECONDS

ubidots.add("test-1", 1);
ubidots.sendAll(timestamp);

Change Device Label

Set Device Label Function

ubidots.setDeviceLabel(DeviceLabel);

Example using setDeviceLabel Function

// This example is to save values with a setted data source name

#include "Ubidots/Ubidots.h"


#define TOKEN "Your_Token_Here"  // Put here your Ubidots TOKEN
#define DEVICE_LABEL "Your_DEVICE_LABEL"

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    ubidots.setDeviceLabel(DEVICE_LABEL);
}
void loop() {
    float value1 = analogRead(A0);
    float value2 = analogRead(A1);
    float value3 = analogRead(A2);
    ubidots.add("Variable_Name_One", value1);  // Change for your variable name
    ubidots.add("Variable_Name_Two", value2);
    ubidots.add("Variable_Name_Three", value3);
    ubidots.sendAll();
    delay(5000);
}

Change Device Name

Set Device Name Function

ubidots.setDeviceName(DEVICE_NAME);

Example using setDeviceName Function

// This example is to save values with a setted data source name

#include "Ubidots/Ubidots.h"


#define TOKEN "Your_Token_Here"  // Put here your Ubidots TOKEN
#define DEVICE_NAME "Your_DEVICE_NAME"

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    ubidots.setDeviceName(DEVICE_NAME);
}
void loop() {
    float value1 = analogRead(A0);
    float value2 = analogRead(A1);
    float value3 = analogRead(A2);
    ubidots.add("Variable_Name_One", value1);  // Change for your variable name
    ubidots.add("Variable_Name_Two", value2);
    ubidots.add("Variable_Name_Three", value3);
    ubidots.sendAll();
    delay(5000);
}

Ubidots Particle library has a special function to set a Device name. This Device name is the digital twin or the digital device in Ubidots that matches the physical hardware in the real world.

With your Particle device you can't change the name of created digital device. To change the name of your device, simply alter them directly in the Ubidots front-end

Use TCP method to Send (POST) Values

Set transmission function

ubidots.setMethod(TYPE_UDP);

Example using setMethod Function

// This example allow send data using UDP protocol

#include "Ubidots/Ubidots.h"


#define TOKEN "Your_Token_Here"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    /*
    TYPE_TCP is used to send data using TCP method
    if you don't call this function, the library automatically
    set as UDP
    */
    ubidots.setMethod(TYPE_TCP);
}
void loop() {
    float value1 = analogRead(A0);
    float value2 = analogRead(A1);
    float value3 = analogRead(A2);
    ubidots.add("Variable_Name_One", value1);  // Change for your variable name
    ubidots.add("Variable_Name_Two", value2);
    ubidots.add("Variable_Name_Three", value3);
    ubidots.sendAll();
    delay(5000);
}

Ubidots libraries available in the Particle platform also have the function to set transmission method of data. With this function you can change from UDP method to a TCP method.

10. Summary 

With this simple tutorial you are able to POST & GET 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?