Introduction to MQTT

Creation date: 2/3/2025 4:13 PM    Updated: 2/6/2025 11:11 AM   introduction mqtt smp
Summary:
MQTT (Message Queuing Telemetry Transport) is a lightweight protocol designed to be used by lightweight and low power devices. It was originally designed by IBM as a method of transporting data from sensors on oil pipelines.

Its lightweight approach makes it a desirable choice for remote locations where data transport comes at a premium. This is perfect for such situations such as battery powered devices or devices connected to metered internet connections (4G/5G or Satelite for example).

In addition to this, MQTT also includes consideration for security, quality of service, and message expiry handling such as "Last Will and Testament".

MQTT communications use existing TCP/IP communication methods, making it a strong solution for cloud based monitoring and control.

MQTT Topology

MQTT uses a central server called an "MQTT Broker" where all MQTT messages are sent to and recieved from. From here, a publish and subscribe model is used.
MQTT devices then connect to this broker where it can publish to and subscribe to data points called "MQTT topics"

MQTT Topics

MQTT Topics are a human readable string where a data value can be stored. Topics can be organised via a slash creating a directory style structure which can then be searched against.

Examples:

Basic:
A basic example of a topic name can just be "sensor". This string can then be assigned an alphanumerical value.

Intermediate:

In practice, a topic is often broken down into a structure which can then be searched against such as 
"Site Name/Level 1/Bathroom/Male/Sensors"

As we can see, we use a slash to start structuring data. Each name between the slashes represents a topic, where in this case, "Site Name" is the parent topic with a series of sub topics beneath it, each cascading beneath its previous topic. This means that the topic "Sensors" is a parent of "Male" which is a parent of Bathroom... so forth. Each level can be assigned a value.

Advanced:

Now that we have a topic structure, now we can add to this. In the previous example we looked at the sensors in the male bathroom on level 1. We can now assign 2 more topics to this.

"Site Name/Level 1/Bathroom/Male/Sensors/Occupancy"
"Site Name/Level 1/Bathroom/Male/Sensors/Light Level"

Now the Sensors topic has 2 data points available to it, each capable of storing their own unique values.

This means that a request can be made for the information at topic "Site Name/Level 1/Bathroom/Male/Sensors" and it will provide the status of the Sensors topic as well as Sensors/Occupancy and Sensors/Light Level.

MQTT Data

MQTT supports sending raw payloads of data, meaning that any data can be sent to a topic, this makes it flexible for different solutions (JSON, XML, binary etc). This means that there is no predefined object model—interpreting data is up to the application.

MQTT Publish and Subscribe

When an MQTT device writes to a topic, it "Publishes" this information to the MQTT broker. If another device wants this information, it must "Subscribe" to this information. A device can both publish and subscribe to the same topic, allowing for multiple devices to control and monitor this topic.

MQTT Quality of Service

Depending on the importance of a message making it to the MQTT broker, you have the choice of 3 different levels of Quality of Service that can be applied to each message sent to the MQTT broker. These levels depend on the network requirements and are as follows

Option 0: The message is attempted to be delivered at most 1 time

The MQTT device sends the message and does not listen for a response from the broker acknowledging the message

Option 1: The message is attempted to be delievered at least 1 time

The MQTT device sends the message and listens for a response from the broker acknowledging the message. If it does not recieve the acknowledgement, it sends the message again

Option 2: The message is only ever delivered once

The MQTT device requests permission to send a the value, waits for the MQTT broker to respond, and then sends the data

MQTT Security

MQTT offers a few options of security depending on what requirements are needed. The security settings are determined by the MQTT broker, and requires that all MQTT devices follow these security settings

No Security - Any device can connect to the MQTT

Username and Password - Any device can connect to the MQTT broker so long as the MQTT device has the username and password. Communications are sent in clear

Security Certificate - Uses a sercurity certificate to secure data transported between the MQTT device and MQTT broker. This method can allow for authentication off of a security certificate or can require both the security certificate and a username and password.

MQTT Last Will and Testament

When an MQTT device connects to a broker, it can provide the broker with details of a topic, and what value it should be set to if the MQTT device ever loses connection to the broker.

This allows the MQTT broker to inform any other MQTT device that is subscribed to this topic that the device has disconnected.

More Information

At Lumen Resources, we support education and growth within the lighting control and integration community. Working in conjunction with zencontrol's DALI solutions, we provide support services for integrating zencontrol via MQTT to other platforms.

The following articles may be of interest

Files