MQTT Last Will & Testament added #2

Open
julaho wants to merge 2 commits from MQTT-Testament-Messages into master
2 changed files with 14 additions and 6 deletions

View File

@ -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

View File

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