Getting Started with MQTT: A Beginner’s Guide
MQTT is a lightweight messaging protocol, which is widely used for IoT applications, thanks to its low overhead and scalability. MQTT’s publish/subscribe model makes it ideal for AUTOSOL’s solutions, especially when dealing with large-scale IoT networks where devices and applications need to communicate seamlessly. We’ll walk through the basics, including core concepts, topic structure, message payloads, and much more.
What Is MQTT?
MQTT (Message Queuing Telemetry Transport) is a lightweight, broker-based protocol designed to operate over TCP/IP. It uses a publish/subscribe (Pub/Sub) model, making it ideal for one-to-many messaging in IoT networks where devices can exchange data in near real-time.
Key Features of MQTT
- Lightweight: Optimized for minimal network and computing resources.
- Broker-Based: Relies on an intermediary server (broker) to manage data flow.
- Pub/Sub Model: Supports a highly scalable one-to-many messaging model.
- Data Agnostic: Can handle any payload type, allowing flexibility in message formats.
Key Components of MQTT
MQTT Client
An MQTT client is any device or application that can send and/or receive messages using an MQTT library.
MQTT Broker
The broker is a program that runs on a more powerful machine, managing client connections and facilitating message distribution. The broker:
- Authenticates and authorizes MQTT clients.
- Manages client sessions.
- Receives, filters, and forwards messages to relevant subscribers.
Why Choose MQTT? Pub/Sub vs. Poll/Response
The Pub/Sub model provides several advantages over a traditional poll/response approach, especially in IoT and unreliable network environments.
Pub/Sub
- Network Efficiency: MQTT easily allows implementation of Report By Exception, which conserves bandwidth.
- Scalability: Allows multiple consumers to subscribe to the same topic without disrupting the source.
- Reliability: Suitable for environments with intermittent connectivity.
Poll/Response
- Tightly Coupled: Data gets contextualized in your SCADA system and usually has to funnel out to other places from there.
- Inefficiency: Requires continuous polling, even if data hasn’t changed, consuming unnecessary resources.
Understanding MQTT Topic Structure
MQTT topics are structured as strings, organized in hierarchical levels separated by forward slashes. For example: building/floor4/lobby/securitycamera. This approach enables easy categorization of data across different topics.
Topic Filtering and Wildcards
- Multi-Level Wildcard (#): Matches multiple levels and can only be used at the end. E.g., building/floor4/lab/#
- Single-Level Wildcard (+): Matches a single level. E.g., building/floor4/+/temperature
MQTT Payloads
MQTT payloads can be of various formats, such as raw binary/hex, JSON, XML, or Protobuf, depending on the requirements of the application. However, payload variety comes with maintenance overhead, and care should be taken to manage payload types for efficiency and reliability.
Payload Best Practices
- Be consistent with data format across similar topics.
Consider using standards like Sparkplug for better interoperability. AUTOSOL’s ACM supports Sparkplug, and eACM supports Sparkplug, Raw, and JSON payloads.
Understanding MQTT Sparkplug
Sparkplug is a protocol specification, managed by the Eclipse Foundation, that builds upon the MQTT standard, customizing it specifically for industrial applications. It defines a set of rules and guidelines for structuring MQTT payloads, making it easier to work with data in industrial settings.
Key features of MQTT Sparkplug include:
Backfill: Sparkplug allows for messages to be stored in the field and sent up the network during communication outages assuring every data point makes it to its destination.
Device Registration: Sparkplug facilitates automatic device registration and management, reducing the complexity of onboarding new devices into the network.
Birth and Death Certificates: It ensures that a device’s status (birth) and the notification of its disconnection (death) are automatically communicated, improving network reliability.
Payload Definition: Sparkplug defines a standardized format for payloads, making it easier for applications to interpret and act upon the data.
Quality of Service (QoS)
QoS is an agreement that defines how messages are sent and ensures reliable communication. MQTT allows clients to specify the desired Quality of Service (QoS) level for messages, which controls delivery guarantees.
- QoS 0 – At Most Once: Messages are sent without acknowledgment, and message loss may occur.
- QoS 1 – At Least Once: Messages are guaranteed to arrive but may be duplicated. (Brokers will handle this.)
- QoS 2 – Exactly Once: Messages are delivered exactly once with additional “handshaking” between client and broker.
Session Management in MQTT
Session management is crucial in MQTT for managing connections and tracking message states. MQTT offers various session options:
- Non-Persistent Session: Requires clients to reconnect and resubscribe upon disconnecting, ideal for simple applications. (Costly on smaller devices.)
- Persistent Session: Stores session data on the broker, which is useful for devices needing to resume from where they left off. (Client ID, subscription topics, and retained messages)
- Clean Session: Discards queued messages upon disconnection, suitable for clients that only publish data and don’t need offline storage.
Last Will & Testament (LWT)
LWT is a feature that lets a client notify others if it unexpectedly disconnects, by publishing a predefined message to a specified topic.
Connection Reliability: Keep Alive Mechanism
To maintain connection health, MQTT uses a keep-alive mechanism involving PINGREQ/PINGRESP messages that verify a live connection between client and broker. This feature ensures that clients can detect when a connection is lost.
Test Clients for MQTT Development
Many MQTT test clients are available to help developers simulate device messaging and understand protocol behavior. These clients support testing of publish/subscribe operations, topic structures, QoS settings, and more.
Writing to a Topic
In MQTT, writing to a topic simply involves publishing a message to that topic. Each message contains:
- Topic name
- Payload (data)
- QoS level
- Optional LWT settings for connection health notifications
AUTOSOL’s solutions are designed to leverage MQTT’s efficient Pub/Sub model, helping industrial enterprises achieve scalable, reliable IoT networks with minimal overhead. Learn how our applications can streamline your IoT integration, maximize data availability, and enhance connectivity across your ecosystem. Contact us today!