Home Assistant sensor node

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

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

🛒 Ready kit in the shop

The easiest first step into the ecosystem

If you have never flashed an ESP32 and want something that works in an evening, start here. The ESPHome file is a dozen lines, there is no C++, and Home Assistant finds the device by itself on the local network.

A few details that save time

  • Set the API encryption key and the OTA password from the start; without them any device on the network can talk to the node.
  • Keep the fallback access point enabled, so a wrong Wi-Fi password does not turn the board into a brick on a shelf.
  • Use a filter on the BME280 temperature to subtract the self-heating offset you measured with a reference thermometer.
  • Give nodes descriptive names, such as "bedroom-climate", because the name becomes the entity ID and renaming later is a chore.

ESPHome also supports deep sleep with a defined run duration if you want to move the node to a battery later. MQTT is an alternative if you do not run Home Assistant; the API is the simpler choice if you do.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • BME280 I2C module (3.3V)
  • Dupont jumper wires (M-F, M-M)

Wiring

Module Module pin ESP32 pin Note
BME280 SDA / SCL I2C GPIO21 / GPIO22
VCC / GND 3V3 / GND

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());
  }
}

Build tips

  • Use a retained discovery message and a unique node id derived from the MAC address.
  • Enable the Mosquitto add-on in Home Assistant and create a dedicated MQTT user.

What you need

Buy the parts kit · Estimated total: $8.50

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
Dupont jumper wires (M-F, M-M) × 1
$1.50
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

ESP32 + Raspberry Pi hub

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

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–4 h
Pick parts / kit

Temperature & humidity station

BME280 plus an OLED screen: temperature, humidity and pressure on the display, a web page and Home Assistant.

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

Dust PM2.5 air monitor

PMS5003 particle sensor plus BME280: PM1 / PM2.5 / PM10, temperature and humidity on a web page.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 1–2 h
Pick parts / kit

← All module ideas →

Comments

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

Comments are moderated.