Why MQTT for Home Sensors?

MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe protocol designed for constrained devices and unreliable networks — which makes it a natural fit for battery-powered ESP32 sensor nodes scattered around your house. Unlike HTTP polling, MQTT keeps a persistent connection and delivers messages with minimal overhead.

In this project, each ESP32 node publishes temperature and humidity readings to a Mosquitto broker running on a Raspberry Pi. A separate subscriber (or Node-RED, or Home Assistant) can then consume those readings in real time.

Hardware Per Node

  • ESP32 development board (any variant: WROOM, WROVER, C3)
  • DHT22 or SHT31 temperature/humidity sensor
  • USB power supply or 3.7V LiPo with charge board (for portable nodes)

Step 1: Install Mosquitto on Raspberry Pi

sudo apt update
sudo apt install mosquitto mosquitto-clients
sudo systemctl enable mosquitto
sudo systemctl start mosquitto

By default, Mosquitto only accepts local connections. Edit /etc/mosquitto/mosquitto.conf to allow LAN connections:

listener 1883
allow_anonymous true

Note: For a home lab, anonymous access on a private VLAN is acceptable. For anything internet-facing, add username/password auth or TLS — do not skip this.

Step 2: ESP32 Firmware with Arduino Framework

Install the PubSubClient library in your Arduino IDE or PlatformIO project. Here's the core sketch logic:

#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

const char* ssid     = "YOUR_SSID";
const char* password = "YOUR_PASS";
const char* broker   = "192.168.1.100"; // Pi's IP
const char* topic    = "home/sensor/living_room";

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(4, DHT22); // GPIO4

void setup() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  client.setServer(broker, 1883);
  dht.begin();
}

void loop() {
  if (!client.connected()) {
    client.connect("esp32_living_room");
  }
  client.loop();

  float temp = dht.readTemperature();
  float hum  = dht.readHumidity();

  char payload[64];
  snprintf(payload, sizeof(payload),
           "{\"temp\":%.1f,\"hum\":%.1f}", temp, hum);

  client.publish(topic, payload);
  delay(30000); // publish every 30 seconds
}

Step 3: Verify Messages Are Arriving

On the Raspberry Pi, subscribe to your topic and watch the output:

mosquitto_sub -h localhost -t "home/sensor/#" -v

The # wildcard matches all sub-topics under home/sensor/, so you can watch all nodes at once. You should see JSON payloads arrive every 30 seconds from each node.

Topic Naming Strategy

Good topic naming pays dividends as your network grows. A consistent pattern like home/<room>/<sensor_type> lets you subscribe selectively:

  • home/bedroom/temperature
  • home/garage/motion
  • home/+/temperature — all temperature sensors in all rooms

Extending the System

Once your broker is running and nodes are publishing, you have a solid foundation to build on:

  1. Node-RED — drag-and-drop flows for processing and dashboards, runs on the same Pi
  2. InfluxDB + Grafana — store time-series data and build beautiful historical charts
  3. Home Assistant — full home automation platform with native MQTT discovery
  4. Deep sleep — put ESP32 nodes into deep sleep between readings for battery-powered deployments

The architecture scales surprisingly well. A single Mosquitto instance on a Pi Zero 2W can comfortably handle dozens of nodes on a home network without breaking a sweat.