Lessons in this course (10)
4. MQTT, Sparkplug B and the Unified Namespace
MQTT — lightweight publish/subscribe protocol over TCP (1883, TLS 8883). Clients publish to topics; the broker delivers to subscribers.
| Feature | Meaning |
|---|---|
| Topics | Hierarchical strings: acme/pune/assembly/line1/robot2/temperature |
| Wildcards | + one level (acme/+/assembly/#), # all remaining levels |
| QoS 0 / 1 / 2 | At most once / at least once / exactly once |
| Retained message | Broker keeps the last value for new subscribers |
| Last Will & Testament | Broker publishes it if a client disconnects unexpectedly (device offline) |
| Persistent session | Broker queues messages for a client while it is offline |
| MQTT 5 | Reason codes, user properties, shared subscriptions, message expiry |
Sparkplug B (Eclipse) adds an industrial standard on top of MQTT: fixed topic namespace (spBv1.0/group/NDATA/edge-node/device), Protobuf payloads with metrics, birth/death certificates (NBIRTH/NDEATH) so subscribers know state, and report-by-exception.
Unified Namespace (UNS) — a single, event-driven hub (usually an MQTT broker) where every system publishes its current state under an ISA-95-structured topic tree, and every consumer subscribes to what it needs — replacing point-to-point integrations.
// Publish a machine state with MQTTnet (v4/v5 style)
var client = new MqttClientFactory().CreateMqttClient();
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("broker.plant.local", 8883).WithTlsOptions(o => o.UseTls())
.WithClientId("line1-edge").WithWillTopic("acme/pune/assembly/line1/edge/status").WithWillPayload("offline").WithWillRetain().Build());
var json = JsonSerializer.Serialize(new { state = "Running", speed = 42.5, ts = DateTime.UtcNow });
await client.PublishAsync(new MqttApplicationMessageBuilder()
.WithTopic("acme/pune/assembly/line1/filler/state").WithPayload(json)
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce).WithRetainFlag().Build());MQTT vs OPC UA: OPC UA = rich information model and client/server access to machines (great at Level 1–2); MQTT = lightweight pub/sub for moving data to many consumers and the cloud. They are complementary (and OPC UA PubSub can run over MQTT).