MQTT-Sensorknoten mit Home-Assistant-Autodiscovery

Ein BME280-Knoten sendet an MQTT und erscheint automatisch in Home Assistant, ganz ohne YAML.

Level: Fortgeschritten Board: ESP32 DevKit V1 (WROOM-32) Smart Home

⚠ Sicherheit

Nur Niederspannung; Zugangsdaten nicht in öffentliche Repositories stellen.

Das brauchst du

Teile-Set kaufen · Geschätzte Summe: €7.82

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
€3.68
Kaufen bei AliExpress Kaufen bei Amazon.de
BME280 I2C module (3.3V) × 1
€2.76
Kaufen bei AliExpress Kaufen bei Amazon.de
Dupont jumper wires (M-F, M-M) × 1
€1.38
Kaufen bei AliExpress Kaufen bei Amazon.de
🇺🇦 Ukraine🌍 Andere Länder

Andere Länder: Bestellen Sie diese Teile über die Links unten bei AliExpress.

Einige Links sind Affiliate-Links: Wir erhalten ggf. eine Provision, für dich entstehen keine Mehrkosten.

Verdrahtung

ModulModul-PinESP32-PinHinweis
BME280 SDA / SCLI2CGPIO21 / GPIO22
VCC / GND3V3 / GND

Bibliotheken

Arduino IDE → Werkzeuge → Bibliotheken verwalten, dann installieren:

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

Fertige Firmware im Browser flashen Fehler in den KI-Doktor einfügen

Bautipps

  • Discovery-Nachricht mit retained-Flag und eine eindeutige Knoten-ID aus der MAC-Adresse nutzen.
  • Das Mosquitto-Add-on in Home Assistant aktivieren und einen eigenen MQTT-Benutzer anlegen.

← ← Alle Projekte

Ähnliche Ideen

ESP32 + Raspberry-Pi-Hub

ESP32-Knoten melden an Home Assistant auf einem Raspberry Pi: Verlauf, Diagramme und Automationen an einem Ort.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Fortgeschritten
  • Bauzeit 2–4 Std.
Teile / Set auswählen

Feinstaub-Monitor PM2.5

PMS5003-Partikelsensor plus BME280: PM1 / PM2.5 / PM10, Temperatur und Feuchte auf einer Webseite.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Fortgeschritten
  • Bauzeit 1–2 Std.
Teile / Set auswählen

Kommentare

Noch keine Kommentare. Teilen Sie Ihre Erfahrung oder stellen Sie eine Frage.

Kommentare werden moderiert.