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

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

In this tutorial you are going to learn how to process some data, publish to a topic and subscribe to it.

To subscribe to a topic we will use Mqttfx. To download and learn more about Mqttfx go to their website https://softblade.de/en/welcome/.


Connecting to Tago

To connect to the Tago MQTT and publish any topic, first, we need to add a device. Go to the Device section and create a device. Then, go to the section tokens and copy your device-token.


Open Mqttfx and click on the gear icon

On the Connection Profile window set the following information:

  • Profile Name: “Any name”
  • Broker Address: mqtt.tago.io
  • Broker Port: 8883
  • Client ID: “Any value”

On the section User credentials set the following information:

  • User Name: “Any value” Tago validates your user by the token only
  • Password: “Your device token”

On the SSL/TLS section check the option Enable SSL/TLS and click ok

Now you can connect to Tago using the configuration you set up. Select the connection profile that you have just created and click connect

When you are connected the connection indicator will be green


Subscribing to a topic

Now to subscribe to a topic just go to the subscribe section, type the topic you want to subscribe, it can be any topic and click subscribe

The list of topics you are subscribed to will appear here.


Processing the data

Now that you have subscribed to a topic, let’s create a dashboard to send data.

Then we will create an analysis to process that data and publish the results to the topic you are subscribed to.

Go to the admin and then create a dashboard, add an Input Form Widget, type the variable "temperature" and save. You will have a widget like the one in the image below.



If you need help creating a widget go to the Getting Started documentation and check out the steps to build a dashboard.

Now that you have a widget to send data to the variable temperature, let’s create an analysis that converts that temperature from Fahrenheit to Celsius and publishes the result in the topic you are subscribed to. You can see the code that converts the analysis below, it’s a very simple analysis that receives the data, converts it to Celsius and then publishes it on the topic you are subscribed to.

const Analysis = require('tago/analysis');
const Services = require('tago/services');

function myanalysis(context, scope) {
  if (!scope.length) return context.log('no data');

  const myData = scope[0];

  const dataInC = {
      variable: 'temperature_celsius',
      value: (myData.value - 32) * ( 5 / 9 ),
      unit: 'C',
  };
  const options = {
      retain: false,
      qos: 0,
  };

  dataInC.value = dataInC.value.toPrecision(3);

  const MQTT = new Services(context.token).MQTT;

  MQTT.publish('tago/my_topic', JSON.stringify(dataInC), myData.bucket, options).then(context.log).catch(context.log);
}

module.exports = new Analysis(myanalysis, 'YOUR ANALYSIS TOKEN HERE');
Download analysis.js

To use this code go to analysis, create a new one, fill up the name, set the timer to disabled, download the analysis above and upload it on the analysis and save. By doing this we have an analysis that converts your data and publishes it on the topic “tago/my_topic”.



Now we need to create an action to trigger this analysis. To do this go to action, create a new one, fill up the name, on “Type of Trigger” select “Variable”, on "Type of action" select "Run Analysis", and then select the analysis you just have created. After everything is complete, click on "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 same variable you selected on the input widget, that is temperature, and on condition select "Anything". After you are done, click on "Save".



By doing this, every time any data is added to the variable temperature, it will trigger the analysis you selected.

Now go to the dashboard you created, on the input type any number and click "Submit".



When you send the value, the action will be notified that the variable temperature received some data, then it will trigger the analysis. The analysis will convert the temperature and publish it on the topic.

Go to the Mqttfx and you can see the data that the analysis published the result of the conversion.


    • 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 that ...
    • MQTT - Example with MQTT.fx

      This is a tutorial on how to connect to Tago MQTT, subscribe to topics using wildcards and publish to a topic. For this tutorial we will use the software Mqttfx. To download and learn more about Mqttfx go to their ...
    • 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 ...