What is MQTT and where do I start to use it with Niagara?

MQTT is quite a big topic, so we are not aiming to tell you everything about MQTT in one article. But we can give you all the directions to start understanding it and start the implementation on Niagara, answering some of the main questions

What is MQTT?

MQTT is a protocol created initially for small devices with low bandwidth networks.

The communication is based on a publish/subscribe method where each "client" device interfaces with a "broker" to exchange data.

The reasons why it is getting popular could be summarise as:

  • Interoperability between devices, being a standard widely available in IoT
  • Easy and secure access to cloud systems
  • Low requirements in network bandwidth

Do you want to know more about MQTT? check the main website here: http://mqtt.org/faq

 

How is MQTT communicating data across devices?

As mentioned before, the protocol is based on a publish/subscribe system (pubsub) and the communication goes all in and out through a "broker"

The "broker" is a server that receives and transmits all the messages to the clients

The "clients" are devices such as IoT sensors, a Jace, that are publishing or subscribing data with the broker

When a client wants to share some data with other devices (send), it will "publish" the new data to the broker so it is available for all other clients that might need it.

When a client wants to get data that is published by another device (receive), it will "subscribe" to that data to receive any update

Data published and subscribed is identified through a "topic", which is literally a string that allows the broker to filter the messages for each connected client.

The topic can look like this for example: "maintopic/variablename".

Unlike with many other protocols where the data list is fixed, with MQTT topics can be created dynamically by the clients into the broker publishing new data sets with a new topic.

A broker can be a local device running a broker like "Mosquitto" for example, or it can be a remote cloud service, like Microsoft Azure IoT Hub, Google Cloud IoT, or Amazon AWS IoT for example.

MQTT is popular now also on domestic use and the open source community. I did integrate it using Mosquitto running on a PC and clients running Node-Red to exchange data.

I have found quite an interesting video on generic integration of MQTT with Node-Red, if you want to get a bit more deep into the use of this, here is the link https://youtu.be/NjKK5ab0-Kk

 

How is data packaged with MQTT?

MQTT has a series of standard parts on its messages, starting from the "fixed header" and the "variable header", but the main part we are interested on is called "Payload". This defines the data you exchange with the broker.

The payload can be just a simple number or string with multiple topics to exchange the data separately, like this:

TOPICS                              PAYLOAD

plant1/machineA/sensorX/value 20
plant1/machineA/sensorX/unit C
plant1/machineA/sensorX/timestamp 2018-08-01T12:00:30.123Z

But MQTT allows also the use of JSON to format the data, so in one topic you can package all the information you need

TOPIC
plant1/machineA/sensorX


PAYLOAD
{
"value": 20,
"unit": "C",
"timestamp": "2018-08-01T12:00:30.123Z"
}

Do you want to know more about JSON and how it is formatted?

Check out this link I found from W3Schools.com where they have a few chapters describing in a really nice way all the major types of data you can use:

https://www.w3schools.com/js/js_json_datatypes.asp

You can find on the menu on the left of the linked page all the specific chapters to find our more.

 

How is Niagara implementing MQTT?

 

The Niagara implementation of MQTT is available through the standard driver "Abstract MQTT". You can find it on your Workbench on the driver list when adding a new driver.

You can find a copy of the manual for Abstract MQTT on our support website.

Here is a quick link to the manual:

http://support.innon.com/Tridium_Niagara/MQTT/docMqtt.pdf 

 

The driver is free and uses points from your license on the Jace or Supervisor.

The points available to be used on the driver are the usual

  • Boolean
  • Numeric
  • String
  • Enum

For each point you can use a "Subscribe" and a "Publish" point type, depending on whether you want to "get" data from the broker or "send" data to the broker.

Each point will need a dedicated MQTT Topic and the payload is going to be just a number or string.

Linking to the "How is data packaged with MQTT?" part of this article, this means that you will need more topics to send more data, or more information about the same number for example.

Each point is then linked to another station point, from another driver or just created independently on the station, and will synchronise the data of that point with the MQTT point.

The implementation should be quite straight forward and well described in the manual.

 

Ok, but how about JSON implementation with Niagara then?

JSON support has been presented at the Niagara Forum in 2019.

Supporting JSON to parse or create data is not going to help only MQTT, but any implementation that makes use of such format. This implementation then is completely independent from MQTT.

A presentation of the new "Niagara Json Tool Kit" has been released to the public during the forum in 2019 showing how the new library of components called "jsonToolkit" (compatible only with N4.7 or higher, a specific version is also available to download for N4.4) allows to compose and parse JSON messages packaging the data into strings, that were then sent/received using MQTT through an Amazon AWS broker to communicate with a Philips HUE system and change colour on the RGB bulbs.

The components are contained in the new jsonToolkit module, which comes with the online documentation within the module files.

This module is included by default on all Niagara installation from N4.8 and over.

To use it with N4.7 you will need to copy the files manually.

The module files will need to be copied into the "modules" folder of your Niagara install (with the Workbench closed), usually "C:\Niagara\Niagara-4.X.XX.XX\modules"

Once the module is installed, if you open the "Help - Help Contents" on your Workbench

And then update the help section pressing the button "Load help" that will appear on the bottom left of the page, you will find now the jsonToolkit guide with all the instructions

The palette of the module contains all the components and some useful examples:

 

The jsonToolkit is a licensed feature. The requirements for it to work are

  • to pay a one-off license fee for the module
    • Part number DR-JSON for Jace and MAC licenses
    • Part number DR-S-JSON for Supervisor licenses
  • to have a valid SMA (ongoing software maintenance agreement) on the Tridium license where the module is used

Please contact our sales department at sales@innon.co.uk to have more information about prices.

You can find the latest release of the jsonToolkit on niagara-central https://www.niagara-central.com, inside the "Software" section.

Or you can download them from our support website here:

http://support.innon.com/Tridium_Niagara/MQTT/

 

This isn't a straight forward topic, so we recommend spending all the time required to go through the documentation and trying the examples using a trial station on your Workbench and a local broker, starting with some simple communication tests.

 

What useful tools have you got for me to test MQTT?

If you want to run some basic tests on your lab, you will probably need a broker.

Mosquitto will be perfect for this, and you can install it on a Raspberry Pi or a PC for example.

You can find the download here: https://mosquitto.org/download/

Another useful tool I can give you is MQTT SPY, which allows you to monitor the activities of the topics on a broker, and also publish test data on it for the tests.

You can find the download here: https://github.com/eclipse/paho.mqtt-spy/releases

And documentation/information here: https://www.eclipse.org/paho/components/mqtt-spy/