Home / Insights

BMS & Controls

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.

puniq EngineeringUpdated 7 min read

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.

QoSGuaranteeOverheadUse it in a building for
0At most once: sent, never acknowledgedLowestFrequent telemetry (temperatures, CO2) where the next reading replaces a lost one
1At least once: acknowledged, may arrive twiceModerateAlarms, status, meter totals and commands, designed so a duplicate does no harm
2Exactly once: four-step handshakeHighestRare: 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 featureWhat it fixes in a building
Reason codes on every acknowledgementYou see why a publish or subscription was refused instead of guessing
Message expiry and session expiryA 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 propertiesMetadata such as source or unit travels with the message, by your own convention
Enhanced authenticationChallenge/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:

MQTTBACnetModbusREST API
PatternPublish/subscribe through a brokerClient/server, plus change-of-value subscriptionsMaster polls slave registersRequest/response over HTTP
Data modelNone: the payload is just bytesStandard objects with units, alarms, schedulesNumbered 16-bit registers and coils, no namesWhatever the vendor's API defines
Typical transportTCP 1883/8883, WebSocketBACnet/IP (UDP 47808), MS/TP on RS-485Modbus TCP (502), RTU on RS-485HTTPS
Security built inTLS and broker ACLs, if configuredLittle in classic BACnet; BACnet/SC adds TLSNone in the classic protocolHTTPS plus tokens or keys
Best job in a buildingMoving data out to historians, analytics and cloudControlling and supervising HVAC and plantReading meters, drives and packaged equipmentLinking business apps such as a CMMS
Weak spotNo semantics; naming is up to youHeavier, broadcast traffic, legacy securityNo metadata, polling onlyPolling, 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.

  1. Field devices: IAQ and occupancy sensors, meters, drives and chillers on BACnet, Modbus, wireless or MQTT.
  2. Controllers and BMS: keep running every control loop and interlock.
  3. Edge gateway: reads BACnet and Modbus, normalises names and units, publishes MQTT, and buffers data when a link drops.
  4. Broker: on-premises or at the edge, bridged to a cloud broker only if needed.
  5. Historian: a time-series database subscribing with a persistent session at QoS 1, so restarts lose nothing.
  6. 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.

TopicCarriesQoSRetainWho may publish
ryd01/twr-a/l12/hvac/ahu-12-1/satSupply air temperature0YesEdge gateway
ryd01/twr-a/l12/hvac/ahu-12-1/sat-spActive setpoint (state)1YesEdge gateway
ryd01/twr-a/l12/hvac/ahu-12-1/alarm/filter-dpFilter alarm, active or cleared1YesEdge gateway
ryd01/twr-a/l12/iaq/zone-n/co2Zone CO2 in ppm0YesSensor gateway
ryd01/twr-a/b1/plant/ch-01/kwChiller electrical power0YesEdge gateway
ryd01/twr-a/b1/elec/mdb-1/kwhMain meter, cumulative energy1YesMeter gateway
ryd01/status/gw-b1-01Gateway online or offline (last will)1YesThat gateway only
ryd01/cmd/twr-a/l12/hvac/ahu-12-1/sat-spSetpoint change request1NeverApproved 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.

CheckPass mark
Broker locationOn-premises or at the edge; a cloud broker only as a bridge
EncryptionTLS on every connection; port 1883 closed outside the test bench
Access listsGateways write only their own branch; command topics writable by named applications only
NamespaceA documented, versioned dictionary covering 100% of published points
TraceabilityEvery topic mapped to its source BACnet object or Modbus register
Session awarenessLast will on every gateway; keep-alive of 60 seconds or less
BufferingStore-and-forward on gateways for at least 24 hours of data
Exit testDevices 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.

Put this into practice

Talk to puniq's engineers about a vendor-neutral path for your building.