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.

implgettingstarted
Broker Bex, the module guide, in a scene from this chapter.
iotclass.org

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
iotclass.org

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.

Key terms

Quality of service
Quality of service means the delivery promise chosen for a message; it is called QoS.
iotclass.org

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).
iotclass.org

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).
iotclass.org

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).
iotclass.org

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.
iotclass.org

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.
iotclass.org

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.
iotclass.org

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.
iotclass.org

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.
iotclass.org

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.
iotclass.org

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?

AESP32 = broker, Python dashboard = publisher, test.mosquitto.org = subscriber
BESP32 = publisher, test.mosquitto.org = broker, Python dashboard = subscriber
CESP32 = subscriber, test.mosquitto.org = publisher, Python dashboard = broker
DESP32 = publisher, Python dashboard = broker, test.mosquitto.org = subscriber
Show answer

Answer: B The three MQTT roles are publisher (data source), broker (message router), and subscriber (data consumer).

iotclass.org

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?

A80% (0.01 x 3,000 + 0.005 x 10,000 = 30 + 50), which the chapter says needs a cluster
B8%, the same as the 1,000-device example, because CPU load only depends on message rate
C30%, counting only the fan-out term and ignoring the per-connection overhead
D300%, treating R_out in msg/sec as if it were the CPU percentage directly
Show answer

Answer: A

iotclass.org

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?

AZero Authentication -- anyone can subscribe to your topics and see your data
BAutomatic message encryption that slows down publish latency
CA hard limit of 100 connected devices per broker
DMandatory paid subscription after 30 days of use
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:

APORT = 1883
BPORT = 8883
CPORT = 443
DPORT = 8080
Show answer

Answer: A MQTT's default unencrypted port is 1883 (8883 is for TLS).

iotclass.org

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.

AInstall Mosquitto
BStart Broker
Cmosquitto_sub
Dmosquitto_pub
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.

iotclass.org

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?

AIt disconnects the client and publishes the will message if one was configured
BIt automatically resets the keep-alive interval to a longer value
CIt silently drops the next few messages but keeps the connection open
DIt sends a PINGREQ to the client and waits indefinitely for a response
Show answer

Answer: A

iotclass.org

Print reference

Answers 1 of 2

Answer key.

  1. B · The three MQTT roles are publisher (data source), broker (message router), and subscriber (data consumer).
  2. A
  3. 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.
  4. A · MQTT's default unencrypted port is 1883 (8883 is for TLS).
  5. A · Place each setup step where it lives so you can prove that a broker, subscriber, and publisher complete one observable MQTT round trip.
iotclass.org

Print reference

Answers 2 of 2

Answer key.

  1. A
iotclass.org