🔹 📖 Lý Thuyết & Kiến Thức Nền Tảng

📌 Bài 1: ESP32 MQTT – Publish and Subscribe
Project Overview
In this example, there’s a Node-RED application that controls ESP32 outputs and receives sensor readings from the ESP32 using MQTT communication protocol. The Node-RED application is running on a Raspberry Pi.
We’ll use the
Mosquitto broker installed
on the same Raspberry Pi. The broker is responsible for receiving all messages, filtering the messages, decide who is interested in them and publishing the messages to all subscribed clients.
The following figure shows an overview of what we’re going to do in this tutorial.
The Node-RED application publishes messages (“
on
” or “
off
“) in the topic
esp32/output
. The ESP32 is subscribed to that topic. So, it receives the message with “on” or “off” to turn the LED on or off.
The ESP32 publishes temperature on the
esp32/temperature
topic and the humidity on the
esp32/humidity
topic. The Node-RED application is subscribed to those topics. So, it receives temperature and humidity readings that can be displayed on a chart or gauge, for example.
Note:
there’s also a similar tutorial on how to use the
ESP8266 and Node-RED with MQTT
.
Prerequisites
You should be familiar with the Raspberry Pi –
read Getting Started with Raspberry Pi
.
You should have the Raspbian operating system installed in your Raspberry Pi –
read Installing Raspbian Lite, Enabling and Connecting with SSH
.
You need
Node-RED installed on your Pi
and
Node-RED Dashboard
.
Learn
what’s MQTT and how it works
.
If you like home automation and you want to learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino, we recommend trying our course:
Build a Home Automation System with Node-RED, ESP8266 and Arduino
. We also have a course dedicated to the ESP32: Enroll in
Learn ESP32 with Arduino IDE
course.
Parts Required
These are the parts required to build the circuit (click the links below to find the best price at
Maker Advisor
):
Raspberry Pi
–



