Particle devices are compact and easy-to-code hardware development kits that provide everything you need to build a connected solution. 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 Ubidots power App, a webhook will be used to bridge the Particle cloud with Ubidots cloud.

Webhooks allow a device to generate an event based on a condition or variables. For example, you can connect a motion sensor to a device and have the data of the device 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 to connecting your Particle devices to Ubidots, be sure to check out Connect a Particle Device to Ubidots.

Requirements

Step-by-Step

  1. Setting up your Particle Account
  2. Create a Particle Webhook to Ubidots
    - Particle.publish()
    - Particle.subscribe()
  3. Create a Ubidots Webhook to Particle
    - Building the Ubidots Webhook

Additional materials and details from the Particle Team 

1. Setting up your Particle Account

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 to claiming your devices to the Particle cloud follow the next steps:

  • Click on Console from the left menu. 
  • Click on Integrations button and press in + NEW INTEGRATION.
  • Choose Webhook and press on label Webhook Builder.
  • Next, you will need to create a webhook called “temperature” pointing to the Ubidots API

The easiest way to send values to Ubidots is specifying the Device label in the URL, followed by your Ubidots TOKE, as shown below: 

  • Ubidots Industrial users:
https://industrial.api.ubidots.com/api/v1.6/devices/{LABEL_DEVICE}/?token={Ubidots_TOKEN}
  • Ubidots for Education users:
https://things.ubidots.com/api/v1.6/devices/{LABEL_DEVICE}/?token={Ubidots_TOKEN}


With this structure, 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 webhook variable available from Particle:

  • {{{PARTICLE_DEVICE_ID}}}: This call will automatically assign to the URL the ID of the device which triggered the webhook.

Webhook URLs:

  • For Industrial users:
https://industrial.api.ubidots.com/api/v1.6/devices/{{{PARTICLE_DEVICE_ID}}}/?token={Ubidots_TOKEN}
  • For Educational users: 
https://things.ubidots.com/api/v1.6/devices/{{{PARTICLE_DEVICE_ID}}}/?token={Ubidots_TOKEN}

If you don't know how locate your Ubidots TOKEN, please reference this article:

How to locate your Ubidots TOKEN


Now that you have the parameters needed to build the URL, fill the text box.

IMPORTANT NOTE: Do not forget select the Device, if you don't select a device, Ubidots will not create the digital device in Ubidots that will handle and house your hardware's data.

  • Select Advanced Settings > Custom.

Complete the text box with:

temperature > {{{PARTICLE_EVENT_VALUE}}} 

When you finish entering your unique info - complete by CREATE WEBHOOK.

Before we move on, let's review the Integration Info to verify everything it's okay.

Now with the Webhook configured, it is time to code! :) 

First, go back to the Build console, in the bottom-left menu you will find the Build icon, press it, and then CREATE NEW APP, assign a name of your preference to your new app. 

For additional insights to Creating a New App, simply click here to learn more.

In this case, we called it "UBIDOTS-WEBHOOK"

Now we're going to provide you with the codes to pass data through the Particle Webhook to Ubidots and visa-versa.

2. Create Particle Webhook to Ubidots

Particle lets you manage events with different functions, in this tutorial we're going to use Particle.publish()  and Particle.subscribe(). Before we start coding, it is important know the difference between these functions:

  • Particle.publish()

Publish is an event through the Particle Cloud that will forward data to all registered listeners, such as callbacks, subscribed streams of Server-Sent Events, and other devices listening via Particle.subscribe(). 

Copy and Paste the sample code below into the project recently created. 

Pro Tip: Before you paste your code into the Particle IDE, be sure to erase the previous code (initial codes) and you begin working from a blank IDE.

void setup() {    

}

void loop() {
  // Get some data
  String data = String(random(60, 80));
  // Trigger the integration
  Particle.publish("temperature", data, PRIVATE);
  // Wait 10 seconds
  delay(10000);
}

Once you have pasted the code, you must Verify this code within the Particle IDE. In the top left corner of our Particle IDE you will see the below icons. Click the "Check Mark" icon to verify any code.

Once the code is verified, you will receive a "Code verified! Great work" message in the Particle IDE.

Next, your have to upload the code into your Particle Device. To do this, choose the flash icon up the check mark icon.  (Be sure you have your device connected to your computer)

Once the code is uploaded, you will receive a "Flash successful! Your device is being updated - Ready" message in the Particle IDE. 

If all instructions 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}]} .

To verify the data was received go to your Ubidots account.

Go to the Devices section from the Device Management dropdown.

Upon the first dot being received a new device will be created with the Device ID of your Particle Device.

