MQTT - Publishing and Subscribing

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 start an analysis; it may be by timer, an Action, or called by another analysis. The figure below shows the data flow from the analysis to the MQTT network.

This section describes how to Publish and Subscribe to a topic in the MQTT Broker. We are assuming that you already know how to connect to MQTT; if you do not, please read our article MQTT on how this is done.

Publishing to TagoIO

You can create an Action and attach it to any specific topic or wildcard topics, and take actions from there.

You can send the payload directly to your bucket, to trigger an Analysis, or through SMS or E-mail.

If you are sending a payload in a JSON format, the TagoIO backend will add a new field called metadata with a child field called topic. However, if you are sending a raw payload, you must transform your format into the TagoIO JSON format with a variable called payload and your raw data as value. This will also add a metadata with a topic name (identically to what would happen with a payload already in a JSON format).

QoS and Retained Message Support 

The Quality of Service (QoS) level is an agreement between the sender of a message and the receiver of a message. It defines the guarantee of delivery for a specific message

TagoIO officially supports QoS 0 and 1.
TagoIO also supports retained messages.

Legacy Support
Before version 5.7.0 of TagoIO Core, we had reserved topics,  tago/data/post and tago/analysis/:analysisID, those topics have since been deprecated, but they will still work for couple more years. If you use those topics, we recommend that you change to a mechanism using Action MQTT Topic. TagoIO will send an email for clients still using those topics three months priors to their deletion.
You can similarly create them (topic tago/data/post) on MQTT Trigger, and it will work just as it had before.

How to Debug MQTT with TagoIO

You can see all your connections directly on the Live Inspector, and you are able to check any connection, payload, QOS, Last Will messages, and more.
The messages are triggered with the following actions.

  • a client connects to the broker
  • a client disconnects from the broker
  • a client subscribes to any topic
  • a client unsubscribes from any topic
  • a client publishes to any topic

Subscribing - Single level

The single level is represented by the + sign, which is then added to the topic.

If the client subscribes to the topic home/+/temperature, the following topics will match:

  • home/kitchen/temperature
  • home/office/temperature
  • home/livingroom/temperature

This means that if any client publishes data to these topics above, the subscribed client will receive it.

The following topics WILL NOT match:

  • home/kitchen
  • home/kitchen/humidity
  • home/office/ac/temperature

Subscribing - Multi level

The multi level is represented by the # sign. By adding a # sign to the topic, the client will subscribe to all the following levels of that topic.

If the client subscribes to the topic home/#, the following topics will match:

  • home/kitchen
  • home/kitchen/temperature
  • home/office/ac/temperature
  • home/upstairs/bedroom/ac/temperature

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