MQTT communication

Comprehensive study notes, diagrams, and exam preparation for MQTT communication.

MQTT Communication

Definition

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. Designed for constrained devices with low bandwidth or unreliable networks, it is the standard messaging protocol for the Internet of Things (IoT).


Main Content

1. Publish/Subscribe Architecture

  • Unlike the traditional Client-Server model (where a client talks directly to a server), MQTT uses a broker to decouple the sender (publisher) from the receiver (subscriber).
  • Publishers send messages to a "topic" on the broker, and the broker pushes those messages to any device subscribed to that specific topic.

2. The MQTT Broker

  • The Broker is the central hub that receives all messages, filters them, decides who is interested in them, and sends them to the subscribed clients.
  • It acts as a post office, ensuring that messages reach the intended recipients without the sender needing to know the location or status of the receiver.

3. Topics and Quality of Service (QoS)

  • Topics are strings (e.g., home/livingroom/temperature) that act as a namespace for the broker to organize and route messages.
  • QoS levels define the reliability of the message delivery:
    • QoS 0: At most once (fire and forget).
    • QoS 1: At least once (ensures delivery but may duplicate).
    • QoS 2: Exactly once (ensures delivery with no duplicates).

Working / Process

1. Connection Establishment

  • The MQTT client sends a CONNECT packet to the broker containing credentials and a unique Client ID.
  • The broker validates the connection and responds with a CONNACK packet to signal a successful link.
Client (Publisher)       Broker       Client (Subscriber)
      |                    |                  |
      |---- CONNECT ------>|                  |
      |<--- CONNACK -------|                  |

2. Publishing and Subscribing

  • The subscriber sends a SUBSCRIBE packet to the broker indicating the topic it wants to follow.
  • The publisher sends a PUBLISH packet to the broker containing the message and the topic name.
Publisher sends message "25C" to topic "temp":
      |                    |                  |
      |-- PUBLISH (temp) ->|                  |
      |                    |-- PUBLISH (temp)->|

3. Message Delivery and Termination

  • The broker identifies the active subscribers for the topic and forwards the payload.
  • When the communication is finished, the client sends a DISCONNECT packet to close the session gracefully.

Advantages / Applications

  • Lightweight: Minimal packet overhead makes it perfect for battery-powered sensors and low-power microcontrollers.
  • Real-time Efficiency: The push-based mechanism ensures data is transmitted instantly without the client needing to poll the server.
  • Broad Use: Widely used in home automation (smart lights), industrial IoT (factory sensors), and mobile applications (push notifications).

Summary

MQTT is an efficient, event-driven messaging protocol that uses a broker-based publish-subscribe model to allow IoT devices to communicate over constrained networks. It is valued for its low power consumption, minimal bandwidth usage, and high reliability in unstable network conditions. Important terms to remember include Publisher, Subscriber, Broker, Topic, and QoS (Quality of Service).