ESP32: build a home sensor hub with MQTT and Node-RED
The complete home sensor network: ESP32 publishes to MQTT, Raspberry Pi runs Node-RED and a dashboard. Multiple sensors in multiple rooms.
This is the project that ties the ESP32 sensor tutorials to the Raspberry Pi MQTT and Node-RED tutorials. Multiple ESP32 boards publish sensor readings to an MQTT broker running on a Raspberry Pi. The Pi runs Node-RED, which subscribes to the readings and exposes them on a dashboard.
It is the system I run in my house. Several ESP32 nodes, one Pi, one dashboard. Total cost: about $50 in hardware.
The architecture
ESP32 sensor 1 (kitchen) ESP32 sensor 2 (bedroom)
| |
| MQTT over Wi-Fi |
+----------+----------------------+
|
v
Raspberry Pi
(MQTT broker + Node-RED + dashboard)
|
v
Any browser
(http://raspberrypi.local:1880/ui)
The ESP32s are clients. The Pi is the broker. Node-RED is the dashboard.
What you need
- 2 or more ESP32 boards (the sensors)
- BME280 breakout (one per ESP32)
- 18650 + TP4056 (one per ESP32)
- Raspberry Pi (any model with Wi-Fi; 3B+ or 4 recommended)
- microSD card with Raspberry Pi OS
- Network with Wi-Fi (the ESP32s and Pi need to be on the same LAN)
Step 1: set up the Pi
Flash Raspberry Pi OS Lite onto the SD card. Boot the Pi, connect it to
your Wi-Fi (via raspi-config or by editing /etc/wpa_supplicant/).
Install Mosquitto (the MQTT broker) and Node-RED:
sudo apt update
sudo apt install mosquitto mosquitto-clients -y
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
sudo systemctl enable nodered
sudo systemctl start nodered
The Mosquitto broker listens on port 1883. Node-RED listens on port 1880.
Verify:
mosquitto_sub -h localhost -t test &
mosquitto_pub -h localhost -t test -m "hello"
You should see “hello” appear in the subscriber. MQTT is working.
Step 2: program the ESP32s
Each ESP32 needs the same code with different room labels. The full code:
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
const char* ssid = "your-wifi";
const char* password = "your-password";
const char* mqttServer = "192.168.1.50"; // Pi's IP
const int mqttPort = 1883;
const char* roomName = "kitchen"; // change per ESP32
WiFiClient wifiClient;
PubSubClient mqtt(wifiClient);
Adafruit_BME280 bme;
unsigned long lastPublish = 0;
const unsigned long PUBLISH_INTERVAL = 30000; // 30 seconds
void setup() {
Serial.begin(115200);
delay(1000);
Wire.begin(21, 22);
if (!bme.begin(0x76)) {
Serial.println("Could not find BME280");
ESP.restart();
}
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
mqtt.setServer(mqttServer, mqttPort);
Serial.print("ESP32 ");
Serial.print(roomName);
Serial.print(" connected. IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (!mqtt.connected()) {
reconnect();
}
mqtt.loop();
if (millis() - lastPublish > PUBLISH_INTERVAL) {
lastPublish = millis();
publishReading();
}
}
void publishReading() {
float t = bme.readTemperature();
float h = bme.readHumidity();
float p = bme.readPressure() / 100.0;
char topic[50];
snprintf(topic, sizeof(topic), "home/%s/sensor", roomName);
char payload[150];
snprintf(payload, sizeof(payload),
"{\"temp\":%.2f,\"hum\":%.2f,\"press\":%.2f}",
t, h, p);
mqtt.publish(topic, payload);
Serial.print("Published to ");
Serial.print(topic);
Serial.print(": ");
Serial.println(payload);
}
void reconnect() {
while (!mqtt.connected()) {
String clientId = "esp32-";
clientId += roomName;
if (mqtt.connect(clientId.c_str())) {
Serial.println("MQTT connected");
} else {
Serial.print("MQTT failed, rc=");
Serial.print(mqtt.state());
delay(5000);
}
}
}
Upload to each ESP32 with a different roomName (kitchen, bedroom,
office, etc.). Open Serial Monitor to see the publish confirmations.
Step 3: verify the messages
On the Pi, subscribe to all home topics:
mosquitto_sub -h localhost -t "home/#" -v
You should see JSON messages appearing every 30 seconds:
home/kitchen/sensor {"temp":22.5,"hum":45.2,"press":1013.2}
home/bedroom/sensor {"temp":21.8,"hum":50.1,"press":1013.4}
home/office/sensor {"temp":23.1,"hum":42.8,"press":1013.3}
If you do not see messages, check:
- The ESP32 is connected to Wi-Fi.
- The MQTT server IP is correct.
- Mosquitto is running (
sudo systemctl status mosquitto).
Step 4: build the Node-RED dashboard
Open http://raspberrypi.local:1880/ in a browser. Node-RED’s UI
loads. Build this flow:
- MQTT input node: subscribe to
home/+/sensor. The+wildcard matches all rooms. - JSON parser: parse the payload as JSON.
- Function node: extract room name from the topic.
- Gauge nodes: display temp, humidity, pressure.
Drag nodes from the left palette. Wire them by dragging from one
node’s output to the next’s input. Click the MQTT node, set the topic
to home/+/sensor, click Deploy. Repeat for the other nodes.
The JSON parser outputs an object with temp, hum, press. The
function node can split these into separate paths:
msg.topic = msg.topic; // e.g. "home/kitchen/sensor"
msg.room = msg.topic.split('/')[1]; // "kitchen"
return msg;
Each of the three Gauge nodes gets a different value from the JSON. Add a text node to show the room name.
For historical graphs, add a Chart node and store data in InfluxDB or SQLite. The book Home Automation with Raspberry Pi covers this.
The total cost
| Component | Cost |
|---|---|
| 3 ESP32 boards | $15 |
| 3 BME280 breakouts | $6 |
| 3 18650 cells (genuine) | $15 |
| 3 TP4056 boards | $3 |
| 3 3.3V LDO regulators | $3 |
| Raspberry Pi 4 (2 GB) | $35 |
| microSD card | $8 |
| Jumpers, wires, enclosures | $20 |
| Total | $105 |
For a smaller network (1 ESP32), drop to $50. For more nodes, add $15 per additional ESP32 sensor.
What you learned
- A complete home sensor network with ESP32 + MQTT + Node-RED + Pi.
- Each ESP32 publishes sensor readings as JSON over MQTT.
- Node-RED subscribes and routes to gauges and charts.
- Total cost: about $50-$100 depending on node count.
What to build next
- The Raspberry Pi Node-RED tutorial covers the dashboard side.
- The MQTT tutorial covers publishing from the ESP32 side.
- The book IoT with ESP32 has more advanced patterns (alerting, historical storage, automation rules).