Chapter 1: MQTT (Message Queuing Telemetry Transport) Protocol

📚 Chapter 1: MQTT (Message Queuing Telemetry Transport) Protocol

1️⃣ What is MQTT

🌐 MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe messaging protocol designed for low‑bandwidth, high‑latency, or unreliable networks. It is widely used in IoT, embedded systems, and machine‑to‑machine communication.

MQTT is ideal when:

  • Devices have limited CPU, RAM, or battery
  • Network bandwidth is expensive or unstable
  • Many devices need to send small messages efficiently

MQTT was originally created in 1999 to monitor oil pipelines over satellite links — a perfect example of constrained communication.

2️⃣ MQTT Architecture

MQTT uses a publish/subscribe model where clients can publish messages to topics and subscribe to topics to receive messages. The architecture consists of:

  • Publishers: Devices that send messages
  • Subscribers: Devices that receive messages
  • Brokers: Servers that manage message routing

Clients

Any device or program that publishes or subscribes to messages:

  • Sensors
  • Mobile apps
  • Embedded systems
  • Command‑line tools (mosquitto_pub, mosquitto_sub)

Broker

A central server that:

  • Receives all messages
  • Filters them by topic
  • Delivers them to subscribers
  • Manages client sessions, last‑will messages, and retained messages

Examples: Mosquitto, HiveMQ, EMQX.

Publish/Subscribe Analogy

Think of MQTT like radio:

  • A station broadcasts on a frequency (topic)
  • Listeners tune in to that frequency
  • The broadcaster does not know who is listening
flowchart LR
    P1([Publisher]) -->|Publish on Topic| B((MQTT Broker))
    P2([Publisher]) -->|Publish on Topic| B
    B -->|Deliver to Subscribers| S1([Subscriber])
    B -->|Deliver to Subscribers| S2([Subscriber])

3️⃣ MQTT Over TCP/IP

MQTT always runs on top of TCP, which provides:

  • Reliable delivery
  • Ordered packets
  • Connection‑oriented communication

Once connected, clients stay connected until they disconnect or time out.

Keep‑Alive Clients send a keep‑alive ping (default ~60 seconds) so the broker knows they’re still alive.

4️⃣ Client ID and Sessions

Client ID

  • Must be unique
  • Used by the broker to track subscriptions
  • If two clients connect with the same ID, the older one is disconnected

Clean Session

  • Clean Session = True → broker forgets everything when the client disconnects
  • Clean Session = False → broker remembers subscriptions and may store messages

5️⃣ Last Will and Testament (LWT)

A client can register a “last will” message that the broker publishes if the client disconnects unexpectedly. Useful for:

  • Detecting offline sensors
  • Monitoring device health

6️⃣ MQTT Topics

Topics are UTF‑8 strings separated by /, similar to file paths.

Examples:

house/garage/door
nait/wc204/light1
sensors/temperature/livingroom

Rules

  • Case‑sensitive
  • No spaces
  • No wildcards when publishing
  • Topics are created dynamically when used

Topic Hierarchy Diagram

graph TD
    A[house] --> B[garage]
    A --> C[kitchen]
    B --> D[light]
    C --> E[temperature]
    C --> F[humidity]

7️⃣ Topic Wildcards (Subscription Only)

Single‑Level Wildcard: +

Matches exactly one level. Example:

house/+/temperature

Matches:

  • house/kitchen/temperature
  • house/garage/temperature

Does NOT match:

  • house/kitchen/main/temperature

Multi‑Level Wildcard: #

Matches everything from that point downward. Example: house/#

flowchart TD
    A[Subscribe: house/#] --> B(house)
    A --> C(house/garage)
    A --> D(house/garage/light)
    A --> E(house/kitchen/sensor/temp)

8️⃣ Quality of Service (QoS)

MQTT defines three delivery guarantees:

QoS Meaning Guarantee
0 At most once No ACK, no guarantee
1 At least once Guaranteed, duplicates possible
2 Exactly once Guaranteed, no duplicates
sequenceDiagram
    participant C as Client
    participant B as Broker

    Note over C,B: QoS 0
    C->>B: PUBLISH (QoS 0)
    Note over B: No ACK

    Note over C,B: QoS 1
    C->>B: PUBLISH (QoS 1)
    B->>C: PUBACK

    Note over C,B: QoS 2
    C->>B: PUBLISH (QoS 2)
    B->>C: PUBREC
    C->>B: PUBREL
    B->>C: PUBCOMP

9️⃣ Retained Messages

A retained message is stored by the broker and delivered immediately to new subscribers.

Example:

mosquitto_pub -h 127.0.0.1 -t nait/wc204/light1 -m "status:on" -r

🔟 Moskitto Tools

  • Mosquitto provides command‑line tools for testing and debugging:

Start Broker

mosquitto -v

Publish

   
mosquitto_pub -h 127.0.0.1 -t nait/WC204/light1 -m "status:on" -d -r

Subscribe

mosquitto_sub -h 127.0.0.1 -t nait/# -d

Subscribe and Auto‑Disconnect After 3 Messages

mosquitto_sub -h 127.0.0.1 -t nait/# -d -C 3

1️⃣1️⃣ Connecting to Public Broker

You can connect to public brokers like test.mosquitto.org for testing.

Subscribe

mosquitto_sub -h test.mosquitto.org -t nait/esp32/# -d

Publish

mosquitto_pub -h test.mosquitto.org -t nait/esp32/light5 -m "on" -r

Sending a File

mosquitto_pub -h 127.0.0.1 -t nait/filetest -f README.txt

Batch Files

lighton.bat

mosquitto_pub -h test.mosquitto.org -t nait/esp32/light5 -m "on" -r

lightoff.bat

mosquitto_pub -h test.mosquitto.org -t nait/esp32/light5 -m "off" -r

1️⃣2️⃣ Troubleshooting

Common issues:

  • Duplicate Client IDs → disconnect loops
  • Wrong topic case → no messages
  • No retained message → subscriber sees nothing on connect
  • Firewall blocking port 1883
  • Using wildcards when publishing (not allowed)