MQTT: getting equipment data to the cloud
Meters, sensors and controllers produce data all the time. MQTT is the most widely used protocol for getting that data to a dashboard, a database or the cloud, even over slow or unstable networks.
What MQTT is
MQTT is an open, lightweight protocol standardised by OASIS. Instead of a system polling the device every so often, the device publishes data when it changes, and whoever is interested subscribes and receives it.
In the middle sits the broker, a server that receives every message and delivers it to the subscribers.
Topics
Each message is published to a topic, an address organised in levels, such as plant/line2/pump3/temperature. A dashboard can subscribe to that one value, or to everything on line 2 at once.
Delivery levels
MQTT lets you choose the delivery guarantee for each message:
- QoS 0: sent once, without confirmation. Fine for frequent readings, where losing one makes no difference.
- QoS 1: guaranteed to arrive, possibly more than once.
- QoS 2: guaranteed to arrive exactly once. Slower, for commands and important events.
Two features help a lot in practice: the retained message, which hands the latest value to anyone who has just connected, and the last will, a message the broker publishes by itself when a device drops off, announcing that it went offline.
From the device to MQTT
Many devices do not speak MQTT. In that case a gateway or a piece of software reads the device in its own protocol (Modbus, BACnet, an API) and publishes to the broker. In new products, we also develop firmware that publishes directly.
Precautions
- Security: encrypted connections (TLS), a username and password or a certificate per device, and permissions per topic. A broker open to the internet is a risk.
- Topic structure: defined at the start of the project, because changing it later means touching every device.
- Data format: a single standard (for example, JSON with unit and timestamp) stops each device from sending data its own way.
- Unstable networks: store data on the device while the connection is down and send it when it returns.
Need to get your equipment’s data to a dashboard or the cloud? Talk to CONTE.