Starting on
June 1st, TagoIO will no longer provide MQTT broker to Free accounts.
Read more
In this tutorial, you will learn how to process data, publish to a topic, and subscribe to it. We will be using the
MQTTX client throughout this tutorial.
Connecting to TagoIO MQTT Broker
To publish a topic via TagoIO's MQTT, you must first add a device.
Navigate to the 'Devices' module in your Admin panel and create a new device. You may choose any connector you prefer; it will not make a difference. Afterward, proceed to the 'General Information' tab of your new device, and in the tokens section, copy your device token.
Open MQTTX and create a new connection
On the Connection screen, set the following information:
Name: “Any name”
Host: mqtt.tago.io
Port: 1883
Username: "Token"
Password: “Your device token”
Client ID: Can be set to any unique identifier
The Client Identifier (ClientID) is a required field to connect to the TagoIO MQTT Broker.
Subscribing to a topic
Now to subscribe to a topic just go to the subscribe section, type "sensor/output" and click subscribe.
Processing the data
In this step, we will create an analysis to process the data on the topic "sensor/input" and receive the processed value on the topic "sensor/output". Let's start by creating an analysis that converts temperature from Fahrenheit to Celsius.
Below is the code that performs the conversion. It's a straightforward analysis that receives the data, converts it to Celsius, and then publishes it on the topic to which you are subscribed.
Note: Replace "DEVICE_ID" with your actual device ID. You can obtain this ID by accessing your device's page and copying it from the URL, for example: https://admin.tago.io/devices/652425f4516e99000a522dce.
- const { Analysis, Services } = require("@tago-io/sdk");
- const DEVICE_ID = "YOUR_DEVICE_ID";
- async function mqttPushExample(context, scope) {
- console.log(context, scope)
- if (!scope.length) {
- return context.log("Missing values");
- }
- const myData = scope.find((x) => x.variable === "payload") || scope[0];
- if (!myData) {
- return context.log("Couldn't find any variable in the scope.");
- }
- // Create your data object to push to MQTT
- // In this case we're sending a JSON object.
- // You can send anything you want.
- // Example:
- // const myDataObject = 'This is a string';
- const myDataObject = {
- variable: "temperature_celsius",
- value: (myData.value - 32) * (5 / 9),
- unit: "C",
- };
- // Create a object with the options you chooses
- const options = {
- retain: false,
- qos: 0,
- };
- // Publishing to MQTT
- const mqtt = new Services().mqtt;
- mqtt.publish({
- bucket: DEVICE_ID,
- message: JSON.stringify(myDataObject),
- topic: "sensor/output",
- options,
- }).then(context.log, context.log)
- }
- module.exports = new Analysis(mqttPushExample);
This analysis requires permission to send MQTT data to this device. Let's enable this access in the Access Management module by creating a new Policy settings screen.
To initiate this analysis, we must create an action. Proceed to the Actions module and create a new action. Fill in the name, choose "MQTT Topic" for the "Type of Trigger," and select "Run Analysis" for the "Type of action." Then, select the analysis you have just created. Once all steps are completed, click "Create my Action."
Now let's configure your action. Under the trigger section, on "Select the device" select the device you created. On the "Trigger" field, type the input topic "sensor/input"s. After you are done, click on "Save".
By doing this, every time a message is published on the "sensor/input" topic, it will trigger the analysis you selected. Let's try it!
Publishing the message "32" on the topic "sensor/input" using the MQTTX client triggers the analysis, which converts the value to Celsius and sends it to the topic "sensor/output". This value should then appear in the client.
If you need help with your MQTT connection, request help in our
Community!
Related Articles
Connecting your MQTT Broker to TagoIO
TagoIO supports MQTT connections through our MQTT Relay command-line tool. This software acts as a bridge between your MQTT Broker and the TagoIO platform, facilitating seamless integration and data flow. Developed in Rust, the TagoIO MQTT Relay is ...
MQTT
Starting on June 1st, TagoIO will no longer provide MQTT broker to Free accounts. Read more MQTT stands for MQ Telemetry Transport; it's an extremely simple and lightweight publish-subscribe messaging protocol. It was designed for constrained devices ...
Defining Actions
Actions are initiated based on specific conditions and can execute a variety of tasks. The first step in configuring an Action is to specify the desired action. Below are the different types of actions available: Push Notification to myself This ...
Trigger by MQTT Topic
The trigger type, Trigger by Variable, allows you to execute your Action when data is sent to an MQTT topic. TagoIO has its own MQTT broker that is responsible for pushing data to clients in case something new is published in the specific topics they ...
MQTT - Publishing and Subscribing
Starting on June 1st, TagoIO will no longer provide MQTT broker to Free accounts. Read more You can publish to your MQTT topics by coding a script that will run from an Analysis. When the analysis runs, your script can publish a topic that can be ...