MQTT Sparkplug B for Shop-Floor IIoT: Structured Namespaces and Report-by-Exception on a Raspberry Pi Broker
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:
- Knowing a device is actually offline, not just quiet: a sensor publishing every 30 seconds that goes silent could be offline, could be stuck, or could just have nothing new to report. Sparkplug's birth/death certificate mechanism uses MQTT's own last-will-and-testament feature to publish a definitive "this device just went offline" (NDEATH) message the instant the broker detects a dropped connection, rather than your dashboard inferring staleness from a timeout you picked somewhat arbitrarily.
- Report-by-exception instead of constant re-publishing: Sparkplug's data model publishes a device's full metric set once on "birth," then only publishes changed values afterward (NDATA messages carry deltas), which meaningfully reduces bus traffic on a shop network with many sensors compared to every device re-publishing its entire state on every interval regardless of whether anything changed.
- A consistent, parseable topic and payload structure across every device type, rather than each project inventing its own JSON schema — valuable mainly once you have enough different device types that "what does this topic's payload actually look like" stops being something you can keep in your head.
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:
- group_id: a logical grouping, such as
Workshopor a specific area likeCNCBay. - message_type: one of a fixed set — NBIRTH/NDEATH (node birth/death), DBIRTH/DDEATH (device birth/death), NDATA/DDATA (data updates), NCMD/DCMD (commands to the device).
- edge_node_id: typically your gateway — in a shop setup, often a Raspberry Pi running a Sparkplug-aware MQTT client that bridges your existing ESP32/Modbus sensors into the Sparkplug namespace.
- device_id: an individual sensor or machine attached to that edge node.
Setting Up a Raspberry Pi as a Sparkplug Edge Node
- 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.
- 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
pysparkplugor 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. - 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.
- 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).
- 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 payloads are Protocol Buffers, not human-readable JSON — budget for a proper Sparkplug-aware client or dashboard tool rather than expecting to eyeball payloads with a generic MQTT explorer the way you might with plain JSON topics.
- Keep your bridge script's reconnection logic solid; because Sparkplug's offline detection depends on the broker's last-will mechanism firing correctly, a bridge that silently stops without a clean MQTT disconnect (a hard crash, a killed process) is exactly the scenario the last-will message is designed to catch, so test that failure mode deliberately rather than assuming it works.
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.