30  MQTT Implementation Basics

In 60 Seconds

Getting started with MQTT requires three components: a broker (message server like Mosquitto), a publisher (sends data to topics), and a subscriber (receives data from topics). This chapter walks through building your first MQTT application using either browser-based Wokwi simulators requiring no hardware setup, or Python scripts with the paho-mqtt library.

30.1 Learning Objectives

By the end of this chapter, you will be able to:

  • Explain MQTT System Components: Describe the role of each of the three essential parts (broker, publisher, subscriber) and how they connect to form a complete IoT messaging system
  • Demonstrate Browser Simulators: Execute MQTT code in Wokwi simulators without any hardware setup, observing publish-subscribe message flow in real time
  • Implement an MQTT Publisher: Construct a Python script that sends sensor data to an MQTT broker using the paho-mqtt library
  • Implement an MQTT Subscriber: Construct a Python script that receives and displays MQTT messages, including threshold-based alerting logic
  • Select Your Learning Path: Evaluate the trade-offs between simulator-based learning and real hardware projects, then justify your choice based on available resources and goals

Key Concepts

  • MQTT: Message Queuing Telemetry Transport — pub/sub protocol optimized for constrained IoT devices over unreliable networks
  • Broker: Central server routing messages from publishers to all matching subscribers by topic pattern
  • Topic: Hierarchical string (e.g., home/bedroom/temperature) used to route messages to interested subscribers
  • QoS Level: Quality of Service 0/1/2 trading delivery guarantee for message overhead
  • Retained Message: Last message on a topic stored by broker for immediate delivery to new subscribers
  • Last Will and Testament: Pre-configured message published by broker when a client disconnects ungracefully
  • Persistent Session: Broker stores subscriptions and pending messages allowing clients to resume after disconnection

30.2 Prerequisites

Before diving into this chapter, you should be familiar with:

  • MQTT Fundamentals: Understanding MQTT architecture, publish-subscribe patterns, topics, and basic protocol operations is essential before implementing practical applications
  • MQTT QoS and Session: Knowledge of Quality of Service levels (QoS 0, 1, 2) and session management (clean vs persistent) helps in choosing appropriate configurations for different use cases
  • Programming fundamentals: Basic experience with Python helps work through the code examples

This chapter connects to multiple learning resources:

Interactive Practice:

  • Simulations Hub - Try 3 live MQTT simulators (ESP32 publisher, subscriber, QoS tester) directly in your browser without hardware
  • Network Topology Explorer - Understand how MQTT fits into Star, Mesh, and Tree topologies

Video Tutorials:

  • Videos Hub - Watch step-by-step MQTT setup tutorials, broker configuration guides, and ESP32 programming demonstrations

Self-Assessment:

  • Quizzes Hub - Test your MQTT implementation knowledge with interactive scenario-based questions (QoS selection, security configuration, debugging workflows)

Knowledge Reinforcement:

  • Knowledge Gaps Hub - Address common MQTT misconceptions (clean_session behavior, QoS guarantees, retained message timing)

Pro Tip: Start with the browser simulators (Simulations Hub) to experiment risk-free, then progress to real hardware using the code examples in this chapter. Use the Knowledge Gaps Hub to avoid common pitfalls like forgetting client.loop() or misunderstanding QoS levels.

30.3 Getting Started (For Beginners)

New to Building MQTT Applications? Start Here!

Time: ~20 min | Level: Beginner | Unit: P09.C26.U01

This section is designed for beginners. If you’re already familiar with setting up MQTT clients and brokers, feel free to skip to the interactive labs below.

“Let’s build something real!” said Max the Microcontroller excitedly. “Step one: install a broker. Mosquitto is free and runs on almost anything – even a Raspberry Pi.”

Sammy the Sensor followed along: “Step two: write a publisher. In Python, it’s just five lines of code – import the library, connect to the broker, publish a message to a topic, done! I published my first temperature reading in under a minute.”

“Step three: write a subscriber!” added Lila the LED. “Another five lines – connect, subscribe to the topic, and define a callback function that runs whenever a message arrives. I had my LED changing color based on Sammy’s readings in less than 10 minutes.”

