ESP32 MQTT and Cloud IoT
Now we move into real IoT communication.
Until now, we learned:
ESP32 → Wi-Fi → Web Server ESP32 → HTTP → API ESP32 → WebSocket → Browser
MQTT introduces a different and very important architecture:
ESP32 │ │ MQTT ▼ MQTT Broker │ ├── Mobile App ├── Web Dashboard ├── Another ESP32 └── Cloud Service
This is widely useful for smart homes, agriculture, industrial IoT, sensor networks, and automation.
1. What is MQTT?
MQTT = Message Queuing Telemetry Transport
It is a lightweight messaging protocol designed especially for devices with limited resources and unreliable/low-bandwidth networks.
Instead of directly connecting every device to every other device:
ESP32 A ───────── ESP32 B ESP32 A ───────── ESP32 C ESP32 B ───────── ESP32 C
MQTT uses a central broker:
MQTT Broker / | \ / | \ ESP32 Phone Dashboard
2. MQTT Architecture
There are three major concepts:
Publisher │ ▼ MQTT Broker │ ▼ Subscriber
For example:
DHT22 → ESP32 → MQTT Broker → Mobile App
The ESP32 publishes temperature.
The mobile app subscribes to temperature.
3. Publisher
A publisher sends messages.
Example:
ESP32 │ │ Publish ▼ temperature = 29.5°C
So:
ESP32 = Publisher
4. Subscriber
A subscriber receives messages.
Example:
MQTT Broker │ │ ▼ Mobile App
The mobile app subscribes to:
home/temperature
Whenever a new value arrives, the app receives it.
5. MQTT Broker
The broker is the central message server.
MQTT BROKER ┌──────┼──────┐ │ │ │ ▼ ▼ ▼ ESP32 Phone Dashboard
The broker:
- Receives messages
- Checks topics
- Delivers messages to subscribers
- Manages client connections
- Can handle many devices
Examples of MQTT brokers include:
- Mosquitto
- EMQX
- HiveMQ
- Cloud-hosted MQTT services
6. MQTT Topic
MQTT uses topics to organize messages.
Example:
home/livingroom/temperature
Another:
home/livingroom/humidity
Another:
home/bedroom/light
Think of a topic like a communication channel.
home/ ├── livingroom/ │ ├── temperature │ ├── humidity │ └── light │ └── bedroom/ ├── temperature └── light
7. Example
Suppose ESP32 measures:
Temperature = 28.7°C
It publishes:
Topic: home/livingroom/temperature Message: 28.7
Architecture:
DHT22 │ ▼ ESP32 │ │ Publish │ ▼ MQTT Broker │ ├────────────► Phone │ └────────────► Dashboard
8. Publish and Subscribe
Suppose:
ESP32 #1
publishes:
home/temperature
and:
ESP32 #2
subscribes to:
home/temperature
Then:
ESP32 #1 │ │ "29.5" ▼ MQTT Broker │ │ "29.5" ▼ ESP32 #2
Neither ESP32 needs to know the other's IP address.
That's one of MQTT's biggest advantages.
9. MQTT vs HTTP
| Feature | HTTP | MQTT |
|---|---|---|
| Architecture | Request/response | Publish/subscribe |
| Connection | Usually request-based | Persistent connection |
| IoT suitability | Good | Excellent |
| Real-time messaging | Moderate | Excellent |
| Overhead | Higher | Low |
| Sensors | Good | Excellent |
| Cloud IoT | Good | Excellent |
Think:
HTTP: "Give me the temperature." MQTT: "Whenever temperature changes, send it to me."
10. MQTT Example for Smart Home
Imagine your smart-home system.
MQTT Broker │ ┌────────────┼────────────┐ │ │ │ ▼ ▼ ▼ ESP32 #1 ESP32 #2 Phone Sensors Devices
ESP32 #1:
DHT22 PIR LDR
ESP32 #2:
Relay Fan Light Servo
Topics:
home/temperature home/humidity home/motion home/light home/fan home/door
11. Controlling a Device with MQTT
Suppose the phone wants to turn on the light.
Phone publishes:
Topic: home/light Message: ON
Broker sends the message to the ESP32.
Phone │ │ home/light → ON ▼ MQTT Broker │ ▼ ESP32 │ ▼ Relay │ ▼ Light ON
12. Two-Way IoT
MQTT supports both directions.
MQTT Broker / \ / \ ▼ ▼ ESP32 Sensor ESP32 Control │ ▲ │ │ Temperature Light ON
Sensor data can go upward:
ESP32 → Broker
Commands can go downward:
Broker → ESP32
13. MQTT Topics for Your Project
For your smart-home project, you could design:
home/ ├── temperature ├── humidity ├── motion ├── light ├── fan ├── door └── security
Or more structured:
home/livingroom/temperature home/livingroom/humidity home/livingroom/light home/livingroom/fan home/entrance/door home/entrance/motion
This makes larger systems easier to manage.
14. JSON + MQTT
MQTT messages don't have to be simple strings.
You can publish JSON:
{ "temperature": 29.4, "humidity": 61, "motion": true }
For example:
Topic: home/livingroom/status
Message:
{ "temperature": 29.4, "humidity": 61, "motion": true }
Now one MQTT message contains several sensor values.
15. ESP32 MQTT Libraries
A common Arduino library is:
#include <PubSubClient.h>
Typically you combine it with:
#include <WiFi.h>
Architecture:
WiFi │ ▼ ESP32 │ PubSubClient │ ▼ MQTT Broker
16. Basic MQTT Connection
The basic structure looks like:
#include <WiFi.h> #include <PubSubClient.h> WiFiClient espClient; PubSubClient client(espClient);
Then specify your broker:
client.setServer("broker.example.com", 1883);
And connect:
client.connect("ESP32_Client");
The exact broker hostname, port, username, and password depend on the MQTT service you're using.
17. Publishing Data
Conceptually:
client.publish( "home/temperature", "29.5" );
This means:
Topic: home/temperature Message: 29.5
18. Subscribing
To receive commands:
client.subscribe("home/light");
Then when a message arrives, a callback processes it.
Conceptually:
void callback(char* topic, byte* payload, unsigned int length) { // Process MQTT message }
For example:
home/light → ON
could turn on a relay.
19. MQTT Callback
The callback is extremely important.
Think:
MQTT Broker │ │ Message arrives ▼ callback() │ ├── Check topic ├── Read message └── Control GPIO
Example logic:
if (message == "ON") { digitalWrite(RELAY_PIN, HIGH); }
20. MQTT Loop
Your program also needs to continuously process MQTT communication.
Typically:
client.loop();
is called regularly inside:
void loop() { client.loop(); }
Without properly processing the MQTT connection, communication can stop working.
21. QoS
MQTT has Quality of Service (QoS) levels.
QoS 0 QoS 1 QoS 2
QoS 0
At most once
Message is sent without delivery confirmation.
Fast and lightweight.
QoS 1
At least once
Message delivery is acknowledged, but duplicates can occur.
QoS 2
Exactly once
Provides the strongest delivery guarantee, with more overhead.
For many ESP32 sensor applications:
QoS 0 or QoS 1
is often sufficient, depending on the application.
22. Retained Messages
MQTT can retain the latest message for a topic.
Suppose:
home/light = ON
is retained.
A new subscriber joins:
Phone │ ▼ MQTT Broker
The broker can immediately provide the retained value:
Light = ON
This is useful for device state.
23. Last Will and Testament
MQTT can also detect unexpected disconnections.
Suppose:
ESP32 │ │ Connected ▼ Broker
Suddenly:
ESP32 X
A configured Last Will message can be published, for example:
home/esp32/status
with:
OFFLINE
This allows dashboards to show:
ESP32: OFFLINE
24. MQTT Keep Alive
MQTT clients maintain a connection with the broker.
The client periodically communicates to show it is still alive.
ESP32 │ │ keep-alive ▼ Broker
If communication stops for too long, the broker can consider the client disconnected.
25. Cloud IoT Architecture
Now let's move beyond a local network.
Internet │ ▼ Cloud MQTT Broker │ ┌────────┼────────┐ │ │ │ ESP32 Phone Web App
Your ESP32 can publish sensor data from anywhere with Internet connectivity.
For example:
Farm ESP32 │ │ Wi-Fi ▼ Internet │ ▼ Cloud MQTT Broker │ ▼ Your Dashboard
26. Smart Agriculture Example
Imagine you have multiple ESP32 nodes:
Cloud Broker ▲ │ ┌──────┼──────┐ │ │ │ ESP32 ESP32 ESP32 Node1 Node2 Node3 │ │ │ Soil Soil DHT22
Topics:
farm/field1/soil farm/field2/soil farm/field3/temperature farm/field3/humidity
Your dashboard can display:
Field 1 Soil: 35% Field 2 Soil: 42% Temperature: 31°C Humidity: 58%
27. Automatic Irrigation
You can create:
Soil Sensor │ ▼ ESP32 │ MQTT │ ▼ Cloud / Controller │ ▼ Pump command │ ▼ ESP32 │ ▼ Relay │ ▼ Water Pump
For example:
Soil < 30% ↓ Pump ON
And:
Soil > 50% ↓ Pump OFF
28. MQTT Security
Never use an unsecured public MQTT broker for important real-world control systems.
Important security measures include:
Authentication
Username Password
TLS encryption
Instead of:
mqtt://
secure deployments commonly use TLS-protected MQTT such as:
mqtts://
or an MQTT TLS port such as:
8883
depending on the broker.
Access control
A device should only be allowed to access the topics it actually needs.
For example:
ESP32 Sensor ↓ Can publish: farm/field1/temperature
but shouldn't automatically have permission to control:
home/door
29. MQTT Topic Wildcards
MQTT subscriptions support wildcards.
+
Single-level wildcard:
home/+/temperature
matches:
home/room1/temperature home/room2/temperature home/kitchen/temperature
#
Multi-level wildcard:
home/#
can match topics underneath home.
For example:
home/room1/temperature home/room1/light home/room2/fan
This is useful for dashboards that need many related topics.
30. MQTT vs WebSocket
Both can provide near-real-time communication, but their purposes differ.
WebSocket: Browser ═════════ ESP32
Usually useful for direct real-time client/server communication.
MQTT:
ESP32 ──┐ ESP32 ──┤ Phone ──┼── MQTT Broker Web ────┤ ESP32 ──┘
MQTT is particularly useful when many independent devices and applications need to exchange IoT messages.
31. MQTT + ESP-NOW
You can even combine the technologies.
Sensor ESP32 │ ESP-NOW │ ▼ Gateway ESP32 │ Wi-Fi │ ▼ MQTT Broker │ ▼ Cloud Dashboard
This is a powerful architecture.
The sensor nodes don't all need direct Internet access.
32. Complete IoT Architecture
Now combine everything you've learned:
┌───────────────┐ │ Sensors │ │ DHT22 / PIR │ │ LDR / Soil │ └───────┬───────┘ │ ESP32 │ ESP-NOW / Wi-Fi │ ▼ MQTT Broker │ ┌────────────┼────────────┐ │ │ │ ▼ ▼ ▼ Database Web App Mobile │ ▼ Dashboard
This is the foundation of a serious IoT system.
33. Important MQTT Terms
| Term | Meaning |
|---|---|
| Broker | Central MQTT server |
| Publisher | Sends messages |
| Subscriber | Receives messages |
| Topic | Message channel/path |
| Payload | Actual message data |
| QoS | Delivery guarantee |
| Retain | Store latest message |
| Will | Offline notification |
| Client | Any connected MQTT device/app |
🧠 Quick Summary
Remember:
MQTT │ ┌───────┼────────┐ │ │ │ Publisher Broker Subscriber │ │ │ └──────►│───────►│ │ Topic │ Payload
Example:
ESP32 │ │ Publish ▼ home/temperature │ │ 29.5 ▼ MQTT Broker │ ├────► Phone └────► Web Dashboard
And for control:
Phone │ │ Publish ▼ home/light │ │ ON ▼ MQTT Broker │ ▼ ESP32 │ ▼ Relay