mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-02-06 00:25:55 +00:00
Improved MQTT stability and changed some defaults
This commit is contained in:
@@ -246,7 +246,13 @@ bool AmsConfiguration::getMeterConfig(MeterConfig& config) {
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
EEPROM.get(CONFIG_METER_START, config);
|
||||
EEPROM.end();
|
||||
if(config.bufferSize < 1 || config.bufferSize > 64) config.bufferSize = 4;
|
||||
if(config.bufferSize < 1 || config.bufferSize > 64) {
|
||||
#if defined(ESP32)
|
||||
config.bufferSize = 2;
|
||||
#else
|
||||
config.bufferSize = 1;
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
clearMeter(config);
|
||||
@@ -1043,6 +1049,13 @@ bool AmsConfiguration::relocateConfig103() {
|
||||
meter.rxPin = gpio103.hanPin;
|
||||
meter.txPin = 0xFF;
|
||||
meter.rxPinPullup = gpio103.hanPinPullup;
|
||||
meter.source = 1;
|
||||
meter.parser = 0;
|
||||
#if defined(ESP8266)
|
||||
if(meter.rxPin != 3 && meter.rxPin != 113) {
|
||||
meter.bufferSize = 1;
|
||||
}
|
||||
#endif
|
||||
wifi.mode = 1; // 1 == WiFi client
|
||||
wifi.ipv6 = false;
|
||||
|
||||
|
||||
@@ -123,6 +123,7 @@ bool AmsMqttHandler::connect() {
|
||||
}
|
||||
}
|
||||
mqtt.publish(statusTopic, "online", true, 0);
|
||||
mqtt.loop();
|
||||
return true;
|
||||
} else {
|
||||
if (debugger->isActive(RemoteDebug::ERROR)) {
|
||||
|
||||
@@ -21,6 +21,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
|
||||
val
|
||||
);
|
||||
ret = mqtt.publish(F("domoticz/in"), json);
|
||||
mqtt.loop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +36,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
|
||||
val
|
||||
);
|
||||
ret |= mqtt.publish(F("domoticz/in"), json);
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
if (config.vl2idx > 0){
|
||||
@@ -45,6 +47,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
|
||||
val
|
||||
);
|
||||
ret |= mqtt.publish(F("domoticz/in"), json);
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
if (config.vl3idx > 0){
|
||||
@@ -55,6 +58,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
|
||||
val
|
||||
);
|
||||
ret |= mqtt.publish(F("domoticz/in"), json);
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
if (config.cl1idx > 0){
|
||||
@@ -65,6 +69,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
|
||||
val
|
||||
);
|
||||
ret |= mqtt.publish(F("domoticz/in"), json);
|
||||
mqtt.loop();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -29,22 +29,25 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En
|
||||
|
||||
if(data->getListType() >= 3) { // publish energy counts
|
||||
publishList3(data, ea);
|
||||
loop();
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
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);
|
||||
mqtt.loop();
|
||||
} else if(data->getListType() == 4) { // publish power counts and volts/amps/phase power and PF
|
||||
publishList4(data, ea);
|
||||
mqtt.loop();
|
||||
}
|
||||
loop();
|
||||
|
||||
if(ea->isInitialized()) {
|
||||
publishRealtime(data, ea, ps);
|
||||
loop();
|
||||
mqtt.loop();
|
||||
}
|
||||
loop();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,12 +25,16 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
|
||||
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);
|
||||
mqtt.loop();
|
||||
} else if(data->getListType() == 3) {
|
||||
ret = publishList3(data, ea);
|
||||
mqtt.loop();
|
||||
} else if(data->getListType() == 4) {
|
||||
ret = publishList4(data, ea);
|
||||
mqtt.loop();
|
||||
}
|
||||
loop();
|
||||
return ret;
|
||||
|
||||
@@ -51,24 +51,28 @@ bool RawMqttHandler::publishList2(AmsData* data, AmsData* meterState) {
|
||||
if(full || meterState->getMeterModel() != data->getMeterModel()) {
|
||||
mqtt.publish(topic + "/meter/type", data->getMeterModel());
|
||||
}
|
||||
loop();
|
||||
if(full || meterState->getL1Current() != data->getL1Current()) {
|
||||
mqtt.publish(topic + "/meter/l1/current", String(data->getL1Current(), 2));
|
||||
}
|
||||
if(full || meterState->getL1Voltage() != data->getL1Voltage()) {
|
||||
mqtt.publish(topic + "/meter/l1/voltage", String(data->getL1Voltage(), 2));
|
||||
}
|
||||
loop();
|
||||
if(full || meterState->getL2Current() != data->getL2Current()) {
|
||||
mqtt.publish(topic + "/meter/l2/current", String(data->getL2Current(), 2));
|
||||
}
|
||||
if(full || meterState->getL2Voltage() != data->getL2Voltage()) {
|
||||
mqtt.publish(topic + "/meter/l2/voltage", String(data->getL2Voltage(), 2));
|
||||
}
|
||||
loop();
|
||||
if(full || meterState->getL3Current() != data->getL3Current()) {
|
||||
mqtt.publish(topic + "/meter/l3/current", String(data->getL3Current(), 2));
|
||||
}
|
||||
if(full || meterState->getL3Voltage() != data->getL3Voltage()) {
|
||||
mqtt.publish(topic + "/meter/l3/voltage", String(data->getL3Voltage(), 2));
|
||||
}
|
||||
loop();
|
||||
if(full || meterState->getReactiveExportPower() != data->getReactiveExportPower()) {
|
||||
mqtt.publish(topic + "/meter/export/reactive", String(data->getReactiveExportPower()));
|
||||
}
|
||||
@@ -96,69 +100,94 @@ bool RawMqttHandler::publishList3(AmsData* data, AmsData* meterState) {
|
||||
bool RawMqttHandler::publishList4(AmsData* data, AmsData* meterState) {
|
||||
if(full || meterState->getL1ActiveImportPower() != data->getL1ActiveImportPower()) {
|
||||
mqtt.publish(topic + "/meter/import/l1", String(data->getL1ActiveImportPower()));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL2ActiveImportPower() != data->getL2ActiveImportPower()) {
|
||||
mqtt.publish(topic + "/meter/import/l2", String(data->getL2ActiveImportPower()));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL3ActiveImportPower() != data->getL3ActiveImportPower()) {
|
||||
mqtt.publish(topic + "/meter/import/l3", String(data->getL3ActiveImportPower()));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL1ActiveExportPower() != data->getL1ActiveExportPower()) {
|
||||
mqtt.publish(topic + "/meter/export/l1", String(data->getL1ActiveExportPower()));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL2ActiveExportPower() != data->getL2ActiveExportPower()) {
|
||||
mqtt.publish(topic + "/meter/export/l2", String(data->getL2ActiveExportPower()));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL3ActiveExportPower() != data->getL3ActiveExportPower()) {
|
||||
mqtt.publish(topic + "/meter/export/l3", String(data->getL3ActiveExportPower()));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL1ActiveImportCounter() != data->getL1ActiveImportCounter()) {
|
||||
mqtt.publish(topic + "/meter/import/l1/accumulated", String(data->getL1ActiveImportCounter(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL2ActiveImportCounter() != data->getL2ActiveImportCounter()) {
|
||||
mqtt.publish(topic + "/meter/import/l2/accumulated", String(data->getL2ActiveImportCounter(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL3ActiveImportCounter() != data->getL3ActiveImportCounter()) {
|
||||
mqtt.publish(topic + "/meter/import/l3/accumulated", String(data->getL3ActiveImportCounter(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL1ActiveExportCounter() != data->getL1ActiveExportCounter()) {
|
||||
mqtt.publish(topic + "/meter/export/l1/accumulated", String(data->getL1ActiveExportCounter(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL2ActiveExportCounter() != data->getL2ActiveExportCounter()) {
|
||||
mqtt.publish(topic + "/meter/export/l2/accumulated", String(data->getL2ActiveExportCounter(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL3ActiveExportCounter() != data->getL3ActiveExportCounter()) {
|
||||
mqtt.publish(topic + "/meter/export/l3/accumulated", String(data->getL3ActiveExportCounter(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getPowerFactor() != data->getPowerFactor()) {
|
||||
mqtt.publish(topic + "/meter/powerfactor", String(data->getPowerFactor(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL1PowerFactor() != data->getL1PowerFactor()) {
|
||||
mqtt.publish(topic + "/meter/l1/powerfactor", String(data->getL1PowerFactor(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL2PowerFactor() != data->getL2PowerFactor()) {
|
||||
mqtt.publish(topic + "/meter/l2/powerfactor", String(data->getL2PowerFactor(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
if(full || meterState->getL3PowerFactor() != data->getL3PowerFactor()) {
|
||||
mqtt.publish(topic + "/meter/l3/powerfactor", String(data->getL3PowerFactor(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RawMqttHandler::publishRealtime(EnergyAccounting* ea) {
|
||||
mqtt.publish(topic + "/realtime/import/hour", String(ea->getUseThisHour(), 3));
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/realtime/import/day", String(ea->getUseToday(), 2));
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/realtime/import/month", String(ea->getUseThisMonth(), 1));
|
||||
mqtt.loop();
|
||||
uint8_t peakCount = ea->getConfig()->hours;
|
||||
if(peakCount > 5) peakCount = 5;
|
||||
for(uint8_t i = 1; i <= peakCount; i++) {
|
||||
mqtt.publish(topic + "/realtime/import/peak/" + String(i, 10), String(ea->getPeak(i).value / 100.0, 10), true, 0);
|
||||
mqtt.loop();
|
||||
}
|
||||
mqtt.publish(topic + "/realtime/import/threshold", String(ea->getCurrentThreshold(), 10), true, 0);
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/realtime/import/monthmax", String(ea->getMonthMax(), 3), true, 0);
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/realtime/export/hour", String(ea->getProducedThisHour(), 3));
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/realtime/export/day", String(ea->getProducedToday(), 2));
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/realtime/export/month", String(ea->getProducedThisMonth(), 1));
|
||||
mqtt.loop();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -169,6 +198,7 @@ bool RawMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw)
|
||||
if(data != NULL && data->lastValidRead > -85) {
|
||||
if(data->changed || full) {
|
||||
mqtt.publish(topic + "/temperature/" + toHex(data->address), String(data->lastValidRead, 2));
|
||||
mqtt.loop();
|
||||
data->changed = false;
|
||||
}
|
||||
}
|
||||
@@ -261,27 +291,31 @@ bool RawMqttHandler::publishPrices(PriceService* ps) {
|
||||
float val = values[i];
|
||||
if(val == PRICE_NO_VALUE) {
|
||||
mqtt.publish(topic + "/price/" + String(i), "", true, 0);
|
||||
mqtt.loop();
|
||||
} else {
|
||||
mqtt.publish(topic + "/price/" + String(i), String(val, 4), true, 0);
|
||||
mqtt.loop();
|
||||
}
|
||||
mqtt.loop();
|
||||
delay(10);
|
||||
}
|
||||
if(min != INT16_MAX) {
|
||||
mqtt.publish(topic + "/price/min", String(min, 4), true, 0);
|
||||
mqtt.loop();
|
||||
}
|
||||
if(max != INT16_MIN) {
|
||||
mqtt.publish(topic + "/price/max", String(max, 4), true, 0);
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
if(min1hrIdx != -1) {
|
||||
mqtt.publish(topic + "/price/cheapest/1hr", String(ts1hr), true, 0);
|
||||
mqtt.loop();
|
||||
}
|
||||
if(min3hrIdx != -1) {
|
||||
mqtt.publish(topic + "/price/cheapest/3hr", String(ts3hr), true, 0);
|
||||
mqtt.loop();
|
||||
}
|
||||
if(min6hrIdx != -1) {
|
||||
mqtt.publish(topic + "/price/cheapest/6hr", String(ts6hr), true, 0);
|
||||
mqtt.loop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -291,15 +325,21 @@ bool RawMqttHandler::publishSystem(HwTools* hw, PriceService* ps, EnergyAccounti
|
||||
return false;
|
||||
|
||||
mqtt.publish(topic + "/id", WiFi.macAddress(), true, 0);
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/uptime", String((uint32_t) (millis64()/1000)));
|
||||
mqtt.loop();
|
||||
float vcc = hw->getVcc();
|
||||
if(vcc > 0) {
|
||||
mqtt.publish(topic + "/vcc", String(vcc, 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
mqtt.publish(topic + "/mem", String(ESP.getFreeHeap()));
|
||||
mqtt.loop();
|
||||
mqtt.publish(topic + "/rssi", String(hw->getWifiRssi()));
|
||||
mqtt.loop();
|
||||
if(hw->getTemperature() > -85) {
|
||||
mqtt.publish(topic + "/temperature", String(hw->getTemperature(), 2));
|
||||
mqtt.loop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user