MQTT sensor node with Home Assistant auto-discovery

A BME280 node that publishes to MQTT and appears in Home Assistant automatically, without YAML.

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

⚠ Safety

Low voltage only; keep credentials out of public repositories.

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

Other countries: order these parts on AliExpress with the links below.

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

Wiring

ModuleModule pinESP32 pinNote
BME280 SDA / SCLI2CGPIO21 / GPIO22
VCC / GND3V3 / GND

Libraries

Arduino IDE → Tools → Manage Libraries, then install:

  • PubSubClient
  • Adafruit BME280
  • ArduinoJson

Code

mqtt-ha-sensor-node.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());
  }
}

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

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.

← ← All projects

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

Comments

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

Comments are moderated.