Publish and subscribe instead of request and response
Most fieldbus protocols follow a request-response pattern: a master asks, a device answers. MQTT turns this around. A device publishes its values without knowing whether anyone is listening. Whoever needs the values subscribes to them at the broker.
This leads to the most important property: decoupling. A second dashboard, an analytics platform or a research project can subscribe to the same values without any change to the device in the basement. With Modbus, that would be one more client polling on top.
Topics: paths, not addresses
A topic is a hierarchical path, for example site/building-a/basement/meter-03/active-energy-import. There is no central registration – a topic comes into being when someone publishes to it.
Subscriptions use wildcards: + for one level, # for everything below. site/building-a/# delivers everything from building A. This makes the structure of the topic tree the real data architecture – and explains why a well-thought-out scheme pays off. Concrete recommendations are in Planning MQTT topics.
QoS, retain and last will
| QoS 0 | Send and forget. For values that will be sent again shortly anyway. |
|---|---|
| QoS 1 | Deliver at least once. Duplicates possible, loss not. The normal case for meter readings. |
| QoS 2 | Exactly once. Costly, rarely needed. |
| Retain | The broker keeps the last value per topic. A newly connected dashboard shows data straight away instead of an empty screen. |
| Last will | A message the broker publishes when the connection drops unexpectedly. A failure becomes visible instead of staying silent. |
The last two are underrated in metering systems. Retain solves the empty dashboard after a restart. Last will solves the bigger problem: a failed device goes unnoticed, because in a publish model silence looks like normal operation.
Why MQTT suits building data
- Outgoing connection – no inbound firewall rule needed
- Low overhead, works over mobile networks too
- Several consumers without extra load on the device
- TLS encryption
- Copes with interrupted connections
In practice the first point is the one that decides. More in Getting meter data into the cloud.
What MQTT is not
MQTT is not storage. The broker delivers and forgets – apart from the retained message. If you need history, you need a database behind it or local buffering in front of it. The EMU M-Center has 16 GB for this purpose and can deliver missed values after an interruption.
MQTT is also not a query protocol. A value that nobody has published cannot be requested. If you need instantaneous values on demand, Modbus TCP serves you better – or use both in parallel.