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

To integrate your Particle device's data into your Powered by Ubidots App, a webhook will be used to bridge the Particle cloud with Ubidots.

Webhooks allow a device to generate an event based on a condition or variable values. For example, you can connect a motion sensor to a device and have the recorded data trigger an event whenever motion is detected. Or as Particle says: "Webhooks bridge the gap between the physical and the digital world, helping you get your data where you need it to be".

In this tutorial we will explain how to interact with Ubidots REST API using Particle's Cloud and webhooks. For additional details about connecting your Particle devices to Ubidots, be sure to check out Connect a Particle Device to Ubidots.

Requirements

Table of Contents

  1. Create a Particle Webhook to Ubidots
  2. Create a New App and Add Ubidots Library
  3. Webhook Sample code1: Send Variable to Ubidots
  4. Webhook Sample code2: Send Multiple Variables to Ubidots
  5. Webhook Sample code3: Send Position Variable containing LAT and LNG Geocoordinates
  6. Create a Ubidots Webhook to Particle to Control Things
  7. Troubleshooting & Pro Tips
    – Accessing Particle Logs
    – How to locate your Particle TOKEN

Additional materials and details from the Particle Team 

1. Create a Particle Webhook to Ubidots


If you are not familiar with Particle and their Webhook feature - you can quickly learn more about this cloud-to-cloud communication method here

To begin your Particle - Ubidots connection, first you will need to setup your Particle device (Photon, Core, or Electron) within your particle account. Click here for simple steps to setting up your device with Particle's device platform. 

After claiming your devices to the Particle cloud continue with the following steps:

Step 1: Go to your Particle account, login and create a "New Integration"
Step 2:
Go to your particle console and choose Integrations
Step 3:
Select "New Integration"
Step 4:
Select "Webhook"
Step 5: Name
the new Webhook
Step 6: Add the sample URL below

Webhook URL:

https://industrial.api.ubidots.com/api/v1.6/devices/{{{PARTICLE_DEVICE_ID}}}

IMPORTANT NOTE: We're going to automatically call and assign the Device ID of the Particle Device to ensure the device label in Ubidots remains unique. To do this we're going to use the pre-defined key available from Particle:{{{PARTICLE_DEVICE_ID}}} This call will automatically assign to the URL the ID of the device which triggered the webhook.

Step 7:
Select "POST" Method
Step 8:
Select "Custom Body" Request Format
Step 9: Make the Webhook
available to ALL the devices from your account by choosing "Any"

Step 10: Select ADVANCED SETTING

  • Complete the text editor with as below:
{{{PARTICLE_EVENT_VALUE}}}
  • In the HTTP Headers options of the settings, complete as seen below:
X-Auth-Token | YOUR_UBIDOTS_TOKEN_HERE
Content-Type | application/json

To find your Ubidots TOKEN refer here.

Step 11: CREATE WEBHOOK and verify data is being streamed to Ubidots.

2. Create a New App and Add Ubidots Library

After setting up the webhook on you Particle account, 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.

Step 1: Create a New APP in Particle IDE
Step 2: Assign an App Name
Step 3: Add
Ubidots library to "Myapp" project:

  • Go to the Libraries option on the right side panel.
  • Search "Ubidots".
  • Select Ubidots Library.NOTE: Be aware not to choose UbidotsMQTT Library.
  • Click on "INCLUDE IN PROJECT".
  • Select the project to include the library in. In this case "Myapp".
  • Click on "CONFIRM".

IMPORTANT NOTE: in the below sections you will need to follow these steps again to create a New App and add the Ubidots library to it.

3. Webhook Sample code1: Send Variable to Ubidots

Now with the Webhook configured, New App created and Ubidots library added from the available libraries, it is time to code! :)

Step 1: Copy and Paste the code below
Step 2: Verify the Code
Step 3: Flash the Code

// This example sends data to a variable to 
// Ubidots using Particle Webhooks.

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

#include "Ubidots.h"

/****************************************
 * Define Instances and Constants
 ****************************************/

const char* WEBHOOK_NAME = "Ubidots";

Ubidots ubidots("webhook", UBI_PARTICLE);


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

//Put here your auxiliar functions


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

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

void loop() {
  float value1 = analogRead(A0);
  ubidots.add("Variable_Name_One", value1);  // Change for your variable name

  bool bufferSent = false;
  bufferSent = ubidots.send(WEBHOOK_NAME, PUBLIC);  // Will use particle webhooks to send data

  if(bufferSent){
    // Do something if values were sent properly
    Serial.println("Values sent by the device");
  }

  delay(5000);
}

4. Webhook Sample code: Send Multiple Variables to Ubidots

Now with the Webhook configured, New App created and Ubidots Library added in the IDE, it is time to code! :)
IMPORTANT NOTE:
Don't forget to follow steps in Section 2.

Step 1: Copy and Paste the code below
Step 2: Verify the Code
Step 3: Flash the Code

// This example sends data to multiple variables to 
// Ubidots using Particle Webhooks.

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

#include "Ubidots.h"

/****************************************
 * Define Instances and Constants
 ****************************************/

const char* WEBHOOK_NAME = "Ubidots";

Ubidots ubidots("webhook", UBI_PARTICLE);


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

  bool bufferSent = false;
  bufferSent = ubidots.send(WEBHOOK_NAME, PUBLIC);  // Will use particle webhooks to send data

  if(bufferSent){
    // Do something if values were sent properly
    Serial.println("Values sent by the device");
  }

  delay(5000);
}

