In this guide, we’ll walk through the general process of setting up webhooks in The Things Network (TTN) and connecting your data to an external platform like Ubidots. This includes everything from creating the webhook, forwarding sensor data, decoding the payload, building dashboards, and setting up alerts or automated events based on the data.
While the steps here are applicable to a wide variety of IoT projects, we’ll use a specific example for clarity. Our payload is being generated by a device using a sendLoraFrame()
function, which constructs the message with multiple sensor readings, including temperature, pressure, humidity, gas resistance, and luminosity. The data is encoded into a byte buffer before transmission using the following pattern:
CopyEdit
uint8_t buffSize = 0;
uint16_t temp = (temperature * 100) + 5000;
uint16_t press = pressure / 100;
uint16_t hum = (humidity * 100) + 5000;
uint16_t gs = gas_resistence / 1000;
uint16_t lum = luminosity;
// Buffer construction
m_lora_app_data_buffer[buffSize++] = 0x01;
m_lora_app_data_buffer[buffSize++] = (uint8_t)(temp / 256);
m_lora_app_data_buffer[buffSize++] = (uint8_t)(temp % 256);
// ... and so on for each sensor
This payload structure is decoded on the receiving side (Ubidots) using a custom decoding function. If you're using a different format or sensor setup, don’t worry—the important takeaway is the process itself: once you understand how webhooks are configured and how payloads are decoded and visualized, you can adapt the logic to match your own use case with minimal changes.
Let’s get started!
1: Adding a Webhook in TTNNow that we have set up our IoT sensors and processed the data within TTN, we will use webhooks to send this data to an external service like Ubidots. Follow the steps below to configure a webhook:
- Navigate to Webhooks: In your TTN application, select the “Integrations” option, then choose “Webhooks.”
- Add a Webhook: Click on the “+ Add Webhook” button. You will see several options for different services (e.g., Ubidots, Datacake, Cayenne).
You will be presented with multiple services that you can connect with via webhooks.
For this module, we will focus on Ubidots due to its educational value, ease of use, and compatibility with TTN.
3: Configuring Webhooks with Ubidots- Creating an Account- Go toUbidots STEMand create a free account here [https://ubidots.com/stem].
- Once the account is created, log in and go to the Devices section to create your first plugin.
- In the Devices menu, choose Plugin.
- Select The Things Stack as the plugin.
- Configure the plugin by assigning a name and description. This plugin will act as the receiver for the data sent via TTN webhooks.
After creating the plugin, return to the TTN console and complete the webhook setup by entering:
- Webhook ID: A name of your choice for identifying the webhook.
- Plugin ID: This is the endpoint URL provided by Ubidots, formatted as needed.
- Token: Any desired string to authenticate the webhook.
Once this is complete, the TTN application will begin forwarding sensor data to Ubidots for visualization.
Now we will edit the plugin; to do this, click on the name or on the three vertical dots to the right of the Status column. Go to Decoder and copy the HTTPs Endpoint URL field right away, as this will be used to integrate this Ubidots with TTN
After setting up your plugin in Ubidots, scroll down to the Decoding Function section. This is where you define how to interpret the raw payload coming from TTN.
To activate the decoder, uncomment line 37 (or the equivalent where the function starts), then press Save & MAKE LIVE to apply your changes.
The decoding script is responsible for unwrapping the raw base64-encoded data received from TTN and converting it into readable sensor values. Here's a generic explanation of how this works:
// Unwrap the payload sent from TTN
let bytes = Buffer.from(args['uplink_message']['frm_payload'], 'base64');
// Decode the binary data into human-readable values
var decoded_payload = decodeUplink(bytes).data;
// Merge the decoded data into the Ubidots format
Object.assign(ubidots_payload, decoded_payload);
return ubidots_payload;
The key part here is the decodeUplink
function, which interprets the byte array based on how your device constructs its payload. In our specific example, the payload is structured using a simple format where each sensor value is sent as two bytes, preceded by a type identifier. Here's the function used to decode it:
function decodeUplink(bytes) {
var decoded = {};
// Our example uses 0x01 to indicate a standard sensor packet
if (bytes[0] == 1) {
decoded.temperature = ((bytes[1] << 8 | bytes[2]) - 5000) / 100;
decoded.pressure = bytes[4] << 8 | bytes[5];
decoded.humidity = ((bytes[7] << 8 | bytes[8]) - 5000) / 100;
decoded.gas = bytes[10] << 8 | bytes[11];
decoded.lum = bytes[13] << 8 | bytes[14]; // Luminosity
}
return { data: decoded };
}
module.exports = { formatPayload };
Note: This decoding logic corresponds to the payload structure created in our sendLoraFrame()
function, where we pack temperature, pressure, humidity, gas resistance, and luminosity into a byte buffer.
If you're using a different sensor setup or encoding structure, you'll need to modify the decoder to match your data format. The key is to understand how each value is packed on the device side (e.g., how many bytes, any offsets, or scaling factors) so you can reverse the process here in JavaScript.
Once this function is properly configured, Ubidots will begin displaying meaningful data from your TTN application—ready to be visualized or used in alerts.
When you go to Devices, it's now listed the device as recognized and if you double click on it you may see the readings.
- Go to the Dashboard section in the Data menu.
- Click on the three horizontal bars icon (on the left, below the Uibdots logo). Note: Do not confuse this with the + symbol; this is used to add objects to an already-created Dashboard.
- Click Create, access the Settings and Appearance options.
- After configuring, click Save.
Now click on the + button and select “Line chart”.
Click “+ ADD VARIABLE” and select the device. Choose the variables and press Select when you are done.
Now you can customize the appearance, and it will display as you can see in the example
In Ubidots STEM (educational), the Events option allows you to create alerts and automated actions based on specific conditions for “our” data.
1.Accessing Events:- Go to the Data menu and select Events.
- After clicking the plus button to create new events, a window will open (see the next image), where you’ll need to complete three sections: Trigger, Action, and Settings.
The Trigger defines the condition that activates the event. You can set it to monitor a specific variable and trigger when that variable reaches a certain value or condition.
Start by choosing the variable, click the device and choose the variable.
For example, for a Gas concentration variable, you could set the condition to activate if this value is smaller than 10 for at least one minute. Also if you click on the bottom add trigger you can set conditional trigger that will take effect depending on AND/OR the conditions are met. In this example we choose to trigger when the gas concentration is low and there are lights on (above 100).
After completing this section, click NEXT to move on to Actions.
The Action defines what will happen when the trigger condition is met. Here, you can set actions such as sending notifications by email, SMS, or making an HTTP request to another service. For example, this Action could send an alert email with a predefined system message or a custom message.
Click + ADD ACTION and See the image:
Note: In this window, we can enable/set the repetition of this action, i.e., the frequency with which actions are executed, and add conditions to avoid multiple rapid triggers of the event. For example, you can set a frequency like “Send a notification every 5 minutes while the condition is true, but only up to 20 times.” See the image below. Additionally, we can use the “back to normal” option, which sends a notification when the trigger condition no longer exists. Multiple actions can be added.
In Settings, we need to configure additional event details, such as the event name. For example: Event Name: "High Temperature Alert, " and its Activation, e.g., every weekday between six in the morning and ten at night. Finish by clicking Save.
If everything is working smoothly you will see your event displayed like in the following picture:
Comments
Please log in or sign up to comment.