← All posts

MQTT explained: how sensors and building systems pass data through a broker

MQTT is the OASIS standard messaging protocol behind many connected devices. This guide for smart building IoT readers covers how publish and subscribe works, what topics and the three delivery levels mean, and what the standard leaves to the people who build the system.

What it is

Anyone reading a smart building IoT proposal, for lifts, chillers or room sensors, is likely to meet the word MQTT. It is a messaging protocol: an agreed way for devices and software to send small messages to each other. OASIS, a standards body, publishes it. MQTT.org says versions 5.0 and 3.1.1 are OASIS standards and that 3.1.1 has also been ratified by ISO. Version 5.0 is dated 7 March 2019.

The standard describes MQTT as light weight, open and simple, and meant for places where a device has little memory or where network bandwidth "is at a premium". MQTT.org says it was invented in 1999 by Dr Andy Stanford-Clark of IBM and Arlen Nipper of Arcom.

The problem it addresses

A sensor in a lift shaft or plant room is often a small device on a mobile or otherwise patchy connection. MQTT.org says the protocol was designed for constrained devices and for "low-bandwidth, high-latency or unreliable networks". It also keeps senders and receivers apart, so a device does not need to know which systems will use its data.

How it works

  • Broker in the middle. Devices do not talk to each other directly. Each one connects to a server, usually called a broker, which MQTT.org describes as a server that routes published messages to subscribers.
  • Publish and subscribe. A device publishes a message. Any program that has subscribed to that kind of message receives a copy. The standard says this gives one-to-many distribution: one reading can feed a dashboard, an alarm service and a data store at once.
  • Topics. Every message carries a topic name, a label with levels separated by a forward slash. As our own illustration, a door sensor might publish to tower-a/lift-3/door. A subscriber can use wildcards: + stands for one level and # for all levels below, so tower-a/+/door would cover the doors of every lift in that tower.
  • Any payload. The standard says the transport is "agnostic to the content of the payload". MQTT carries the message; it does not define what is inside it.
  • Both directions. MQTT.org says messages can go from device to cloud and from cloud to device.

Three delivery levels

The standard defines three qualities of service:

  • At most once (QoS 0): best effort, and a message can be lost. The standard's example is ambient sensor data, where the next reading follows soon after.
  • At least once (QoS 1): the message is assured to arrive, but duplicates can occur.
  • Exactly once (QoS 2): the message arrives once only. The standard's example is a billing system, where a lost or repeated message would cause a wrong charge.

Knowing when a device drops off

A device agrees a Keep Alive interval with the broker. If the broker hears nothing within one and a half times that interval, the standard says it must close the connection as if the network had failed. A device can also register a Will Message in advance, which the broker publishes if the connection is not closed normally. This is how other systems can learn that a sensor has gone silent. MQTT.org adds that persistent sessions shorten the time needed to reconnect.

Things to keep in mind

  • MQTT does not set the data format or the topic names. Two products that both "support MQTT" can still fail to understand each other's messages.
  • Security is a design choice, not a default. The standard says it is "the implementer's responsibility" to include suitable security features, and its security chapter is guidance, not a requirement. It discusses TLS encryption, user name and password or certificate checks, and limits on which topics a client may publish or subscribe to, with particular care over broad filters such as #.
  • The standard strongly recommends TCP port 8883 for servers that offer TLS. MQTT.org lists port 1883 as the registered port for MQTT without it.
  • The delivery levels are a trade-off. At most once sends no acknowledgement and makes no retry, so it is the lightest and the only one that can lose a message. It is worth asking which level each kind of data uses and why.
  • MQTT.org's statements about scale and ease of use are its own descriptions; results depend on the broker and the network chosen.

Sources

Disclaimer. This guide is for general information only and is not professional advice. It is compiled automatically from the public sources listed above and written in our own words. We do not guarantee that it is accurate, complete or up to date, and products, features and prices can change. Product and company names, and any video shown, belong to their owners, who have not reviewed or endorsed this post. Please check the original source before relying on it. Read the full disclaimer.

Want to know what this means for your own systems?

Talk to us

Related service: AIoT for elevators