MQTT basics: publish/subscribe, topics, QoS and security
MQTT basics
MQTT (Message Queuing Telemetry Transport) is the standard OASIS messaging protocol for IoT and IIoT applications, built on TCP/IP. It is light, works well on unreliable or constrained networks, and decouples the devices that produce data from the ones that consume it, which is why it is the usual way to bring machine data to a SCADA, a historian or a cloud platform.
Publish and subscribe
One device is the publisher and sends data; any device that wants it is a subscriber. The data is sent to a topic chosen by the publisher, and a subscriber receives only the topics it has subscribed to. Publishers and subscribers never interact with each other: a broker receives every message and distributes it to the right destinations.

Figure from mqtt.org.
Topics
Topics are hierarchical strings separated by slashes, for example plant1/line2/pump3/pressure. Subscribers can use wildcards: + replaces one level (plant1/+/pump3/pressure) and # replaces all the levels below (plant1/line2/#). A consistent naming convention from the start (site, area, machine, magnitude) saves a lot of work when the number of devices grows.
Quality of service, retained messages and last will
- QoS 0: at most once. The message is sent once with no confirmation; fast, but it can be lost.
- QoS 1: at least once. The message is confirmed, and it can arrive duplicated.
- QoS 2: exactly once. The safest and slowest option.
- Retained message: the broker keeps the last message of a topic and gives it to every new subscriber, so a dashboard knows the last state as soon as it connects.
- Last will: a message that the broker publishes if a client disconnects unexpectedly. It is the standard way to detect that a machine has gone offline.
Security
MQTT itself does not encrypt anything. MQTTS adds TLS (Transport Layer Security) for encryption and authentication of the broker, without changing how the protocol works. In an industrial installation, require a user and password (or client certificates) in the broker, limit what each client can publish or subscribe to with access control lists, and use TLS whenever the traffic leaves the machine network.
Where MQTT fits
- IIoT: monitoring and controlling machinery, and collecting data from sensors in manufacturing and process plants.
- Agriculture: soil moisture, weather and irrigation control.
- Building and home automation: lighting, climate and security systems with little bandwidth.
- Healthcare: remote monitoring that sends data from devices to the providers.
The broker
The broker processes the clients and the data of an MQTT environment. Several brokers exist, such as HiveMQ, EMQX or Eclipse Mosquitto, and the choice depends on the number of messages to handle, the security features and the resources available. In this course we use Eclipse Mosquitto, an open source broker light enough to run on the PLC itself.
See the PLC range at www.industrialshields.com.
There are no comments for now.