5. Webhook Sample code: Send Position Variable containing LAT and LNG Geocoordinates

Now with the Webhook configured, New App created and Ubidots Library added, it is time to code! :)
IMPORTANT NOTE: Don't forget to follow steps in Section 2.

Step 1: Copy and Paste the code below
Step 2: Verify the Code
Step 3: Flash the Code

// This example sends data to a variable along with hardcoded 
// GPS coordinates to Ubidots using Particle Webhooks.

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

#include "Ubidots.h"

/****************************************
 * Define Instances and Constants
 ****************************************/

const char* WEBHOOK_NAME = "Ubidots";

Ubidots ubidots("webhook", UBI_PARTICLE);


/****************************************
 * 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 value = analogRead(A0);

  /* Harcoded Coordinates */
  float latitude = 37.773;
  float longitude = -6.2345;

  /* Reserves memory to store context keys values, add as much as needed */
  char* str_lat = (char *) malloc(sizeof(char) * 10);
  char* str_lng = (char *) malloc(sizeof(char) * 10);

  /* Saves as char the coordinates */
  sprintf(str_lat, "%f", latitude);
  sprintf(str_lng, "%f", longitude);

  /* Reserves memory to store context array */
  char* context = (char *) malloc(sizeof(char) * 50);

  /* Adds context key-value pairs */
  ubidots.addContext("lat", str_lat);
  ubidots.addContext("lng", str_lng);

  /* Builds the context with the coordinates to send to Ubidots */
  ubidots.getContext(context);

  /* Sends the position */
  ubidots.add("position", value, context); // Change for your variable name

  bool bufferSent = false;
  bufferSent = ubidots.send(WEBHOOK_NAME, PUBLIC);  // Will use particle webhooks to send data

  if(bufferSent){
    // Do something if values were sent properly
    Serial.println("Values sent by the device");
  }

  /* frees memory */
  free(str_lat);
  free(str_lng);
  free(context);

  delay(5000);
}

5. Create Ubidots Webhook to Particle to Control Things

The sample code provided in this article utilize the LED of PIN D7. By utilizing this sample code the LED will turn on and off utilizing a Control Widget in an Ubidots Dashboard. 

Step 1: Create a New APP in Particle IDE
Step 2: Assign an App Name
Step 3: Copy and Paste the code below
Step 4: Verify the Code
Step 5: Flash the Code

int led = D7;
float control = 0;
int i = 0;

void setup() {

    Serial.begin(115200);
    // Subscribe to the integration response event
    Particle.subscribe("UbidotsWebhook", myHandler);
    pinMode(led, OUTPUT);
}

float setControl(float value) {
    if (value == 1) {
        return 1;
    }
    return 0;
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
  i++;
  Serial.println(i);
  Serial.print(event);
  Serial.print(", data: ");
  if (data) {
    Serial.println(data);
    control = setControl(atof(data));
  } else {
    Serial.println("NULL");
  }
}

void loop() {    
    if (control == 1) {
        digitalWrite(led, HIGH);
    } else {
        digitalWrite(led, LOW);
    }
}


Step 6: Return
to your Ubidots Account
Step 7: Create a Raw Variable called "Control" directly from the Ubidots user interface. 

Step 4: Create a new "Event" (see GIF below for step-by-step)
Step 5: Configure the "Control" Variable of the Particle device to equal 1 for 0 seconds
Step 6: Select the Action Webhook
Step 7: Configure the Webhook to Particle with the following Parameters:
HTTP Method: POST
URL: https://api.particle.io/v1/devices/events
Headers:
Content-Type: application/x-www-form-urlencoded
Body:
name=UbidotsWebhook&data={{last_value}}&access_token=Particle_Token
IMPORTANTE NOTE: Don't forget to change your Particle TOKEN in the Webhook's body. See troubleshooting below for how to locate your Particle TOKEN

**Don't forget to also setup the 'Back to Normal' for simple On/Off functionality**

Step 8: Schedule and Save the Event

Step 9: Create a Control Widget on your Ubidots Dashboard linking to the Control Variable of the Particle device - see GIF below/learn more 

Step 10: Test and confirm the D7 LED is active/inactive
With the final Step 10 you will now have a switch displaying in the Ubidots Dashboard that is controlling a Particle device. To confirm the control of your device, change the state of the switch button and verify the data published can be found in the Log console of the Particle IDE.

6. Troubleshooting & Pro Tips

  • Accessing Particle Logs

To confirm data is properly being sent from Particle to Ubidots, simply check the Particle Webhook Logs. Locate the webhook and scroll to the bottom of the page to find the Logs. Select a particular event to find the response codes.

If all steps have been completed correctly – you will receive the 201 confirmation response from Ubidots in the Log section from the Webhook logs –> {"temperature": [{"status_code": 201}]} .

  • Locating your Particle TOKEN
    From the Settings tab of your Particle Build IDE and find the Access TOKEN.

Customize Visualization with Ubidots

Now that you are able to publish and subscribe data to/from Ubidots with your Particle device, it is time to create your customize Ubidots Dashboards to visualize, analyze, and engage with your data. 

Other users also found helpful...

  1. Creating Control Widgets
  2. Creating Global Events
  3. Devices Types: bulk device creation using a CSV file
Did this answer your question?