From 5316dba469bea3bd20a9c2070eee22d2185f1af1 Mon Sep 17 00:00:00 2001 From: Julian Langhoff Date: Sun, 16 May 2021 10:27:17 +0200 Subject: [PATCH] MQTT Last Will & Testament added --- esp-receiver/src/config.h.example | 16 +++++++++++----- esp-receiver/src/main.cpp | 4 +++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/esp-receiver/src/config.h.example b/esp-receiver/src/config.h.example index e0d203b..6a5ecdd 100644 --- a/esp-receiver/src/config.h.example +++ b/esp-receiver/src/config.h.example @@ -4,15 +4,21 @@ * For project specific settings. Copy to config.h and place your settings there. **/ +// MQTT-Configuration const char *MQTT_BROKER = "192.168.178.74"; -const char *MQTT_SENSOR_TOPIC = "living/sensor2"; +const char *MQTT_CLIENT_USER = "NULL"; +const char *MQTT_CLIENT_PW = "NULL"; const char *MQTT_CLIENT_ID = "living_mobile_sensor"; + +const char *MQTT_SENSOR_TOPIC = "living/sensor2"; const char *MQTT_LAST_WILL_TOPIC = "living/sensor2/status"; const char *MQTT_LAST_WILL_MSG = "offline"; -const char *MQTT_CLIENT_USER = "NULL"; // if NULL, no username or password is used -const char *MQTT_CLIENT_PW = "NULL"; // if NULL, no password is used +uint8_t MQTT_WILL_QOS = 1; // 0, 1 or 2 +boolean MQTT_WILL_RETAIN = true; -const int AirValue = 800; //replace the value with value when placed in air -const int WaterValue = 345; //replace the value with value when placed in water + +// Configuration for Soil-Sensor +const int AirValue = 700; //replace the value with value when placed in air (800) +const int WaterValue = 150; //replace the value with value when placed in water (345) #endif \ No newline at end of file diff --git a/esp-receiver/src/main.cpp b/esp-receiver/src/main.cpp index 593e519..54b8a52 100644 --- a/esp-receiver/src/main.cpp +++ b/esp-receiver/src/main.cpp @@ -225,9 +225,10 @@ void reconnect() // Attempt to connect // Should work without authentication, if credentials are NULL - if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_CLIENT_USER, MQTT_CLIENT_PW)) + if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_CLIENT_USER, MQTT_CLIENT_PW, MQTT_WILL_TOPIC, MQTT_WILL_QOS, MQTT_WILL_RETAIN, MQTT_WILL_MSG)) { Serial.println("INFO: connected"); + mqttClient.publish(MQTT_WILL_TOPIC, "online", true); } else { @@ -284,6 +285,7 @@ void publishData(float temp, float humid, float soil, long battery) char data[200]; serializeJson(doc, data); mqttClient.publish(MQTT_SENSOR_TOPIC, data, true); + yield(); } -- 2.40.1