Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <PubSubClient.h>
- /* ========= CONFIG ========= */
- // WLAN
- const char* ssid = "SSID";
- const char* password = "PASSWORD";
- // MQTT
- const char* mqtt_server = "192.168.1.110";
- const int mqtt_port = 1883;
- // MQTT Login
- const char* mqtt_user = "mqtt-user";
- const char* mqtt_pass = "mqtt-user";
- // MQTT Topics
- const char* topic_presence = "home/mmwave/presence";
- const char* topic_motion = "home/mmwave/motion";
- // UART Pins ESP32-C3 Super Mini
- #define RX_PIN 4 // GPIO4 ← Sensor TX
- #define TX_PIN 5 // GPIO5 → Sensor RX
- /* ========================== */
- WiFiClient espClient;
- PubSubClient mqtt(espClient);
- HardwareSerial mmwave(1);
- // Zustände
- bool lastPresence = false;
- bool lastMotion = false;
- // UART Zeilenpuffer
- String lineBuffer = "";
- // Radar-Logik
- int lastRange = -1;
- unsigned long lastValidMotion = 0;
- // Feintuning (empfohlen)
- const int RANGE_CHANGE_THRESHOLD = 15; // Mindeständerung Range
- const unsigned long MOTION_TIMEOUT = 5000; // 5 s
- const unsigned long PRESENCE_TIMEOUT = 15000;// 15 s
- /* ========= WIFI ========= */
- void setupWifi() {
- Serial.print("Verbinde WLAN");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nWLAN verbunden");
- Serial.print("IP: ");
- Serial.println(WiFi.localIP());
- }
- /* ========= HOME ASSISTANT DISCOVERY ========= */
- void publishHADiscovery() {
- mqtt.publish(
- "homeassistant/binary_sensor/esp32c3_mmwave_motion/config",
- "{"
- "\"name\":\"mmWave Motion\","
- "\"uniq_id\":\"esp32c3_mmwave_motion\","
- "\"stat_t\":\"home/mmwave/motion\","
- "\"pl_on\":\"ON\","
- "\"pl_off\":\"OFF\","
- "\"dev_cla\":\"motion\""
- "}",
- true
- );
- delay(200);
- mqtt.publish(
- "homeassistant/binary_sensor/esp32c3_mmwave_presence/config",
- "{"
- "\"name\":\"mmWave Presence\","
- "\"uniq_id\":\"esp32c3_mmwave_presence\","
- "\"stat_t\":\"home/mmwave/presence\","
- "\"pl_on\":\"ON\","
- "\"pl_off\":\"OFF\","
- "\"dev_cla\":\"occupancy\""
- "}",
- true
- );
- }
- /* ========= MQTT ========= */
- void reconnectMQTT() {
- while (!mqtt.connected()) {
- Serial.print("Verbinde MQTT...");
- if (mqtt.connect("ESP32C3_MMWAVE", mqtt_user, mqtt_pass)) {
- Serial.println("OK");
- publishHADiscovery();
- mqtt.publish(topic_motion, "OFF", true);
- mqtt.publish(topic_presence, "OFF", true);
- lastMotion = false;
- lastPresence = false;
- lastRange = -1;
- } else {
- Serial.print("FEHLER rc=");
- Serial.println(mqtt.state());
- delay(2000);
- }
- }
- }
- /* ========= SETUP ========= */
- void setup() {
- Serial.begin(115200);
- delay(2000);
- Serial.println("ESP32 startet...");
- setupWifi();
- mqtt.setServer(mqtt_server, mqtt_port);
- mmwave.begin(
- 115200,
- SERIAL_8N1,
- RX_PIN,
- TX_PIN
- );
- }
- /* ========= LOOP ========= */
- void loop() {
- if (!mqtt.connected()) reconnectMQTT();
- mqtt.loop();
- // UART ASCII lesen
- while (mmwave.available()) {
- char c = mmwave.read();
- if (c == '\n') {
- handleLine(lineBuffer);
- lineBuffer = "";
- } else if (c != '\r') {
- lineBuffer += c;
- }
- }
- unsigned long now = millis();
- // Motion OFF
- if (lastMotion && (now - lastValidMotion > MOTION_TIMEOUT)) {
- mqtt.publish(topic_motion, "OFF", true);
- lastMotion = false;
- }
- // Presence OFF
- if (lastPresence && (now - lastValidMotion > PRESENCE_TIMEOUT)) {
- mqtt.publish(topic_presence, "OFF", true);
- lastPresence = false;
- }
- }
- /* ========= HMM-D LINE PARSER ========= */
- void handleLine(String line) {
- line.trim();
- if (line.length() == 0) return;
- Serial.print("HMM-D: ");
- Serial.println(line);
- // Wir ignorieren dauerhaftes "ON" und reagieren NUR auf Range-Änderung
- if (line.startsWith("Range ")) {
- int range = line.substring(6).toInt();
- if (lastRange < 0) {
- lastRange = range;
- return;
- }
- int diff = abs(range - lastRange);
- if (diff >= RANGE_CHANGE_THRESHOLD) {
- lastValidMotion = millis();
- if (!lastMotion) {
- mqtt.publish(topic_motion, "ON", true);
- lastMotion = true;
- }
- if (!lastPresence) {
- mqtt.publish(topic_presence, "ON", true);
- lastPresence = true;
- }
- }
- lastRange = range;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment