Sparkplug B Protocol¶
Reference for Sparkplug B v1.0 implementation.
Topic Structure¶
Components¶
- spBv1.0: Protocol version
- group_id: Sparkplug group (logical grouping)
- message_type: NBIRTH, NDEATH, DBIRTH, DDEATH, DDATA, DCMD, NCMD
- edge_node_id: This gateway's ID
- device_id: Modbus device ID (omit for node messages)
Message Types¶
NBIRTH (Node Birth)¶
Published: When edge node connects to broker
Topic:
Contains: - Node metrics - bdSeq = 0 - Rebirth metric - Node Control/Metrics
NDEATH (Node Death)¶
Published: - Gracefully: Before disconnect - LWT: By broker on unexpected disconnect
Topic:
Contains: - bdSeq = 0
DBIRTH (Device Birth)¶
Published: When device starts polling
Topic:
Contains: - All tag definitions - Metric aliases - Data types - seq = 0
DDATA (Device Data)¶
Published: Tag values on each poll
Topic:
Contains: - Tag values (by alias) - Timestamp - Incrementing seq
DCMD (Device Command)¶
Received: Write commands from SCADA
Topic:
Contains: - Metric name - Value to write - Data type
NCMD (Node Command)¶
Received: Node control commands
Topic:
Commands: - Rebirth request
Sequence Numbers¶
bdSeq (Birth/Death Sequence)¶
- Always = 0 for NBIRTH/NDEATH
- Never incremented
- Fixed value per Sparkplug spec
seq (Message Sequence)¶
- Starts at 0 on DBIRTH
- Increments with each DDATA
- Wraps at 256
- Used to detect missing messages
Metric Aliasing¶
DBIRTH¶
Defines all metrics with aliases:
DDATA¶
References by alias only:
Benefits: - Reduced message size - Lower bandwidth
Data Types¶
| Sparkplug Type | Description |
|---|---|
| Int8, Int16, Int32, Int64 | Signed integers |
| UInt8, UInt16, UInt32, UInt64 | Unsigned integers |
| Float | 32-bit float |
| Double | 64-bit float |
| Boolean | True/false |
| String | Text |
| DateTime | Timestamp |
| Text | UTF-8 text |
Best Practices¶
- Always publish NBIRTH on connect
- Use LWT for unexpected disconnects
- Increment seq properly
- Use metric aliases
- Include timestamps
- Validate DCMD before writing
Example Flow¶
- Gateway connects to MQTT
- Publish NBIRTH
- Start device polling
- Publish DBIRTH with all tags
- Publish DDATA with values
- On disconnect, publish NDEATH