Modul 4 von 9 · 📖 4 min Lesezeit · ⏱ 30 min gesamt

FI-DV 04 MQTT und Sparkplug B (EN)

Inhaltsverzeichnis (6 Abschnitte)
  1. Concepts and Background
  2. Architecture Diagram
  3. Practical Steps
  4. Common Pitfalls
  5. Further Resources
  6. Knowledge Check

FI-DV 04 MQTT and Sparkplug B

MQTT (Message Queuing Telemetry Transport) is a lightweight Publish/Subscribe protocol designed for efficient data transmission in networked systems. In this module, you will learn the fundamentals of MQTT architecture, understand the different Quality-of-Service levels, and master handling Retained Messages. You will gain insights into security concepts for MQTT installations and learn about the specifics of the Sparkplug B standard for standardizing industrial applications.

The practical application is in focus: You will configure an MQTT broker, implement client applications, and integrate Sparkplug B for seamless communication between devices and applications. Upon completion of this module, you will be able to plan and implement MQTT-based solutions for industrial automation and IoT applications.

Concepts and Background

MQTT Broker
The central instance that receives all messages from publishers and forwards them to the appropriate subscribers. The broker manages the topics and establishes the connection between the clients.
Quality of Service (QoS)
Three levels (0, 1, 2) that define the reliability of message transmission. QoS 0 provides "at most once" delivery, QoS 1 "at least once" and QoS 2 "exactly once" with increased overhead.
Retained Messages
Messages stored by the broker that are sent to new subscribers that subscribe to a topic. This ensures that new clients immediately receive the current state.
Sparkplug B
An open standard that extends MQTT with semantic structures. It defines a fixed topic format and message schema for monitoring, control, and data acquisition in industrial applications.
Willow-Way Topic Hierarchy
The standardized topic structure of Sparkplug B, which allows a clear separation between metadata (NDATA), payload data (DDATA) and commands (CMD) and enables hierarchical organization of devices.

Architecture Diagram

flowchart TD
    A[Edge Device] -->|Publish/Subscribe| B[MQTT Broker]
    C[SCADA System] -->|Subscribe| B
    D[Historian] -->|Subscribe| B
    E[IIoT Platform] -->|Publish/Subscribe| B
    F[Human Machine Interface] -->|Subscribe| B
    B -->|Sparkplug B| G[Application Server]
    
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:2px
    style G fill:#bfb,stroke:#333,stroke-width:2px

Practical Steps

  1. Install the Eclipse Mosquitto MQTT broker on a Linux system with the command
    sudo apt install mosquitto mosquitto-clients
    . This provides the basic infrastructure for MQTT communication.
  2. Configure the broker's security by creating a password file with
    mosquitto_passwd -c /etc/mosquitto/passwortfile username
    and adjusting the configuration in
    listener 1883
    allow_anonymous false
    password_file /etc/mosquitto/passwortfile
    . This protects the broker from unauthorized access.
  3. Create a test environment with two terminals: One for the publisher (
    mosquitto_pub -h localhost -u username -P password -t test/topic -m "Hello MQTT"
    ) and one for the subscriber (
    mosquitto_sub -h localhost -u username -P password -t test/topic
    ). This verifies the basic functionality.
  4. Implement Sparkplug B compatibility by installing the library
    pip install sparkplugbpayload
    and creating a Node-Birth packet with
    from sparkplugbpayload import SparkplugBPayload
    payload = SparkplugBPayload("NBIRTH")
    payload.add_metric("Node Control/Rebirth", "boolean", False)
    payload.serialize()
    . This initializes a Sparkplug-capable device.
  5. Configure Retained Messages for critical state data with
    mosquitto_pub -r -t device/status -m "online"
    . This ensures that new clients immediately receive the current device status.
  6. Test the different QoS levels by sending messages with
    mosquitto_pub -q 0 -t qos/test -m "QoS 0"
    ,
    mosquitto_pub -q 1 -t qos/test -m "QoS 1"
    and
    mosquitto_pub -q 2 -t qos/test -m "QoS 2"
    and observing the behavior in the subscriber. This helps in selecting the appropriate QoS for your application.

Common Pitfalls

Further Resources

Knowledge Check

Four questions for self-assessment. Click on each question to see the correct answer and explanation.

What is the main task of an MQTT broker in a Publish/Subscribe system?
  • A) Ensure encryption of all messages
  • B) Receive messages from publishers and forward them to appropriate subscribers
  • C) Perform authentication of all clients in the network
  • D) Guarantee data persistence for all messages

Correct Answer: B. The broker acts as an intermediary that forwards messages based on the topics a