Local smart home: ESP32 + Raspberry Pi (Home Assistant + MQTT)

A complete offline stack: Raspberry Pi 4/5 runs Home Assistant and Mosquitto, ESP32 nodes report over MQTT. No cloud, no subscriptions.

Level: Intermediate Board: ESP32 DevKit V1 (WROOM-32) Smart home

⚠ Safety

The Pi is powered by a USB-C supply: use the official one. Mains wiring still needs an electrician.

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.

Wiring

ModuleModule pinESP32 pinNote
BME280 SDA / SCLI2CGPIO21 / GPIO22
ESP32 ↔ Raspberry PiWi-FiMQTT 1883same LAN, broker on the Pi

Libraries

Arduino IDE → Tools → Manage Libraries, then install:

  • PubSubClient
  • Adafruit BME280
  • ArduinoJson

Code

rpi-home-assistant-hub.ino
#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

Flash a ready firmware from the browser Paste your error into the AI Error Doctor

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.

ESP32, Arduino and mini-PC: who does what

← ← All projects

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

Comments

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

Comments are moderated.