What Is MQTT? A Practical Guide to MQTT and IoT in Buildings
MQTT explained for building owners: broker, topics, QoS, retained messages, MQTT 5, Sparkplug B and security, with a sample topic namespace and an architecture checklist.
MQTT is an open, lightweight publish/subscribe messaging protocol, standardised by OASIS and as ISO/IEC 20922. Devices publish readings to named topics on a central server called a broker, and any authorised application subscribes to the topics it needs. In buildings, MQTT is the usual way to move sensor and BMS data to historians, analytics and the cloud.
What MQTT does not do matters as much. It does not run a chiller's control loop, and it does not say what a number means: 23.4 could be a supply air temperature or a valve position. So good building IoT pairs MQTT with BACnet or Modbus below it and a naming dictionary around it. Below: how it works, versions, Sparkplug B, security, a protocol comparison, a sample topic namespace for a building and our architecture checklist.
How MQTT works: broker, topics and publish/subscribe
Every MQTT device or application is a client. Clients open an outbound TCP connection to the broker (port 1883, or 8883 with TLS; both are registered with IANA), then publish messages, subscribe to topics, or both. Publishers never talk to subscribers directly. A meter gateway publishes once; the historian, energy dashboard and fault detection each get a copy. Because gateways connect outward, no inbound firewall rule is needed per device.
Topics are text paths separated by /, such as ryd01/twr-a/l12/hvac/ahu-12-1/sat. Subscribers can use two wildcards: + matches one level and # matches everything below a level. Topics that start with $ are reserved for the broker's own information, such as $SYS. MQTT was invented in 1999 by Andy Stanford-Clark of IBM and Arlen Nipper of Arcom (now Eurotech), and one of its first jobs was SCADA over satellite links, which is why it is frugal: it sends data when values change instead of answering a fixed poll.
QoS 0, 1 and 2: which level for which point
Every message carries a quality of service (QoS) level. The broker delivers at the lower of the published QoS and the subscriber's requested QoS, so set both ends on purpose.
| QoS | Guarantee | Overhead | Use it in a building for |
|---|---|---|---|
| 0 | At most once: sent, never acknowledged | Lowest | Frequent telemetry (temperatures, CO2) where the next reading replaces a lost one |
| 1 | At least once: acknowledged, may arrive twice | Moderate | Alarms, status, meter totals and commands, designed so a duplicate does no harm |
| 2 | Exactly once: four-step handshake | Highest | Rare: only where a duplicate would cause harm and the receiver cannot filter it |
One design habit makes QoS 1 safe for energy data: publish meters as cumulative totals (the kWh register), never as increments. A duplicated total changes nothing; a duplicated increment double-counts energy on the bill.
Retained messages and the last will
A retained message is the last value the broker keeps on a topic and hands to every new subscriber at once, so a dashboard opened at 9:00 shows the current setpoint immediately. An empty retained message clears it. The last will is a message a client registers when it connects; if the client vanishes without a clean disconnect (power cut, broken cable), the broker publishes it. Pair a retained online status with a will of offline, and every application learns a gateway has died within one and a half keep-alive intervals.
MQTT 3.1.1 vs MQTT 5, and where Sparkplug B fits
MQTT 3.1.1 became an OASIS Standard in October 2014 and ISO/IEC 20922 in 2016. MQTT 5.0 became an OASIS Standard on 7 March 2019. Both are in wide use and most brokers speak both. The version 5 changes that matter in a building:
| MQTT 5 feature | What it fixes in a building |
|---|---|
| Reason codes on every acknowledgement | You see why a publish or subscription was refused instead of guessing |
| Message expiry and session expiry | A setpoint sent during an outage expires instead of arriving hours late |
Shared subscriptions ($share/group/topic) | Several historian or analytics workers split one busy feed |
| User properties | Metadata such as source or unit travels with the message, by your own convention |
| Enhanced authentication | Challenge/response and token methods beyond a plain password |
For new projects, specify MQTT 5 at the broker and accept 3.1.1 on devices that only support it.
Sparkplug B: structure on top of MQTT
Plain MQTT sets no rules for topic names or payloads. Sparkplug, an Eclipse Foundation specification whose version 3.0 is also published as ISO/IEC 20237:2023, adds them. Topics follow spBv1.0/group_id/message_type/edge_node_id/device_id. A gateway announces itself with birth messages (NBIRTH, DBIRTH) listing every metric and its data type, then reports only changes (NDATA, DDATA). Its death certificate (NDEATH) is registered as the MQTT will, so if the gateway drops, subscribers mark its data stale. Payloads are encoded with Google Protocol Buffers, and commands travel on NCMD and DCMD topics.
Sparkplug comes from industrial SCADA and is strong at state awareness and discovery. Its limits in buildings: the binary payload is not human-readable, it still does not say a metric is a supply air temperature, and many building devices ship plain MQTT with JSON instead. Check what your gateways and historian support before you specify it.
Semantics are still your job
Neither MQTT nor Sparkplug tells an analytics tool what a point is. Pair your topics with a tag dictionary, for example based on the open Brick or Project Haystack models, and make it part of the handover.
MQTT security: TLS, authentication and access lists
The MQTT specification leaves most security to the implementation, so five broker rules carry most of it:
- Encrypt everything with TLS on port 8883. Plain 1883 belongs on a test bench, not on a building network.
- One identity per device: unique credentials for every gateway, ideally an X.509 client certificate, never a shared password.
- Access control lists (ACLs) on the broker: each gateway publishes only under its own branch, only approved applications publish to command topics, and only the historian subscribes to
#. - Keep the broker inside: on the building OT network or in a DMZ, with gateways connecting outward.
- Writes go through controllers: an MQTT command changes a setpoint in a BMS controller that enforces limits, never drives a chiller or safety system directly.
In Saudi Arabia, government entities and organisations that own or operate critical national infrastructure work under the NCA's Essential Cybersecurity Controls, and its OT Cybersecurity Controls (OTCC) add requirements for industrial control systems. Involve your cybersecurity team before a broker or historian moves to the cloud.
MQTT will deliver anything to anyone allowed to ask. The broker's access list is your real security design.
MQTT vs BACnet vs Modbus vs REST for buildings
These are layers more than rivals:
| MQTT | BACnet | Modbus | REST API | |
|---|---|---|---|---|
| Pattern | Publish/subscribe through a broker | Client/server, plus change-of-value subscriptions | Master polls slave registers | Request/response over HTTP |
| Data model | None: the payload is just bytes | Standard objects with units, alarms, schedules | Numbered 16-bit registers and coils, no names | Whatever the vendor's API defines |
| Typical transport | TCP 1883/8883, WebSocket | BACnet/IP (UDP 47808), MS/TP on RS-485 | Modbus TCP (502), RTU on RS-485 | HTTPS |
| Security built in | TLS and broker ACLs, if configured | Little in classic BACnet; BACnet/SC adds TLS | None in the classic protocol | HTTPS plus tokens or keys |
| Best job in a building | Moving data out to historians, analytics and cloud | Controlling and supervising HVAC and plant | Reading meters, drives and packaged equipment | Linking business apps such as a CMMS |
| Weak spot | No semantics; naming is up to you | Heavier, broadcast traffic, legacy security | No metadata, polling only | Polling, not built for streaming |
A typical Gulf tower keeps BACnet for HVAC control and Modbus on meters and drives, then adds MQTT to carry the data upward. In depth: BACnet, KNX and Modbus compared and Modbus explained.
A typical building IoT architecture, with a sample topic namespace
A sound design has six layers, each replaceable without rebuilding the others.
- Field devices: IAQ and occupancy sensors, meters, drives and chillers on BACnet, Modbus, wireless or MQTT.
- Controllers and BMS: keep running every control loop and interlock.
- Edge gateway: reads BACnet and Modbus, normalises names and units, publishes MQTT, and buffers data when a link drops.
- Broker: on-premises or at the edge, bridged to a cloud broker only if needed.
- Historian: a time-series database subscribing with a persistent session at QoS 1, so restarts lose nothing.
- Applications: fault detection, energy dashboards, a digital twin and the CMMS, usually over REST.
Sample topic namespace for one building
Pattern: site/building/floor/system/equipment/point, with commands and gateway status in their own branches so access rules stay simple. The examples use a Riyadh site coded ryd01.
| Topic | Carries | QoS | Retain | Who may publish |
|---|---|---|---|---|
ryd01/twr-a/l12/hvac/ahu-12-1/sat | Supply air temperature | 0 | Yes | Edge gateway |
ryd01/twr-a/l12/hvac/ahu-12-1/sat-sp | Active setpoint (state) | 1 | Yes | Edge gateway |
ryd01/twr-a/l12/hvac/ahu-12-1/alarm/filter-dp | Filter alarm, active or cleared | 1 | Yes | Edge gateway |
ryd01/twr-a/l12/iaq/zone-n/co2 | Zone CO2 in ppm | 0 | Yes | Sensor gateway |
ryd01/twr-a/b1/plant/ch-01/kw | Chiller electrical power | 0 | Yes | Edge gateway |
ryd01/twr-a/b1/elec/mdb-1/kwh | Main meter, cumulative energy | 1 | Yes | Meter gateway |
ryd01/status/gw-b1-01 | Gateway online or offline (last will) | 1 | Yes | That gateway only |
ryd01/cmd/twr-a/l12/hvac/ahu-12-1/sat-sp | Setpoint change request | 1 | Never | Approved applications only |
Rules: lowercase, no spaces, no leading / (it creates an empty first level), never +, # or $ in names, units and timestamps in the payload, and every name from one documented dictionary. A payload such as {"v":12.8,"u":"degC","ts":"2026-11-30T09:15:00Z","src":"bacnet:2001/AI-3"} also records which controller object the value came from, so every number stays traceable.
Common MQTT pitfalls in buildings
- Topic sprawl: each integrator invents its own naming and one AHU ends up under three paths. Fix the namespace in the specification.
- No semantics: numbers without units, point types or sources, so analytics stalls on clean-up.
- Cloud lock-in: a platform that requires its own SDK, topic structure or device registry. The test: could you repoint every gateway to another standard broker by changing one address?
- Retained commands: a retained setpoint change is replayed to every controller that reconnects.
Architecture checklist before you sign off
Use this pass or fail list on any MQTT design. The pass marks are our planning rules, not requirements of the standard.
| Check | Pass mark |
|---|---|
| Broker location | On-premises or at the edge; a cloud broker only as a bridge |
| Encryption | TLS on every connection; port 1883 closed outside the test bench |
| Access lists | Gateways write only their own branch; command topics writable by named applications only |
| Namespace | A documented, versioned dictionary covering 100% of published points |
| Traceability | Every topic mapped to its source BACnet object or Modbus register |
| Session awareness | Last will on every gateway; keep-alive of 60 seconds or less |
| Buffering | Store-and-forward on gateways for at least 24 hours of data |
| Exit test | Devices can move to another standard MQTT broker without re-flashing |
Where puniq fits
puniq is vendor-neutral and sells no broker, gateway or IoT platform. Our Data Auditor inventories every controller, protocol and data point, unifies them on open protocols (BACnet, Modbus and MQTT) in an on-premises layer you own, and closes with the Documented Data Map, the record a topic dictionary should be built from. Our advisory-only AI layer then works on top; see AI building management. To see where your building stands first, take the building intelligence readiness scorecard.
Is MQTT a replacement for BACnet?
No. BACnet runs control and supervision inside the building with a standard data model. MQTT moves data from gateways to historians, analytics and the cloud. Good designs use both, with an edge gateway translating points into MQTT topics.
What port does MQTT use?
TCP 1883 for unencrypted MQTT and 8883 for MQTT over TLS, both registered with IANA. On a building network, use 8883 and close 1883.
Which MQTT QoS level should I use?
QoS 0 for frequent telemetry, QoS 1 for alarms, status, meter totals and commands, and QoS 2 only where a duplicate would cause harm.
Do I need Sparkplug B?
Not always. Sparkplug adds a fixed topic structure, birth and death messages and typed metrics, which helps when many gateways feed one host. With plain MQTT and JSON, a documented namespace and payload convention does much of the same job.
More from puniq
What Is SCADA? Components, Protocols and Security Explained
SCADA explained for owners: components, how it differs from DCS and BMS, the protocols, where it meets Gulf buildings, and an IEC 62443 and NCA OTCC security checklist.
7 min readModbus Explained: RTU vs TCP, Registers and BMS Integration
What Modbus is, how RTU and TCP differ, how function codes, registers and word order work, how to wire RS-485, and how to bring Modbus devices into a BMS.
7 min readKNX Explained: How a KNX System Works in Villas and Buildings
What KNX is, how the bus, group addresses and ETS work, what KNX Secure protects, how KNX links to a BMS, what drives cost, plus a sample villa plan.
7 min readPut this into practice
Talk to puniq's engineers about a vendor-neutral path for your building.