Skip to main content
All CollectionsConnect your Devices
Connect your Particle device to Ubidots using Particle Webhooks
Connect your Particle device to Ubidots using Particle Webhooks

POST/GET data to/from Particle's Cloud with a webhook to/from Ubidots and enable your data to drive-decisions and solve problems.

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

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 Multiple Variables to Ubidots

  4. Webhook Sample code2: Send Position Variable containing LAT and LNG Geocoordinates

  5. Create a Ubidots Webhook to Particle to Control Things

  6. Troubleshooting & Pro Tips
    – Accessing Particle Logs

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 your Particle account, go to Particle’s Web IDE and "Create New App." For additional details on 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 code 1: Send Multiple 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); // 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);
}

4. Webhook Sample code 2: 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 10 bytes of 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 the coordinates as char*/
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 use the LED of PIN D7. By utilizing this sample code the LED will turn on and off using a Switch Widget.

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 switchBuiltinLed(String command);
int led = D7; // Built-in LED

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

int switchBuiltinLed(String command) {
// look for the matching argument "ON"
if (command == "ON") {
digitalWrite(led, HIGH);
return 1;
}
// look for the matching argument "OFF"
else if (command == "OFF") {
digitalWrite(led, LOW);
return 1;
}
return -1;
}

/****************************************
* Main Functions
****************************************/
void setup() {
Particle.function("Switch", switchBuiltinLed);
pinMode(led, OUTPUT);
}

void loop() {

}

NOTE: A function callback process needs to return as quickly as possible otherwise the cloud call will timeout.

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

Step 8: Create a new "Event" and configure the Event logic using Ubidots’ “ANDs” / “ORs” operators.
NOTE: For additional details on how to configure conditional and complex Event triggers, check out our Creating Conditional Events and Alerts user guide.


Step 5: Configure the "Control" Variable of the Particle device to equal 1 for 0 seconds

Step 9: Select the Particle Function
Step 10: Fill the fields related to particle:
Access Token: Please refer to this Ubidots' article to know how to create and get your Particle Token.
Particle Device ID: The particle Device ID that will be triggered. Normally, it’s best to enter the “Device label” bookmark
Particle Function Key: The Particle funcKey in the code. In the example herein we used “Switch”

Step 11: Fill the fields related to the messages:
Active trigger: message to send. It most begin with “args=”. In the case herein we used “args=ON”
Back to normal: message to send when condition isn’t true. Here we used “args=OFF”


NOTES:
– A cloud function is set up to take one argument of the String datatype. This argument length is limited to a max of 63 characters (prior to Particle’s firmware v0.8.0) or 622 characters (since Particle’s firmware v0.8.0). The String is UTF-8 encoded.
– Only use letters, numbers, underscores and dashes in function names. Spaces and special characters may be escaped by different tools and libraries causing unexpected results.

Step 12: Enable, or leave disable, the Repeat events. More info here.
Step 13: Continue to the end, Name the event and Set the active days and hours. Save it.

Step 14: Create a Switch Widget on your Ubidots Dashboard and link the Control Variable recently created in Step 7. learn more

Step 10: Test and confirm the D7 LED is active/inactive


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}]} .

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...

Did this answer your question?