← How-Tos
raspberry-pi 2 days ago ◑ 4 views ◯ 6 min read

MQTT Sparkplug B for Shop-Floor IIoT: Structured Namespaces and Report-by-Exception on a Raspberry Pi Broker

mqttsparkplug biiotmosquittoraspberry pinode-redbirth death certificateprotocol buffershome assistant

If you've wired up a handful of ESP32 sensors or Modbus-connected shop devices and published their readings to MQTT, you've probably ended up with a topic tree that grew organically and a set of plain-text or loosely-structured JSON payloads that made sense at the time but are awkward to query or extend six months later. Sparkplug B is a published MQTT payload and topic-namespace specification, originally built for industrial SCADA and IIoT systems, that solves exactly this problem: a defined topic structure, a compact binary payload format (Protocol Buffers rather than ad hoc JSON), and — the part that actually matters day to day — built-in "birth" and "death" certificates so your dashboard always knows definitively whether a device is online, offline, or has lost its connection, without guessing from a stale last-seen timestamp.

Why Bother When Plain MQTT Already Works

Plain MQTT with ad hoc topics and JSON payloads is genuinely fine for a handful of devices, and nothing here is mandatory. Sparkplug B earns its complexity once you have enough devices and enough dashboard logic depending on them that these specific problems start to bite:

Sparkplug B's Topic Namespace

Sparkplug defines a fixed topic structure: spBv1.0/group_id/message_type/edge_node_id/[device_id]. In a shop context:

Setting Up a Raspberry Pi as a Sparkplug Edge Node

  1. Run a standard MQTT broker (Mosquitto, already common on these self-hosted Pi setups) — Sparkplug B is a convention layered on top of ordinary MQTT, not a different broker or protocol, so your existing broker works unchanged.
  2. Rather than rewriting every existing ESP32 sensor's firmware to speak native Sparkplug/Protobuf (more work than it's worth for a simple sensor), run a bridging script on the Pi using a Sparkplug-compliant Python library (such as the pysparkplug or similar community libraries implementing the Eclipse Tahu reference payload format) that subscribes to your existing plain-MQTT sensor topics and republishes them into proper Sparkplug B NDATA messages under a defined group/node/device structure.
  3. On startup, the bridge publishes an NBIRTH message declaring the edge node is online along with the full metric set (every known device and its current values), then a DBIRTH per device if you're modeling individual sensors as Sparkplug devices rather than flattening everything into node-level metrics.
  4. Configure the bridge's MQTT connection with a proper last-will-and-testament message set to the NDEATH payload for that edge node — this is what makes the broker itself publish the death certificate immediately on a dropped TCP connection, without the bridge script needing to detect and announce its own failure (which obviously can't happen if the failure is severe enough to kill the process entirely).
  5. Point your dashboard (Node-RED, Grafana via an MQTT-to-InfluxDB bridge, or a Sparkplug-aware SCADA-style tool like Ignition's free trial tier for experimentation) at the Sparkplug topic namespace and subscribe to birth/death and data messages per the spec rather than treating it as arbitrary JSON.

Report-by-Exception in Practice

Once birthed, a device only needs to publish an NDATA/DDATA message when a metric's value actually changes beyond whatever deadband you configure (a temperature sensor might only republish on a 0.5°C change, for instance, rather than every single reading even if the value is identical). This is a meaningful bandwidth and log-volume reduction on a shop network with many sensors polling frequently, and it also makes your historical data genuinely reflect "when did this value change" rather than a constant stream of repeated identical readings that have to be filtered out downstream anyway.

Where This Is and Isn't Worth Adopting

For two or three ESP32 sensors feeding a single Home Assistant dashboard, plain MQTT topics remain the simpler, more directly supported path (Home Assistant's own MQTT discovery convention solves a similar "is this device really online" problem in a different, HA-specific way). Sparkplug B earns its keep once you're building something closer to a genuine shop-floor monitoring system — multiple machines, multiple sensor types, a dashboard that needs to reliably distinguish "device offline" from "nothing changed," and enough scale that reducing redundant traffic actually matters. It's also the right choice if you ever want your home shop monitoring to interoperate with real industrial SCADA tooling, since Sparkplug B is an actual industry specification (maintained by the Eclipse Foundation's Sparkplug working group) rather than a hobbyist convention, and tools built for real industrial deployments will understand it natively.

Practical Notes

Sparkplug B is overhead you don't need for a couple of sensors, but it's a genuinely well-designed solution to the specific "is this thing actually online" and "stop re-publishing unchanged data" problems that start to matter once a shop monitoring setup grows past a handful of devices, and building the bridge yourself on a Raspberry Pi is a solid way to actually understand the specification rather than just configuring someone else's platform.