1
0

Added config options for type of mqtt message

Added toggle for Domoticz and custom Mqtt messages
This commit is contained in:
2021-12-12 18:23:10 +01:00
parent 8f56efefd5
commit 168b042366
2 changed files with 47 additions and 33 deletions

View File

@@ -13,6 +13,7 @@ IPAddress local_IP(192,168,1,10);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
IPAddress dns(1,1,1,1);
// MQTT Config
#define CONFIG_MQTT_HOST "<MQTT-Addres>"
#define CONFIG_MQTT_PORT 1883
@@ -21,7 +22,8 @@ IPAddress dns(1,1,1,1);
#define CONFIG_MQTT_CLIENT_ID "TempProbe"
//Domoticz Config
#define CONFIG_MQTT_TOPIC "domoticz/in"
#define CONFIG_DOMOTICZ_MSG 1
#define CONFIG_MQTT_DOMOTICZ_TOPIC "domoticz/in"
//IDX for virtual Temp + Humidity sensor
#define CONFIG_MQTT_IDX_Comb <TEMP+HUMidx>
//IDX for virtual Temp sensor
@@ -29,7 +31,8 @@ IPAddress dns(1,1,1,1);
//IDX for virtual Humidity sensor
#define CONFIG_MQTT_IDX_Hum <HUMidx>
//Custom mqtt msg
#define CONFIG_CUSTOM_MSG 1
//Temprature Custom Mqtt topic
#define CONFIG_MQTT_Temp_TOPIC "EST-C/<SensorName>/Temp"
//Humidity Custom Mqtt topi

View File

@@ -21,7 +21,7 @@ const char* mqttPassword = CONFIG_MQTT_PASS;
const char* mqttClientId = CONFIG_MQTT_CLIENT_ID;
//Domoticz
const char* domotopic = CONFIG_MQTT_TOPIC;
const char* domotopic = CONFIG_MQTT_DOMOTICZ_TOPIC;
const int idxcomb = CONFIG_MQTT_IDX_Comb;
const int idxtemp = CONFIG_MQTT_IDX_Temp;
const int idxhum = CONFIG_MQTT_IDX_Hum;
@@ -133,39 +133,50 @@ void loop()
Serial.print(cTemp);
Serial.println(" C");
StaticJsonDocument<100> JSONHum;
StaticJsonDocument<100> JSONTemp;
StaticJsonDocument<100> JSONComb;
//DomoticzMQTT
if (CONFIG_DOMOTICZ_MSG == 1){
StaticJsonDocument<100> JSONHum;
StaticJsonDocument<100> JSONTemp;
StaticJsonDocument<100> JSONComb;
JSONHum["idx"] = idxhum;
JSONHum["nvalue"] = RoundHumidity;
JSONHum["svalue"] = "0";
JSONHum["idx"] = idxhum;
JSONHum["nvalue"] = RoundHumidity;
JSONHum["svalue"] = "0";
JSONTemp["idx"] = idxtemp;
JSONTemp["nvalue"] = 0;
JSONTemp["svalue"] = String(cTemp) ;
JSONTemp["idx"] = idxtemp;
JSONTemp["nvalue"] = 0;
JSONTemp["svalue"] = String(cTemp) ;
JSONComb["idx"] = idxcomb;
JSONComb["nvalue"] = 0;
JSONComb["svalue"] = String(cTemp) + String(";") + String(CalcHumidity) + String(";0");
JSONComb["idx"] = idxcomb;
JSONComb["nvalue"] = 0;
JSONComb["svalue"] = String(cTemp) + String(";") + String(CalcHumidity) + String(";0");
char buffer1[256];
char buffer2[256];
char buffer3[256];
serializeJson(JSONTemp, buffer1);
serializeJson(JSONHum, buffer2);
serializeJson(JSONComb, buffer3);
char buffer1[256];
char buffer2[256];
char buffer3[256];
serializeJson(JSONTemp, buffer1);
serializeJson(JSONHum, buffer2);
serializeJson(JSONComb, buffer3);
if (client.publish(domotopic, buffer1) == true) {
Serial.println("Published Domoticz MQTT messages");
} else {
Serial.println("Error Domoticz sending MQTT messages");
}
client.publish(domotopic, buffer2);
client.publish(domotopic, buffer3);
}
if (client.publish(domotopic, buffer1) == true) {
Serial.println("Published MQTT messages");
} else {
Serial.println("Error sending MQTT messages");
}
client.publish(domotopic, buffer2);
client.publish(domotopic, buffer3);
//Custom Mqtt
client.publish(custom_temperature_topic, String(cTemp).c_str(), true);
client.publish(custom_humidity_topic, String(CalcHumidity).c_str(), true);
client.loop();
delay(60000); //1min
if (CONFIG_CUSTOM_MSG == 1){
if (client.publish(custom_temperature_topic, String(cTemp).c_str(), true) == true) {
Serial.println("Published Custom MQTT messages");
} else {
Serial.println("Error Custom sending MQTT messages");
}
client.publish(custom_humidity_topic, String(CalcHumidity).c_str(), true);
client.loop();
delay(60000); //1min
}
}