Bella the Battery encouraged everyone: “Don’t overthink it – just start! Install Mosquitto, open two terminal windows, use one to publish and one to subscribe. When you see your first message appear on the subscriber side, you’ll get that ‘aha!’ moment. Then you can add QoS, retained messages, and all the fancy features later.”

30.3.1 What Will You Learn in This Chapter? (Simple Explanation)

In previous chapters, you learned what MQTT is (the theory). In this chapter, you’ll learn how to actually build MQTT applications (the practice).

Analogy: It’s like learning to drive a car.

  • Previous chapters = Driving theory (how gears work, traffic rules, car parts)
  • This chapter = Getting behind the wheel and actually driving!

30.3.2 The Three Essential MQTT Components

Every MQTT system needs three parts:

Component Role Simple Analogy Your Implementation
MQTT Broker Message post office Central mailbox at your street Mosquitto, HiveMQ, test.mosquitto.org
Publisher Sends messages Someone dropping letters in mailbox ESP32 with temperature sensor
Subscriber Receives messages Someone checking mailbox for letters Python dashboard on your laptop
Quick Check: MQTT Components

For an MQTT broker handling \(N\) devices each publishing at rate \(r\) messages/sec to \(T\) topics with \(S\) subscribers per topic:

Broker input rate: $ R_{} = N r $

Broker output rate (fan-out to all subscribers): $ R_{} = N r S $

Example (1000 sensors, 0.1 msg/sec, 3 subscribers each):

  • Input: \(R_{\text{in}} = 1000 \times 0.1 = 100\text{ msg/sec}\)
  • Output: \(R_{\text{out}} = 1000 \times 0.1 \times 3 = 300\text{ msg/sec}\)
  • Fan-out factor: \(\frac{R_{\text{out}}}{R_{\text{in}}} = S = 3\times\)

Broker CPU load formula (simplified): $ R_{} + 0.005 N_{} $

For 1000 devices @ 100 msg/sec with 3× fan-out: $ + 0.005 = 3 + 5 = 8% $

Scaling insight: Doubling message rate doubles CPU load (linear in \(R\)), but doubling devices increases both \(R\) and connection overhead. At 10,000 devices publishing 0.1 msg/sec: - Input: 1000 msg/sec - Output (3 subscribers): 3000 msg/sec - CPU: \(0.01 \times 3000 + 0.005 \times 10,000 = 80\%\) (needs cluster)

Lesson: Broker load scales with (message rate × subscriber count). High fan-out (many subscribers per topic) requires more broker resources than high device count alone.

Simple flow:

ESP32 (Publisher)  →  Broker (test.mosquitto.org)  →  Your Laptop (Subscriber)
    "Temp: 24°C"           [Routes message]              "Display: 24°C"

30.3.3 Your First MQTT Project: What You’ll Build

Goal: Create a complete IoT temperature monitoring system.

What happens:

  1. ESP32 reads temperature from DHT22 sensor every 10 seconds
  2. ESP32 publishes temperature to topic: iot/sensors/temperature
  3. Broker (test.mosquitto.org) receives and stores the message
  4. Your laptop subscribes to iot/sensors/temperature and displays readings
  5. Bonus: Alert system triggers when temperature > 25C

Visual flow:

MQTT publish-subscribe flow
Figure 30.1: ESP32 temperature monitoring with MQTT publish-subscribe flow
Lab setup components
Figure 30.2: Lab setup components

This diagram shows the three-layer lab architecture: hardware (ESP32 + sensor), network (Wi-Fi + MQTT broker), and application (monitoring tools).

30.3.4 Two Ways to Try MQTT (Choose Your Path)

Path 1: Browser Simulator (Easiest)

  • No hardware needed
  • Works immediately
  • Perfect for learning
  • Not real sensors

How: Scroll down to the “Interactive Simulator” section and click Play!

Path 2: Real Hardware (Best for learning)

  • Real sensors (actual temperature readings)
  • Full IoT experience
  • Can deploy anywhere
  • Requires ESP32 + DHT22 sensor

Hardware needed:

  • ESP32 DevKit v1 ($4-7 on Amazon/AliExpress)
  • DHT22 temperature sensor ($3-5)
  • Breadboard + jumper wires ($5 for kit)
  • USB cable (usually included with ESP32)

Total cost: ~$12-20 (reusable for many projects)

30.3.5 Common Beginner Questions

Q: Do I need to install an MQTT broker on my computer?

A: No! You can use free public brokers like: - test.mosquitto.org (most popular) - broker.hivemq.com - mqtt.eclipseprojects.io

These are perfect for learning and testing. For production projects, you’ll want your own broker.

Q: What’s the difference between ESP32 code and Python code?

A: They do different jobs: - ESP32 code (C/C++): Runs on the microcontroller near your sensor (publisher) - Python code: Runs on your computer to display data (subscriber)

Both talk to the same MQTT broker!

Q: Can multiple devices subscribe to the same topic?

A: Yes! That’s the beauty of MQTT. You can have: - Your laptop showing temperature on a dashboard - Your phone app showing the same data - A database logger saving historical data - An alert system checking for high temperatures

All subscribed to iot/sensors/temperature simultaneously!

Q: What if my Wi-Fi disconnects?

A: MQTT is designed for unreliable networks: - The broker stores messages (if you use QoS > 0) - Your client will automatically reconnect - You won’t lose critical data

This is why MQTT is perfect for IoT (where Wi-Fi isn’t always reliable).

30.3.6 Self-Check: Are You Ready?

Question: You want to build a soil moisture monitor that sends alerts to your phone when plants need watering. Which component runs where?

Click to see the answer

Answer:

Publisher (ESP32 in the garden):

  • Reads soil moisture sensor
  • Publishes to topic: garden/moisture/plant1
  • Example message: {"moisture": 45, "status": "needs_water"}

Broker (in the cloud):

  • test.mosquitto.org or your own broker
  • Receives messages from ESP32
  • Routes to subscribers

Subscriber (on your phone):

  • Python script or mobile app
  • Subscribes to garden/moisture/plant1
  • Triggers push notification when moisture < 50%

Why this works:

  • ESP32 stays near the plant (wired to sensor)
  • Your phone can be anywhere with internet
  • Broker bridges them across the internet
  • Multiple family members can all get alerts (multiple subscribers)
Common Mistake: Using test.mosquitto.org in Production

The Mistake: A developer builds a prototype using the public MQTT broker test.mosquitto.org, gets excited about how well it works, and deploys 50 sensors to a client’s greenhouse without changing the broker.

Why This Is Dangerous:

  1. Zero Authentication: Anyone can subscribe to garden/moisture/plant1 and see your data
  2. Zero Encryption: WiFi sniffing reveals all messages in plain text
  3. No Reliability Guarantee: Public brokers regularly restart, losing connections and retained messages
  4. Topic Collisions: Another developer might use the same topic name, creating data mix-ups

Real-World Consequences:

  • Competitor subscribes to your topics and reverse-engineers your IoT product design
  • Retained messages from someone else’s testing pollute your production topics
  • Broker outage during critical frost event causes $15,000 crop loss

The Fix: Spend 30 minutes setting up a private broker:

# Install Mosquitto on Ubuntu/Raspberry Pi
sudo apt install mosquitto mosquitto-clients

# Create user account
sudo mosquitto_passwd -c /etc/mosquitto/passwd myuser

# Configure /etc/mosquitto/mosquitto.conf
allow_anonymous false
password_file /etc/mosquitto/passwd
listener 8883
cafile /etc/ssl/certs/ca.crt
certfile /etc/ssl/certs/server.crt
keyfile /etc/ssl/private/server.key

# Restart broker
sudo systemctl restart mosquitto

Cost Comparison:

  • Public broker: Free, but one security breach costs $10,000+ (average breach cost for small IoT deployment)
  • Private VPS: $5/month ($60/year) = 0.6% of potential breach cost
  • AWS IoT Core: Pay-per-message, includes TLS, device auth, and ACLs automatically

Bottom Line: Use public brokers ONLY for learning/prototyping. Production deployments need private brokers with authentication and TLS.


Ready to build? The sections below provide three complete implementations:

  1. Interactive Simulators - No hardware, start immediately
  2. Python Examples - Publisher/Subscriber code you can run on your laptop
  3. Lab Exercise - Complete ESP32 project with real sensors

Let’s start with the browser simulator so you can see MQTT in action right now!

30.4 Interactive Simulator

30.4.1 Try MQTT in Your Browser

Below is a live ESP32 simulator running MQTT code. You can modify the code and see it work!

{sim id="mqtt-esp32-dht22"}

Using the Simulator
  1. Click the green “Play” button to run the simulation
  2. The ESP32 will connect to a public MQTT broker
  3. Watch the Serial Monitor for messages
  4. Try modifying the topic name or message
  5. No hardware needed - runs entirely in your browser!

30.5 Example Code: MQTT Publisher

Here’s a complete example using Python to publish temperature data:

# Python MQTT Publisher Example
import paho.mqtt.client as mqtt
import time
import random

# MQTT Broker settings
BROKER = "test.mosquitto.org"  # Public test broker
PORT = 1883
TOPIC = "iotclass/sensors/temperature"

# Create MQTT client (paho-mqtt 2.0+ API)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="TempSensor_001")

# Connect to broker
print(f"Connecting to {BROKER}...")
client.connect(BROKER, PORT)

# Publish temperature readings
try:
    while True:
        # Simulate temperature sensor
        temp = round(20 + random.uniform(-5, 5), 2)

        # Publish message
        client.publish(TOPIC, f"{temp}")
        print(f"Published: {temp}C to {TOPIC}")

        time.sleep(5)  # Wait 5 seconds

except KeyboardInterrupt:
    print("Stopping publisher...")
    client.disconnect()

30.6 Example Code: MQTT Subscriber

And here’s how to receive those messages:

# Python MQTT Subscriber Example
import paho.mqtt.client as mqtt

BROKER = "test.mosquitto.org"
PORT = 1883
TOPIC = "iotclass/sensors/temperature"

# Callback when connection is established (paho-mqtt 2.0+ signature)
def on_connect(client, userdata, flags, reason_code, properties):
    print(f"Connected with reason code {reason_code}")
    client.subscribe(TOPIC)
    print(f"Subscribed to {TOPIC}")

# Callback when message is received
def on_message(client, userdata, msg):
    temp = float(msg.payload.decode())
    print(f"Received: {temp}C from {msg.topic}")

    # Alert if temperature is too high
    if temp > 25:
        print("Warning: High temperature detected!")

# Create and configure client (paho-mqtt 2.0+ API)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="Dashboard_001")
client.on_connect = on_connect
client.on_message = on_message

# Connect and start listening
client.connect(BROKER, PORT)
client.loop_forever()  # Block and listen for messages

30.7 Lab Exercise: Build a Temperature Monitoring System

Using the publisher and subscriber examples above, build the complete temperature monitoring system described in Your First MQTT Project. Follow these steps:

  1. Set up broker: Use test.mosquitto.org for learning, or install Mosquitto locally
  2. Run the publisher: Copy the publisher code above, run it in one terminal
  3. Run the subscriber: Copy the subscriber code, run it in a second terminal
  4. Verify end-to-end: Confirm published temperatures appear in the subscriber output
  5. Add threshold alerts: Modify the subscriber to trigger warnings when temperature exceeds 25C (already included in the example code)

30.8 Concept Relationships

MQTT Implementation connects to:

Implementation workflow: Learn theory (MQTT fundamentals) → choose hardware (ESP32 + sensors) → implement basic pub/sub (this chapter) → add production patterns (next chapter) → deploy securely (labs chapter).

30.9 See Also

30.10 What’s Next

Chapter Focus Why Read It
MQTT Python Patterns and Security Production-ready coding patterns, error handling, reconnection logic, and TLS authentication Build on the basic publisher/subscriber code from this chapter with robust, real-world practices
MQTT Hands-On Labs Complete ESP32 projects: temperature dashboard, home automation, and TLS security configuration Apply the concepts from this chapter using real hardware with DHT22 and other sensors
MQTT QoS and Session In-depth coverage of QoS 0/1/2, clean vs persistent sessions, and Last Will messages Understand when to upgrade from the default QoS 0 used in this chapter’s examples
MQTT Fundamentals Core publish-subscribe architecture, topic hierarchies, and protocol internals Revisit the theoretical foundation if you want to deepen understanding of how the broker routes messages
MQTT Security TLS/SSL setup, username/password authentication, and access control lists (ACLs) The essential follow-up after learning to avoid public brokers in production deployments
CoAP Features and Labs CoAP protocol as a REST-over-UDP alternative to MQTT Compare MQTT’s broker-centric model against CoAP’s peer-to-peer request-response design