ESP32 + Raspberry Pi hub

ESP32 nodes report to Home Assistant on a Raspberry Pi: history, graphs and automations in one place.

Server / Home Assistant Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 2–4 h

The weak point is the memory card

A Raspberry Pi running Home Assistant writes to its database all the time. On an SD card that means wear, and a power cut in the middle of a write can corrupt the whole system. In a country with scheduled blackouts that is not a rare event. Plan for it from day one.

How to make it survive

  • Boot from a small SSD over USB instead of the card, or use a good "high endurance" card as the minimum.
  • Put the Pi on its own tiny UPS or a battery HAT, so it can shut down cleanly.
  • In the recorder settings keep about ten days of history and exclude noisy sensors that you never look at.
  • Make backups to another device. A snapshot that lives on the same card is not a backup.

ESP32 nodes with ESPHome then report to the Pi over Wi-Fi. If your router is also on the UPS, the whole chain keeps logging through the outage and you can see exactly when the mains left and returned.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • BME280 I2C module (3.3V)
  • Raspberry Pi 4 Model B (2/4 GB)
  • microSD 32 GB A1 (industrial / high endurance)
  • Official-class 5 V 3-5 A USB-C power supply

Wiring

Module Module pin ESP32 pin Note
BME280 SDA / SCL I2C GPIO21 / GPIO22
ESP32 ↔ Raspberry Pi Wi-Fi MQTT 1883 same LAN, broker on the Pi

Libraries

  • PubSubClient
  • Adafruit BME280
  • ArduinoJson

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <PubSubClient.h>

const char* MQTT_HOST = "192.168.1.10";   // IP of your Raspberry Pi / mini-PC
const uint16_t MQTT_PORT = 1883;
const char* MQTT_USER = "esp32";
const char* MQTT_PASS = "mqtt-password";

WiFiClient netClient;
PubSubClient mqtt(netClient);

bool mqttConnect(const char* clientId) {
  mqtt.setServer(MQTT_HOST, MQTT_PORT);
  uint32_t t0 = millis();
  while (!mqtt.connected() && millis() - t0 < 10000) {
    if (mqtt.connect(clientId, MQTT_USER, MQTT_PASS)) return true;
    delay(500);
  }
  return mqtt.connected();
}
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <ArduinoJson.h>
Adafruit_BME280 bme;
const char* NODE = "esp32-node1";

void publishDiscovery(const char* key, const char* name, const char* unit, const char* devClass) {
  JsonDocument d;
  d["name"] = name; d["unique_id"] = String(NODE) + "_" + key; d["state_topic"] = String("esp32/") + NODE + "/state";
  d["value_template"] = String("{{ value_json.") + key + " }}"; d["unit_of_measurement"] = unit; d["device_class"] = devClass;
  d["device"]["identifiers"][0] = NODE; d["device"]["name"] = NODE;
  String out; serializeJson(d, out);
  mqtt.setBufferSize(768);
  mqtt.publish((String("homeassistant/sensor/") + NODE + "_" + key + "/config").c_str(), out.c_str(), true);
}
void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  bme.begin(0x76);
  connectWiFi();
  if (mqttConnect(NODE)) {
    publishDiscovery("temperature", "Temperature", "°C", "temperature");
    publishDiscovery("humidity", "Humidity", "%", "humidity");
  }
}
void loop() {
  if (!mqtt.connected()) mqttConnect(NODE);
  mqtt.loop();
  static uint32_t t = 0;
  if (millis() - t > 30000) {
    t = millis();
    String s = String("{"temperature":") + String(bme.readTemperature(), 1) + ","humidity":" + String(bme.readHumidity(), 0) + "}";
    mqtt.publish((String("esp32/") + NODE + "/state").c_str(), s.c_str());
  }
}

docker-compose.yml

services:
  mosquitto:
    image: eclipse-mosquitto:2
    restart: unless-stopped
    ports: ["1883:1883"]
    volumes: ["./mosquitto:/mosquitto/config", "./mosquitto-data:/mosquitto/data"]
  homeassistant:
    image: ghcr.io/home-assistant/home-assistant:stable
    restart: unless-stopped
    network_mode: host
    volumes: ["./ha-config:/config", "/etc/localtime:/etc/localtime:ro"]

mosquitto/mosquitto.conf

persistence true
persistence_location /mosquitto/data/
listener 1883
allow_anonymous false
password_file /mosquitto/config/passwd
# create the user: docker compose exec mosquitto mosquitto_passwd -c /mosquitto/config/passwd esp32

Build tips

  • Boot the Pi from a USB SSD instead of an SD card for reliability.
  • Give the Pi and the router a static address so ESP32 nodes always find the broker.

What you need

Buy the parts kit · Estimated total: $72.00

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
BME280 I2C module (3.3V) × 1
$3.00
Buy at AliExpress Buy at Amazon.com
Raspberry Pi 4 Model B (2/4 GB) × 1
$45.00
Buy at AliExpress Buy at Amazon.com
microSD 32 GB A1 (industrial / high endurance) × 1
$8.00
Buy at AliExpress Buy at Amazon.com
Official-class 5 V 3-5 A USB-C power supply × 1
$12.00
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Ukraine: we buy and sell these parts ourselves, order them on this site.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

Home Assistant sensor node

A ready ESPHome node with BME280 that Home Assistant finds automatically, the easiest start.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●○○ Beginner
  • Build time 1 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Offline energy monitor (Orange Pi)

Orange Pi Zero 3 stores PZEM data locally and shows graphs even without internet.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–5 h
Pick parts / kit
⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

ESP-NOW sensor network with one gateway

Several battery nodes send readings over ESP-NOW without joining Wi-Fi, and one gateway forwards them to MQTT or Home Assistant.

  • Board ESP32-C3 SuperMini
  • Level ●●● Advanced
  • Build time 2–4 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.