Guest User

MMWAVE

a guest
Jan 3rd, 2026
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. /* ========= CONFIG ========= */
  5.  
  6. // WLAN
  7. const char* ssid = "SSID";
  8. const char* password = "PASSWORD";
  9.  
  10. // MQTT
  11. const char* mqtt_server = "192.168.1.110";
  12. const int mqtt_port = 1883;
  13.  
  14. // MQTT Login
  15. const char* mqtt_user = "mqtt-user";
  16. const char* mqtt_pass = "mqtt-user";
  17.  
  18. // MQTT Topics
  19. const char* topic_presence = "home/mmwave/presence";
  20. const char* topic_motion = "home/mmwave/motion";
  21.  
  22. // UART Pins ESP32-C3 Super Mini
  23. #define RX_PIN 4 // GPIO4 ← Sensor TX
  24. #define TX_PIN 5 // GPIO5 → Sensor RX
  25.  
  26. /* ========================== */
  27.  
  28. WiFiClient espClient;
  29. PubSubClient mqtt(espClient);
  30. HardwareSerial mmwave(1);
  31.  
  32. // Zustände
  33. bool lastPresence = false;
  34. bool lastMotion = false;
  35.  
  36. // UART Zeilenpuffer
  37. String lineBuffer = "";
  38.  
  39. // Radar-Logik
  40. int lastRange = -1;
  41. unsigned long lastValidMotion = 0;
  42.  
  43. // Feintuning (empfohlen)
  44. const int RANGE_CHANGE_THRESHOLD = 15; // Mindeständerung Range
  45. const unsigned long MOTION_TIMEOUT = 5000; // 5 s
  46. const unsigned long PRESENCE_TIMEOUT = 15000;// 15 s
  47.  
  48. /* ========= WIFI ========= */
  49.  
  50. void setupWifi() {
  51. Serial.print("Verbinde WLAN");
  52. WiFi.begin(ssid, password);
  53. while (WiFi.status() != WL_CONNECTED) {
  54. delay(500);
  55. Serial.print(".");
  56. }
  57. Serial.println("\nWLAN verbunden");
  58. Serial.print("IP: ");
  59. Serial.println(WiFi.localIP());
  60. }
  61.  
  62. /* ========= HOME ASSISTANT DISCOVERY ========= */
  63.  
  64. void publishHADiscovery() {
  65.  
  66. mqtt.publish(
  67. "homeassistant/binary_sensor/esp32c3_mmwave_motion/config",
  68. "{"
  69. "\"name\":\"mmWave Motion\","
  70. "\"uniq_id\":\"esp32c3_mmwave_motion\","
  71. "\"stat_t\":\"home/mmwave/motion\","
  72. "\"pl_on\":\"ON\","
  73. "\"pl_off\":\"OFF\","
  74. "\"dev_cla\":\"motion\""
  75. "}",
  76. true
  77. );
  78.  
  79. delay(200);
  80.  
  81. mqtt.publish(
  82. "homeassistant/binary_sensor/esp32c3_mmwave_presence/config",
  83. "{"
  84. "\"name\":\"mmWave Presence\","
  85. "\"uniq_id\":\"esp32c3_mmwave_presence\","
  86. "\"stat_t\":\"home/mmwave/presence\","
  87. "\"pl_on\":\"ON\","
  88. "\"pl_off\":\"OFF\","
  89. "\"dev_cla\":\"occupancy\""
  90. "}",
  91. true
  92. );
  93. }
  94.  
  95. /* ========= MQTT ========= */
  96.  
  97. void reconnectMQTT() {
  98. while (!mqtt.connected()) {
  99. Serial.print("Verbinde MQTT...");
  100. if (mqtt.connect("ESP32C3_MMWAVE", mqtt_user, mqtt_pass)) {
  101. Serial.println("OK");
  102.  
  103. publishHADiscovery();
  104.  
  105. mqtt.publish(topic_motion, "OFF", true);
  106. mqtt.publish(topic_presence, "OFF", true);
  107.  
  108. lastMotion = false;
  109. lastPresence = false;
  110. lastRange = -1;
  111.  
  112. } else {
  113. Serial.print("FEHLER rc=");
  114. Serial.println(mqtt.state());
  115. delay(2000);
  116. }
  117. }
  118. }
  119.  
  120. /* ========= SETUP ========= */
  121.  
  122. void setup() {
  123. Serial.begin(115200);
  124. delay(2000);
  125. Serial.println("ESP32 startet...");
  126.  
  127. setupWifi();
  128. mqtt.setServer(mqtt_server, mqtt_port);
  129.  
  130. mmwave.begin(
  131. 115200,
  132. SERIAL_8N1,
  133. RX_PIN,
  134. TX_PIN
  135. );
  136. }
  137.  
  138. /* ========= LOOP ========= */
  139.  
  140. void loop() {
  141. if (!mqtt.connected()) reconnectMQTT();
  142. mqtt.loop();
  143.  
  144. // UART ASCII lesen
  145. while (mmwave.available()) {
  146. char c = mmwave.read();
  147. if (c == '\n') {
  148. handleLine(lineBuffer);
  149. lineBuffer = "";
  150. } else if (c != '\r') {
  151. lineBuffer += c;
  152. }
  153. }
  154.  
  155. unsigned long now = millis();
  156.  
  157. // Motion OFF
  158. if (lastMotion && (now - lastValidMotion > MOTION_TIMEOUT)) {
  159. mqtt.publish(topic_motion, "OFF", true);
  160. lastMotion = false;
  161. }
  162.  
  163. // Presence OFF
  164. if (lastPresence && (now - lastValidMotion > PRESENCE_TIMEOUT)) {
  165. mqtt.publish(topic_presence, "OFF", true);
  166. lastPresence = false;
  167. }
  168. }
  169.  
  170. /* ========= HMM-D LINE PARSER ========= */
  171.  
  172. void handleLine(String line) {
  173. line.trim();
  174. if (line.length() == 0) return;
  175.  
  176. Serial.print("HMM-D: ");
  177. Serial.println(line);
  178.  
  179. // Wir ignorieren dauerhaftes "ON" und reagieren NUR auf Range-Änderung
  180. if (line.startsWith("Range ")) {
  181. int range = line.substring(6).toInt();
  182.  
  183. if (lastRange < 0) {
  184. lastRange = range;
  185. return;
  186. }
  187.  
  188. int diff = abs(range - lastRange);
  189.  
  190. if (diff >= RANGE_CHANGE_THRESHOLD) {
  191. lastValidMotion = millis();
  192.  
  193. if (!lastMotion) {
  194. mqtt.publish(topic_motion, "ON", true);
  195. lastMotion = true;
  196. }
  197.  
  198. if (!lastPresence) {
  199. mqtt.publish(topic_presence, "ON", true);
  200. lastPresence = true;
  201. }
  202. }
  203.  
  204. lastRange = range;
  205. }
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment