MQTT · Study deck
MQTT Publisher-Subscriber Setup
Picture a desk sensor sending a button state to a small program on the same laptop.
Broker Bex is your guide for this deck.

After studying this chapter
Learning objectives
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
- 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
Major section
In 60 Seconds
Telemetry means readings and status sent by a remote device.
- Message Queuing Telemetry Transport (MQTT) means a lightweight way for devices to exchange messages.
- A broker means the service that passes messages from senders to receivers.
- A payload means the useful data inside one message.
- Transport layer security means protection for a network stream; it is called TLS.
Major section
In 60 Seconds (continued)
Quality of service means the delivery promise chosen for a message; it is called QoS.
- Old state must not appear as a new button press.
- This runway does not prove a cloud path or production security.
- 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).
Major section
Your First MQTT Project
Mosquitto is free and runs on almost anything -- even a Raspberry Pi.".
- "Step three: write a subscriber!" added 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.".
- In previous chapters, you learned what MQTT is (the theory).
Major section
Mobile Visual Flow
Your phone can be anywhere with internet.
- ESP32 Publisher: reads the DHT22 sensor and publishes a temperature message every 10 seconds.
- Q: What if my Wi-Fi disconnects?: This is why MQTT is perfect for IoT (where Wi-Fi isn't always reliable).
- Multiple family members can all get alerts (multiple subscribers).
Major section
Mobile Visual Flow (continued)
Question:: You want to build a soil moisture monitor that sends alerts to your phone when plants need watering.
- A learning exercise may use test.mosquitto.org, while a controlled deployment uses its own broker; in either case, the broker receives the ESP32 publication and routes it to every matching, authorized subscription.
- A Python service or mobile application subscribes to garden/moisture/plant1, interprets the payload, and triggers a notification when moisture is below the chosen threshold.
- The separation lets the phone move independently of the garden sensor while the topic remains their shared contract.
Major section
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 a real sensor fleet without changing the broker.
- Third, there is no project-specific reliability guarantee when the public service restarts.
- Together these are deployment boundaries, not limitations that application code can repair.
- Each outcome connects a missing broker control to a concrete product failure.
Major section
Common Mistake: Using test.mosquitto.org in Production (continued)
The consequences follow the same order: an unauthorized subscriber can infer product behavior, another user's retained value can pollute the application's state, and a broker outage can suppress a time-critical greenhouse alert.
- A public broker is useful for learning but provides no project-specific authentication, privacy, or uptime accountability.
- A private broker can be inexpensive to self-host, while making the team responsible for accounts, TLS certificates, patching, and monitoring.
- This progression connects the prototype warning to an explicit operational choice instead of implying that one hosting model fits every product.
Major section
Deep-Dive Note: MQTT Connection Evidence Boundaries
A random client ID can make every reconnect look like a new device, so queued session state is never resumed.
- The client sends CONNECT; the broker replies with CONNACK.
- A duplicated client ID can make two gateways disconnect each other in a loop.
- That record makes later reconnect, duplicate-ID, and offline-presence bugs diagnosable.
Major section
Deep-Dive Note: MQTT Connection Evidence Boundaries (continued)
That first exchange carries the client identifier, clean-start or clean-session intent, keep-alive interval, optional will message, credentials, and the broker's acceptance or refusal result.
- A robust client subscribes again when Session Present = 0, but avoids duplicating subscriptions when the broker says the old session was resumed.
- The client declares the interval in seconds; if it has no other packet to send, it sends PINGREQ and expects PINGRESP.
- Any MQTT control packet resets the timer, so a telemetry publisher that sends every 20 seconds does not need separate pings for a 60 second keep-alive.
Deck summary
Key takeaways
Telemetry means readings and status sent by a remote device.
- Quality of service means the delivery promise chosen for a message; it is called QoS.
- Mosquitto is free and runs on almost anything -- even a Raspberry Pi.".
- Your phone can be anywhere with internet.
- Question:: You want to build a soil moisture monitor that sends alerts to your phone when plants need watering.
Retrieval practice
Recall check 1 of 5

Broker Bex says: answer from memory, then check your reasoning.
Q1In an MQTT-based temperature monitoring system, an ESP32 sensor node reads temperature data and sends it to a broker. A Python dashboard running on a laptop displays the readings. Which statement correctly identifies each component's role?
Show answer
Answer: B The three MQTT roles are publisher (data source), broker (message router), and subscriber (data consumer).
Retrieval practice
Recall check 2 of 5

Broker Bex says: answer from memory, then check your reasoning.
Q2Using this chapter's broker CPU-load formula (CPU% ~ 0.01 x R_out + 0.005 x N_conn), what CPU load does the chapter compute for 10,000 devices publishing 0.1 msg/sec each with 3 subscribers per topic?
Show answer
Answer: A
Retrieval practice
Recall check 3 of 5

Broker Bex says: answer from memory, then check your reasoning.
Q3This chapter's 'Common Mistake' warning lists four dangers of using test.mosquitto.org in a production deployment. Which of the following is one of them?
Show answer
Answer: A The chapter's four dangers of using test.mosquitto.org in production: Zero Authentication, Zero Encryption, No Reliability Guarantee, and Topic Collisions -- each with a stated production consequence.
Q4Complete the MQTT publisher with broker configuration:
Show answer
Answer: A MQTT's default unencrypted port is 1883 (8883 is for TLS).
Retrieval practice
Recall check 4 of 5

Broker Bex says: answer from memory, then check your reasoning.
Q5Place each setup step where it lives so you can prove that a broker, subscriber, and publisher complete one observable MQTT round trip.
Show answer
Answer: A Place each setup step where it lives so you can prove that a broker, subscriber, and publisher complete one observable MQTT round trip.
Retrieval practice
Recall check 5 of 5

Broker Bex says: answer from memory, then check your reasoning.
Q6Per this chapter's Deep-Dive Note, what does the broker do if it receives no control packet from a client within 1.5x the declared keep-alive interval?
Show answer
Answer: A
Print reference
Answers 1 of 2
Answer key.
- B · The three MQTT roles are publisher (data source), broker (message router), and subscriber (data consumer).
- A
- A · The chapter's four dangers of using test.mosquitto.org in production: Zero Authentication, Zero Encryption, No Reliability Guarantee, and Topic Collisions -- each with a stated production consequence.
- A · MQTT's default unencrypted port is 1883 (8883 is for TLS).
- A · Place each setup step where it lives so you can prove that a broker, subscriber, and publisher complete one observable MQTT round trip.
Print reference
Answers 2 of 2
Answer key.
- A