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

View File

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