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 www.mqttfx.org.

Connecting to Tago

To connect to the Tago MQTT and publish to any topic, first, we need to adda device. Go to the admin 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, dashboard then create a dashboard, add an input widget, select 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 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',
}

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

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

MQTT.publish('tago/my_topic', JSON.stringify(dataInC)).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 and on “Action to be taken” select “Run analysis”, when you select this option, you have to select which analysis to run, select the analysis that you have just created.

Then on the trigger section, select the same variable you selected on the input widget, that is temperature, on condition, select any, disable the trigger lock and 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 send.

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.