Requirements

Setting up your Particle device

If you are not familiar with Particle and their Webhook feature - you can read up on this connection 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 Ubidots API. To get a better idea how the Ubidots API  works, I recommend you read the REST API Reference.

The easiest way to send values to Ubidots is specifying the Device label in the URL, as you can see on the link below: 

https://things.ubidots.com/api/v1.6/devices/{LABEL_DEVICE}/

In this case, we're going to assign the Device ID of the Particle Device to make it unique. For this we're going to use the pre-defined webhook variable available to use:

  • {{PARTICLE_DEVICE_ID}}: The ID of the device that triggered the webhook. It will be assign to the URL the ID of the device selected automatically. 

Also, do not forget assign your Ubidots TOKEN to the end the URL because this is the key to publishing events to your Ubidots account .

URL: 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:

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

NOTE: Do not forget select the Device, if you don't select it will not create the device in Ubidots.

  • Select Advaced Settings > Form.

Body (form option): {“temperature”: {{PARTICLE_EVENT_VALUE}}

On the option "INCLUDE DEFAULT DATA" press No, to erased the JSON data that Particle sends by default:

When you finish entering your unique info - complete with 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.

Next select the Code icon from the menu.
Then CREATE NEW APP and assign a name of your preference to your new app.

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.

Particle Webhook to Ubidots

Particle lets you manage events with different functions, in this case 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(). 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."

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.

So, now let's try the example code with you previously designed webhook. Copy the code below and paste it into the Particle IDE. 

NOTE: Before you paste your code into the Particle IDE, be sure to erase the previous code (initial codes) and you are working with 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 well - you will receive the response from Ubidots on the Log section from the Console -> {"temperature": [{"status_code": 201}]} .

Next - go to your Ubidots account to verify the data received. (For Business or Industrial Users, login here

In your main tabs select the Device tab.

You should now see your new device created with the Device ID of your Particle Device.

If you desire you can modify the name of the device with a friendly one, like the one below, please reference the article below: 

Select the device created to visualize the data received on 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 to the Particle ID.

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 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,
Particle.publish()
and Particle.subscribe()

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 device using Ubidots. 

Into your Ubidots account (business and industrial users sign in here), go to the Device tab and select the device already created to create the variable that you will control. Select Add Variable > Default 

Assign the name "control" to the variable. Once the variable is created you should see something like this:

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

Select the device created and the variable called control.

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 less or equal to 0, and two) other if control value is equal to 1.

Once you set the action, press Continue and select Webhook.

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

URL: https://api.particle.io/v1/devices/events
Method: POST. Once you select the method will appear the payload form.
Payload: name=UbidotsWebhook&data={{last_value}}&access_token={Particle_Token}
Headers: content-type / application/x-www-form-urlencoded

Once all is filled, you will see this:

Press Finish, and repeat the same steps to create other 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 created a new control widget. Select Control > Switch > My Particle Device (device) > control (variable). Then, press finish.

The newly displayed switch is now ready to send data through the Ubidots Webhook to Particle. To confirm the pathway, change the state of the switch button and verify the data published on Log console from the Particle IDE.

Now, that you are able to publish data from the Ubidots Webhook to Particle, it is time to subscribe it to the event created to be able to control what ever you want with your Particle Device.

In the following code we're going to subscribe it to the event just setting on the Ubidots Webhook called "UbidotsWebhook".

void setup() {
  Serial.begin(115200);
  // Subscribe to the integration response event
  Particle.subscribe("UbidotsWebhook", myHandler);
}

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() {
   
}

As you can see on the code we made a sample routine to handle the integration with the device, to be able to visualize if the device is receiving the right response, we look to the serial port of the device. If all is correct, you will receive the responses below on IDE's serial port:


NOTE: If you desire read more about Particle Webhook works and Particle Cloud, please reference to the links below:

Also, don't forget get familiar with the Ubidots REST API reference.

Happy Hacking :)

Did this answer your question?