When using raw payload with MQTT, only update if changed

This commit is contained in:
Gunnar Skjold 2020-03-10 18:57:25 +01:00
parent e12980db11
commit 97669cf4ad

View File

@ -349,6 +349,7 @@ void mqttMessageReceived(String &topic, String &payload)
// Ideas could be to query for values or to initiate OTA firmware update
}
AmsData lastMqttData;
void readHanPort() {
if (hanReader.read()) {
// Empty serial buffer. For some reason this seems to make a difference. Some garbage on the wire after package?
@ -385,27 +386,56 @@ void readHanPort() {
mqtt.publish(config.getMqttPublishTopic() + "/meter/dlms/timestamp", String(data.getPackageTimestamp()));
switch(data.getListType()) {
case 3:
// ID and type belongs to List 2, but I see no need to send that every 10s
mqtt.publish(config.getMqttPublishTopic() + "/meter/id", data.getMeterId());
mqtt.publish(config.getMqttPublishTopic() + "/meter/type", data.getMeterType());
mqtt.publish(config.getMqttPublishTopic() + "/meter/clock", String(data.getMeterTimestamp()));
mqtt.publish(config.getMqttPublishTopic() + "/meter/import/reactive/accumulated", String(data.getReactiveImportCounter(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/import/active/accumulated", String(data.getActiveImportCounter(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/export/reactive/accumulated", String(data.getReactiveExportCounter(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/export/active/accumulated", String(data.getActiveExportCounter(), 2));
case 2:
mqtt.publish(config.getMqttPublishTopic() + "/meter/id", data.getMeterId());
mqtt.publish(config.getMqttPublishTopic() + "/meter/type", data.getMeterType());
mqtt.publish(config.getMqttPublishTopic() + "/meter/l1/current", String(data.getL1Current(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/l1/voltage", String(data.getL1Voltage(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/l2/current", String(data.getL2Current(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/l2/voltage", String(data.getL2Voltage(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/l3/current", String(data.getL3Current(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/l3/voltage", String(data.getL3Voltage(), 2));
mqtt.publish(config.getMqttPublishTopic() + "/meter/export/reactive", String(data.getReactiveExportPower()));
mqtt.publish(config.getMqttPublishTopic() + "/meter/export/active", String(data.getActiveExportPower()));
mqtt.publish(config.getMqttPublishTopic() + "/meter/import/reactive", String(data.getReactiveImportPower()));
// Only send data if changed. ID and Type is sent on the 10s interval only if changed
if(lastMqttData.getMeterId() != data.getMeterId()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/id", data.getMeterId());
}
if(lastMqttData.getMeterType() != data.getMeterType()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/type", data.getMeterType());
}
if(lastMqttData.getL1Current() != data.getL1Current()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/l1/current", String(data.getL1Current(), 2));
}
if(lastMqttData.getL1Voltage() != data.getL1Voltage()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/l1/voltage", String(data.getL1Voltage(), 2));
}
if(lastMqttData.getL2Current() != data.getL2Current()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/l2/current", String(data.getL2Current(), 2));
}
if(lastMqttData.getL2Voltage() != data.getL2Voltage()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/l2/voltage", String(data.getL2Voltage(), 2));
}
if(lastMqttData.getL3Current() != data.getL3Current()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/l3/current", String(data.getL3Current(), 2));
}
if(lastMqttData.getL3Voltage() != data.getL3Voltage()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/l3/voltage", String(data.getL3Voltage(), 2));
}
if(lastMqttData.getReactiveExportPower() != data.getReactiveExportPower()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/export/reactive", String(data.getReactiveExportPower()));
}
if(lastMqttData.getActiveExportPower() != data.getActiveExportPower()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/export/active", String(data.getActiveExportPower()));
}
if(lastMqttData.getReactiveImportPower() != data.getReactiveImportPower()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/import/reactive", String(data.getReactiveImportPower()));
}
case 1:
mqtt.publish(config.getMqttPublishTopic() + "/meter/import/active", String(data.getActiveImportPower()));
if(lastMqttData.getActiveImportPower() != data.getActiveImportPower()) {
mqtt.publish(config.getMqttPublishTopic() + "/meter/import/active", String(data.getActiveImportPower()));
}
}
}
lastMqttData.apply(data);
mqtt.loop();
delay(10);
}