MQTT on interval

This commit is contained in:
Gunnar Skjold 2024-04-21 10:21:39 +02:00
parent efacbd4b31
commit 3f1861deda
11 changed files with 125 additions and 47 deletions

View File

@ -92,7 +92,10 @@ struct MqttConfig {
char password[256];
uint8_t payloadFormat;
bool ssl;
}; // 676
uint8_t magic;
bool stateUpdate;
uint16_t stateUpdateInterval;
}; // 680
struct WebConfig {
uint8_t security;

View File

@ -147,6 +147,11 @@ bool AmsConfiguration::getMqttConfig(MqttConfig& config) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_MQTT_START, config);
EEPROM.end();
if(config.magic != 0x7B) {
config.stateUpdate = false;
config.stateUpdateInterval = 10;
config.magic = 0x7B;
}
return true;
} else {
clearMqtt(config);
@ -166,6 +171,8 @@ bool AmsConfiguration::setMqttConfig(MqttConfig& config) {
mqttChanged |= strcmp(config.password, existing.password) != 0;
mqttChanged |= config.payloadFormat != existing.payloadFormat;
mqttChanged |= config.ssl != existing.ssl;
mqttChanged |= config.stateUpdate != existing.stateUpdate;
mqttChanged |= config.stateUpdateInterval != existing.stateUpdateInterval;
} else {
mqttChanged = true;
}
@ -195,6 +202,9 @@ void AmsConfiguration::clearMqtt(MqttConfig& config) {
memset(config.password, 0, 256);
config.payloadFormat = 0;
config.ssl = false;
config.magic = 0x7B;
config.stateUpdate = false;
config.stateUpdateInterval = 10;
}
void AmsConfiguration::setMqttChanged() {

View File

@ -65,6 +65,7 @@ protected:
WiFiClientSecure *mqttSecureClient = NULL;
char* json;
uint16_t BufferSize = 2048;
uint64_t lastStateUpdate = 0;
};
#endif

View File

@ -6,16 +6,29 @@
#include "DomoticzMqttHandler.h"
#include "json/domoticz_json.h"
#include "Uptime.h"
bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
bool DomoticzMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
bool ret = false;
AmsData data;
if(mqttConfig.stateUpdate) {
uint64_t now = millis64();
if(now-lastStateUpdate < mqttConfig.stateUpdateInterval * 1000) return false;
data.apply(*previousState);
data.apply(*update);
lastStateUpdate = now;
} else {
data = *update;
}
if (config.elidx > 0) {
if(data->getActiveImportCounter() > 1.0) {
energy = data->getActiveImportCounter();
if(data.getActiveImportCounter() > 1.0 && !data.isCounterEstimated()) {
energy = data.getActiveImportCounter();
}
if(energy > 0.0) {
char val[16];
snprintf_P(val, 16, PSTR("%.1f;%.1f"), (data->getActiveImportPower()/1.0), energy*1000.0);
snprintf_P(val, 16, PSTR("%.1f;%.1f"), (data.getActiveImportPower()/1.0), energy*1000.0);
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.elidx,
val
@ -25,12 +38,12 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
}
}
if(data->getListType() == 1)
if(data.getListType() == 1)
return ret;
if (config.vl1idx > 0){
char val[16];
snprintf_P(val, 16, PSTR("%.2f"), data->getL1Voltage());
snprintf_P(val, 16, PSTR("%.2f"), data.getL1Voltage());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl1idx,
val
@ -41,7 +54,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
if (config.vl2idx > 0){
char val[16];
snprintf_P(val, 16, PSTR("%.2f"), data->getL2Voltage());
snprintf_P(val, 16, PSTR("%.2f"), data.getL2Voltage());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl2idx,
val
@ -52,7 +65,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
if (config.vl3idx > 0){
char val[16];
snprintf(val, 16, "%.2f", data->getL3Voltage());
snprintf(val, 16, "%.2f", data.getL3Voltage());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl3idx,
val
@ -63,7 +76,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
if (config.cl1idx > 0){
char val[16];
snprintf(val, 16, "%.1f;%.1f;%.1f", data->getL1Current(), data->getL2Current(), data->getL3Current());
snprintf(val, 16, "%.1f;%.1f;%.1f", data.getL1Current(), data.getL2Current(), data.getL3Current());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.cl1idx,
val

View File

@ -20,31 +20,42 @@
#include <esp_task_wdt.h>
#endif
bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
bool HomeAssistantMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
if(topic.isEmpty() || !mqtt.connected())
return false;
if(time(nullptr) < FirmwareVersion::BuildEpoch)
return false;
if(data->getListType() >= 3) { // publish energy counts
publishList3(data, ea);
AmsData data;
if(mqttConfig.stateUpdate) {
uint64_t now = millis64();
if(now-lastStateUpdate < mqttConfig.stateUpdateInterval * 1000) return false;
data.apply(*previousState);
data.apply(*update);
lastStateUpdate = now;
} else {
data = *update;
}
if(data.getListType() >= 3 && !data.isCounterEstimated()) { // publish energy counts
publishList3(&data, ea);
mqtt.loop();
}
if(data->getListType() == 1) { // publish power counts
publishList1(data, ea);
if(data.getListType() == 1) { // publish power counts
publishList1(&data, ea);
mqtt.loop();
} else if(data->getListType() <= 3) { // publish power counts and volts/amps
publishList2(data, ea);
} else if(data.getListType() <= 3) { // publish power counts and volts/amps
publishList2(&data, ea);
mqtt.loop();
} else if(data->getListType() == 4) { // publish power counts and volts/amps/phase power and PF
publishList4(data, ea);
} else if(data.getListType() == 4) { // publish power counts and volts/amps/phase power and PF
publishList4(&data, ea);
mqtt.loop();
}
if(ea->isInitialized()) {
publishRealtime(data, ea, ps);
publishRealtime(&data, ea, ps);
mqtt.loop();
}
loop();

View File

@ -9,7 +9,7 @@
#include "hexutils.h"
#include "Uptime.h"
bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
bool JsonMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
if(strlen(mqttConfig.publishTopic) == 0) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Unable to publish data, no publish topic\n"));
return false;
@ -22,18 +22,29 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
bool ret = false;
memset(json, 0, BufferSize);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Publishing list ID %d!\n"), data->getListType());
if(data->getListType() == 1) {
ret = publishList1(data, ea);
AmsData data;
if(mqttConfig.stateUpdate) {
uint64_t now = millis64();
if(now-lastStateUpdate < mqttConfig.stateUpdateInterval * 1000) return false;
data.apply(*previousState);
data.apply(*update);
lastStateUpdate = now;
} else {
data = *update;
}
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Publishing list ID %d!\n"), data.getListType());
if(data.getListType() == 1) {
ret = publishList1(&data, ea);
mqtt.loop();
} else if(data->getListType() == 2) {
ret = publishList2(data, ea);
} else if(data.getListType() == 2) {
ret = publishList2(&data, ea);
mqtt.loop();
} else if(data->getListType() == 3) {
ret = publishList3(data, ea);
} else if(data.getListType() == 3) {
ret = publishList3(&data, ea);
mqtt.loop();
} else if(data->getListType() == 4) {
ret = publishList4(data, ea);
} else if(data.getListType() == 4) {
ret = publishList4(&data, ea);
mqtt.loop();
}
loop();

View File

@ -8,25 +8,36 @@
#include "hexutils.h"
#include "Uptime.h"
bool RawMqttHandler::publish(AmsData* data, AmsData* meterState, EnergyAccounting* ea, PriceService* ps) {
bool RawMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
if(topic.isEmpty() || !mqtt.connected())
return false;
if(data->getPackageTimestamp() > 0) {
mqtt.publish(topic + "/meter/dlms/timestamp", String(data->getPackageTimestamp()));
AmsData data;
if(mqttConfig.stateUpdate) {
uint64_t now = millis64();
if(now-lastStateUpdate < mqttConfig.stateUpdateInterval * 1000) return false;
data.apply(*previousState);
data.apply(*update);
lastStateUpdate = now;
} else {
data = *update;
}
switch(data->getListType()) {
if(data.getPackageTimestamp() > 0) {
mqtt.publish(topic + "/meter/dlms/timestamp", String(data.getPackageTimestamp()));
}
switch(data.getListType()) {
case 4:
publishList4(data, meterState);
publishList4(&data, previousState);
loop();
case 3:
publishList3(data, meterState);
publishList3(&data, previousState);
loop();
case 2:
publishList2(data, meterState);
publishList2(&data, previousState);
loop();
case 1:
publishList1(data, meterState);
publishList1(&data, previousState);
loop();
}
if(ea->isInitialized()) {

File diff suppressed because one or more lines are too long

View File

@ -614,6 +614,17 @@
{translations.conf?.mqtt?.publish ?? "Publish topic"}<br/>
<input name="qb" bind:value={configuration.q.b} type="text" class="in-s"/>
</div>
<div class="my-1">
{translations.conf?.mqtt?.update ?? "Update method"}
<span class="float-right">Interval</span>
<div class="flex">
<select name="qt" bind:value={configuration.q.t} class="in-f w-1/2">
<option value={0}>Real time</option>
<option value={1}>Interval</option>
</select>
<input name="qd" bind:value={configuration.q.d} type="number" min="1" max="3600" class="in-l tr w-1/2" disabled={configuration?.q?.t != 1}/>
</div>
</div>
</div>
{/if}
{#if configuration?.q?.m == 3}

View File

@ -11,5 +11,7 @@
"c": %s,
"r": %s,
"k": %s
}
},
"t": %d,
"d": %d
},

View File

@ -911,7 +911,9 @@ void AmsWebServer::configurationJson() {
mqttConfig.ssl ? "true" : "false",
qsc ? "true" : "false",
qsr ? "true" : "false",
qsk ? "true" : "false"
qsk ? "true" : "false",
mqttConfig.stateUpdate,
mqttConfig.stateUpdateInterval
);
server.sendContent(buf);
@ -1320,6 +1322,9 @@ void AmsWebServer::handleSave() {
if(mqtt.port == 0) {
mqtt.port = mqtt.ssl ? 8883 : 1883;
}
mqtt.stateUpdate = server.arg(F("qt")).toInt() == 1;
mqtt.stateUpdateInterval = server.arg(F("qd")).toInt();
} else {
config->clearMqtt(mqtt);
}