MQTT - Process data, Publish it and Subscribe to a topic

MQTT - Process data, Publish it and Subscribe to a topic

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”

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.

  1. const { Analysis, Services } = require("@tago-io/sdk");

  2. const DEVICE_ID = "YOUR_DEVICE_ID";

  3. async function mqttPushExample(context, scope) {
  4.  console.log(context, scope)
  5.  if (!scope.length) {
  6.    return context.log("Missing values");
  7.  }

  8.  const myData = scope.find((x) => x.variable === "payload") || scope[0];
  9.  if (!myData) {
  10.    return context.log("Couldn't find any variable in the scope.");
  11.  }

  12.  // Create your data object to push to MQTT
  13.  // In this case we're sending a JSON object.
  14.  // You can send anything you want.
  15.  // Example:
  16.  // const myDataObject = 'This is a string';
  17.  const myDataObject = {
  18.    variable: "temperature_celsius",
  19.    value: (myData.value - 32) * (5 / 9),
  20.    unit: "C",
  21.  };

  22.  // Create a object with the options you chooses
  23.  const options = {
  24.    retain: false,
  25.    qos: 0,
  26.  };

  27.  // Publishing to MQTT
  28.  const mqtt = new Services().mqtt;
  29.  mqtt.publish({
  30.      bucket: DEVICE_ID,
  31.      message: JSON.stringify(myDataObject),
  32.      topic: "sensor/output",
  33.      options,
  34.    }).then(context.log, context.log)
  35. }

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

    • MQTT

      MQTT stands for MQ Telemetry Transport; it's an extremely simple and lightweight publish-subscribe messaging protocol. It was designed for constrained devices and low-bandwidth, high-latency or unreliable networks. TagoIO has its own MQTT broker, ...
    • 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

      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 received by any device that has subscribed to that specific topic. There are different ways to ...
    • MQTT with Sensor Tag

      This is an example using the SensorTag Bluetooth module from Texas Instruments to send data to TagoIO. There is no code modification needed in the SensorTag side, and as it uses the MQTT protocol, only a configuration setup is needed. For the ...
    • MQTT Retain on TagoIO Broker

      TagoIO's MQTT broker, while not designed to support the native retain feature found in standard MQTT protocol implementations, we offer a workaround to achieve similar functionality. This approach involves utilizing Analysis and Actions features to ...