Click here to modify the name of the Ubidots device to a more friendly one. 

Select the device created to visualize the data received in the variable called "temperature".

  • Particle.subscribe()

Subscribe to events published by devices. This function will allow devices to speak with each other very easily. For example, one device could publish events when a motion sensor is triggered and another could subscribe to these events and respond by sounding an alarm.

With the following example you'll be able to visualize the subscription message every time the event is published

Copy and paste the code below into to the Particle IDE.

void setup() {
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/temperature", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}
void loop() {
  // Get some data
  String data = String(random(60, 80));
  // Trigger the integration
  Particle.publish("temperature", data, PRIVATE);
  // Wait 10 seconds
  delay(10000);
}

Once you have pasted the code, you must Verify this code within the Particle IDE. In the top left corner of our Particle IDE you will see the below icons. Click the "Check Mark" icon to verify any code.

Once the code is verified, you will receive a "Code verified! Great work" message in the Particle IDE.

Next, your have to upload the code into your Particle Device. To do this, choose the flash icon up the check mark icon.  (Be sure you have your device connected to your computer)

Once the code is uploaded, you will receive a "Flash successful! Your device is being updated - Ready" message in the Particle IDE. 

Once the code is verified and compiled properly, go to the Log section from the Console to visualize the response. As you can see the on the Log section, the event subscription works every time the event is published to Ubidots.

Now you're able to publish a event to Ubidots, using Particle.publish() and Particle.subscribe()

3. Create Ubidots Webhook to Particle

Not always do you want to just publish data to Ubidots. Often being able to control devices with webhooks provides added functionality to your projects or applications. For this, we're going to show you how to build the Ubidots Webhook properly to be able to control your Particle devices using Ubidots platfrom. 

In the following code we're going to subscribe the particle device using a Particle Event called "UbidotsWebhook." Using the below sample code, a new Device in Ubidots will be automatically created the first time a dot is Published the subscribed to.  

To control your Particle device from Ubidots using a control widget, please copy then paste the below sample code into the Particle IDE.

#include "Ubidots.h"
#define TOKEN "YOUR_UBIDOTS_TOKEN_HERE"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    // Subscribe to the integration response event
    Particle.subscribe("UbidotsWebhook", myHandler);
    //ubidots.setDebug(true); //Uncomment this line for printing debug messages
   
    //Create the control variable automatically in Ubidots
    float control = 0;
    ubidots.add("Control", control);
    ubidots.sendAll();
    delay(5000);
}

int i = 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);
  else
    Serial.println("NULL");
}


void loop() {
 
}

Once you have pasted the code, you must Verify this code within the Particle IDE. In the top left corner of our Particle IDE you will see the below icons. Click the "Check Mark" icon to verify any code.

Once the code is verified, you will receive a "Code verified! Great work" message in the Particle IDE.

Next, your have to upload the code into your Particle Device. To do this, choose the flash icon up the check mark icon.  (Be sure you have your device connected to your computer)

Once the code is uploaded, you will receive a "Flash successful! Your device is being updated - Ready" message in the Particle IDE.

  • Building the Ubidots Webhook

Now, it is time to build the Ubidots Webhook. Select the Device Management > Event tab from the upper menu. Next, select + Add event.

In the following window assign the parameters below:

    First field:  Device > Particle Device > Control
    Second field: Value
    Third field: equal to
    Fourth field: 1
    Fifth field: 0  

We desire to be able to control a switch so we have to create two events with the same settings but with different actions, one, if control value is equal to 1, and two, if control value is equal to 0.

Once you set the condition, press the right blue arrow to continue, and then select the plus orange icon to create the action type. Select "Trigger Webhook"

You should see the form below, please complete the text box with the below information:

HTTP Method: POST v
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} 

Once all fields are complete, press the green "check-mark" button.

Now we're going to schedule the trigger window for the event. Click on the blue "arrow" button.

Select the trigger window for your desired use case. In our case, we selected the entire week for all parts of the each day. Then press the green "check-mark" to save and close the event.

Repeat the above steps to create the other webhook events as you see fit. Once the events are created, you will see something like this on your Events tab.

Now it is time to control the variable! 

Go to the Dashboard tab and create a new control widget. Select Control > Switch > My Particle Device (device) > control (variable). Then, press finish.

With this final step you will now have a switch displaying in your 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.

Now, that you are able to publish data from the Ubidots Webhook to Particle. We look to the serial port of the device. If all is correct, you will receive the responses below on IDE's serial port verifying console and Ubidots activity.

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. 

Did this answer your question?