Compare commits

...

6 Commits

Author SHA1 Message Date
Gunnar Skjold
ea91248e67 Changed MQTT client timeout setting for ESP8266 (#1077) 2025-12-05 15:37:23 +01:00
Gunnar Skjold
271ce2081f Fixed reboot loop for some meters (#1075) 2025-12-05 10:02:59 +01:00
Gunnar Skjold
8438020dbd Feature: Dump hex data from meter to MQTT (#1071)
* Send raw data debug to MQTT

* Publish hexdump to /data

* Sensor for /data, but it doesnt work
2025-12-01 14:02:07 +01:00
Gunnar Skjold
9252a810df Improve power stability when using MQTT (#1070)
* Changes to improve MQTT and power stability

* Re-added the memory leak fix

* Re-added the memory leak fix

* Stop client before deleting

* Fixed potential nullpointer
2025-12-01 10:01:20 +01:00
Gunnar Skjold
c0c696a55c Fixed default MQTT subscription (#1065) 2025-11-27 09:37:06 +01:00
Gunnar Skjold
ef70d39f70 Fixed what hours the fixed price is applied to (#1069) 2025-11-27 09:36:10 +01:00
19 changed files with 182 additions and 77 deletions

View File

@@ -43,6 +43,7 @@ public:
void setConfig(MqttConfig& mqttConfig); void setConfig(MqttConfig& mqttConfig);
bool connect(); bool connect();
bool defaultSubscribe();
void disconnect(); void disconnect();
lwmqtt_err_t lastError(); lwmqtt_err_t lastError();
bool connected(); bool connected();
@@ -56,11 +57,16 @@ public:
virtual bool publishTemperatures(AmsConfiguration*, HwTools*) { return false; }; virtual bool publishTemperatures(AmsConfiguration*, HwTools*) { return false; };
virtual bool publishPrices(PriceService* ps) { return false; }; virtual bool publishPrices(PriceService* ps) { return false; };
virtual bool publishSystem(HwTools*, PriceService*, EnergyAccounting*) { return false; }; virtual bool publishSystem(HwTools*, PriceService*, EnergyAccounting*) { return false; };
virtual bool publishRaw(String data) { return false; }; virtual bool publishRaw(uint8_t* raw, size_t length) { return false; };
virtual bool publishFirmware() { return false; }; virtual bool publishFirmware() { return false; };
virtual void onMessage(String &topic, String &payload) {}; virtual void onMessage(String &topic, String &payload) {};
virtual ~AmsMqttHandler() { virtual ~AmsMqttHandler() {
if(mqttSecureClient != NULL) {
mqttSecureClient->stop();
delete mqttSecureClient;
}
if(mqttClient != NULL) { if(mqttClient != NULL) {
mqttClient->stop(); mqttClient->stop();
delete mqttClient; delete mqttClient;
@@ -80,6 +86,7 @@ protected:
bool caVerification = true; bool caVerification = true;
WiFiClient *mqttClient = NULL; WiFiClient *mqttClient = NULL;
WiFiClientSecure *mqttSecureClient = NULL; WiFiClientSecure *mqttSecureClient = NULL;
boolean _connected = false;
char* json; char* json;
uint16_t BufferSize = 2048; uint16_t BufferSize = 2048;
uint64_t lastStateUpdate = 0; uint64_t lastStateUpdate = 0;

View File

@@ -103,6 +103,17 @@ bool AmsMqttHandler::connect() {
actualClient = mqttClient; actualClient = mqttClient;
} }
// This section helps with power saving on ESP32 devices by reducing timeouts
// The timeout is multiplied by 10 because WiFiClient is retrying 10 times internally
// Power drain for this timeout is too great when using the default 3s timeout
// On ESP8266 the timeout is used differently and the following code causes MQTT instability
#if defined(ESP32)
int clientTimeout = mqttConfig.timeout / 1000;
if(clientTimeout > 3) clientTimeout = 3; // 3000ms is default, see WiFiClient.cpp WIFI_CLIENT_DEF_CONN_TIMEOUT_MS
actualClient->setTimeout(clientTimeout);
// Why can't we set number of retries for write here? WiFiClient defaults to 10 (10*3s == 30s)
#endif
mqttConfigChanged = false; mqttConfigChanged = false;
mqtt.setTimeout(mqttConfig.timeout); mqtt.setTimeout(mqttConfig.timeout);
mqtt.setKeepAlive(mqttConfig.keepalive); mqtt.setKeepAlive(mqttConfig.keepalive);
@@ -125,8 +136,9 @@ bool AmsMqttHandler::connect() {
#endif #endif
debugger->printf_P(PSTR("Successfully connected to MQTT\n")); debugger->printf_P(PSTR("Successfully connected to MQTT\n"));
mqtt.onMessage(std::bind(&AmsMqttHandler::onMessage, this, std::placeholders::_1, std::placeholders::_2)); mqtt.onMessage(std::bind(&AmsMqttHandler::onMessage, this, std::placeholders::_1, std::placeholders::_2));
mqtt.publish(statusTopic, "online", true, 0); _connected = mqtt.publish(statusTopic, "online", true, 0);
mqtt.loop(); mqtt.loop();
defaultSubscribe();
postConnect(); postConnect();
return true; return true;
} else { } else {
@@ -146,13 +158,29 @@ bool AmsMqttHandler::connect() {
} }
} }
bool AmsMqttHandler::defaultSubscribe() {
bool ret = true;
if(!subTopic.isEmpty()) {
if(mqtt.subscribe(subTopic)) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR(" Subscribed to [%s]\n"), subTopic.c_str());
} else {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR(" Unable to subscribe to [%s]\n"), subTopic.c_str());
ret = false;
}
}
return ret;
}
void AmsMqttHandler::disconnect() { void AmsMqttHandler::disconnect() {
mqtt.disconnect(); mqtt.disconnect();
mqtt.loop(); mqtt.loop();
if(mqttSecureClient != NULL) { _connected = false;
delete mqttSecureClient;
mqttSecureClient = NULL;
}
delay(10); delay(10);
yield(); yield();
} }
@@ -162,12 +190,12 @@ lwmqtt_err_t AmsMqttHandler::lastError() {
} }
bool AmsMqttHandler::connected() { bool AmsMqttHandler::connected() {
return mqtt.connected(); return _connected && mqtt.connected();
} }
bool AmsMqttHandler::loop() { bool AmsMqttHandler::loop() {
uint64_t now = millis64(); uint64_t now = millis64();
bool ret = mqtt.connected() && mqtt.loop(); bool ret = connected() && mqtt.loop();
if(ret) { if(ret) {
lastSuccessfulLoop = now; lastSuccessfulLoop = now;
} else if(mqttConfig.rebootMinutes > 0) { } else if(mqttConfig.rebootMinutes > 0) {

View File

@@ -25,7 +25,7 @@ public:
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea); bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
bool publishRaw(String data); bool publishRaw(uint8_t* raw, size_t length);
void onMessage(String &topic, String &payload); void onMessage(String &topic, String &payload);

View File

@@ -103,7 +103,7 @@ uint8_t DomoticzMqttHandler::getFormat() {
return 3; return 3;
} }
bool DomoticzMqttHandler::publishRaw(String data) { bool DomoticzMqttHandler::publishRaw(uint8_t* raw, size_t length) {
return false; return false;
} }

View File

@@ -27,7 +27,7 @@ public:
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea); bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
bool publishRaw(String data); bool publishRaw(uint8_t* raw, size_t length);
bool publishFirmware(); bool publishFirmware();
bool postConnect(); bool postConnect();
@@ -51,7 +51,7 @@ private:
String updateTopic; String updateTopic;
String sensorNamePrefix; String sensorNamePrefix;
bool l1Init, l2Init, l2eInit, l3Init, l3eInit, l4Init, l4eInit, rtInit, rteInit, pInit, sInit, rInit, fInit; bool l1Init, l2Init, l2eInit, l3Init, l3eInit, l4Init, l4eInit, rtInit, rteInit, pInit, sInit, rInit, fInit, dInit;
bool tInit[32] = {false}; bool tInit[32] = {false};
uint8_t priceImportInit = 0, priceExportInit = 0; uint8_t priceImportInit = 0, priceExportInit = 0;
uint32_t lastThresholdPublish = 0; uint32_t lastThresholdPublish = 0;

View File

@@ -124,5 +124,6 @@ const HomeAssistantSensor SystemSensors[SystemSensorCount] PROGMEM = {
const HomeAssistantSensor TemperatureSensor PROGMEM = {"Temperature sensor %s", "/temperatures", "temperatures['%s']", 900, "°C", "temperature", "measurement", ""}; const HomeAssistantSensor TemperatureSensor PROGMEM = {"Temperature sensor %s", "/temperatures", "temperatures['%s']", 900, "°C", "temperature", "measurement", ""};
const HomeAssistantSensor DataSensor PROGMEM = {"Data", "/data", "data", 900, "", "", "", ""};
#endif #endif

View File

@@ -3,7 +3,6 @@
"stat_t" : "%s%s", "stat_t" : "%s%s",
"uniq_id" : "%s_%s", "uniq_id" : "%s_%s",
"obj_id" : "%s_%s", "obj_id" : "%s_%s",
"unit_of_meas" : "%s",
"val_tpl" : "{{ value_json.%s | is_defined }}", "val_tpl" : "{{ value_json.%s | is_defined }}",
"expire_after" : %d, "expire_after" : %d,
"dev" : { "dev" : {
@@ -13,5 +12,8 @@
"sw" : "%s", "sw" : "%s",
"mf" : "%s", "mf" : "%s",
"cu" : "%s" "cu" : "%s"
}%s%s%s%s%s%s }
%s%s%s
%s%s%s
%s%s%s
} }

View File

@@ -20,7 +20,7 @@
#endif #endif
void HomeAssistantMqttHandler::setHomeAssistantConfig(HomeAssistantConfig config, char* hostname) { void HomeAssistantMqttHandler::setHomeAssistantConfig(HomeAssistantConfig config, char* hostname) {
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = fInit = false; l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = fInit = dInit = false;
if(strlen(config.discoveryNameTag) > 0) { if(strlen(config.discoveryNameTag) > 0) {
snprintf_P(json, 128, PSTR("AMS reader (%s)"), config.discoveryNameTag); snprintf_P(json, 128, PSTR("AMS reader (%s)"), config.discoveryNameTag);
@@ -70,18 +70,26 @@ void HomeAssistantMqttHandler::setHomeAssistantConfig(HomeAssistantConfig config
} }
bool HomeAssistantMqttHandler::postConnect() { bool HomeAssistantMqttHandler::postConnect() {
if(!subTopic.isEmpty() && !mqtt.subscribe(subTopic)) { bool ret = true;
#if defined(AMS_REMOTE_DEBUG) if(!statusTopic.isEmpty()) {
if (debugger->isActive(RemoteDebug::ERROR)) if(mqtt.subscribe(statusTopic)) {
#endif #if defined(AMS_REMOTE_DEBUG)
debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), subTopic.c_str()); if (debugger->isActive(RemoteDebug::ERROR))
return false; #endif
debugger->printf_P(PSTR(" Subscribed to [%s]\n"), statusTopic.c_str());
} else {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR(" Unable to subscribe to [%s]\n"), statusTopic.c_str());
ret = false;
}
} }
return true; return ret;
} }
bool HomeAssistantMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) { bool HomeAssistantMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
if(pubTopic.isEmpty() || !mqtt.connected()) if(pubTopic.isEmpty() || !connected())
return false; return false;
if(time(nullptr) < FirmwareVersion::BuildEpoch) if(time(nullptr) < FirmwareVersion::BuildEpoch)
@@ -350,7 +358,7 @@ bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwT
} }
bool HomeAssistantMqttHandler::publishPrices(PriceService* ps) { bool HomeAssistantMqttHandler::publishPrices(PriceService* ps) {
if(pubTopic.isEmpty() || !mqtt.connected()) if(pubTopic.isEmpty() || !connected())
return false; return false;
if(!ps->hasPrice()) if(!ps->hasPrice())
return false; return false;
@@ -486,7 +494,7 @@ bool HomeAssistantMqttHandler::publishPrices(PriceService* ps) {
} }
bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea) { bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea) {
if(pubTopic.isEmpty() || !mqtt.connected()) if(pubTopic.isEmpty() || !connected())
return false; return false;
publishSystemSensors(); publishSystemSensors();
@@ -533,7 +541,6 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor sensor) {
mqttConfig.publishTopic, sensor.topic, mqttConfig.publishTopic, sensor.topic,
deviceUid.c_str(), uid.c_str(), deviceUid.c_str(), uid.c_str(),
deviceUid.c_str(), uid.c_str(), deviceUid.c_str(), uid.c_str(),
sensor.uom,
sensor.path, sensor.path,
sensor.ttl, sensor.ttl,
deviceUid.c_str(), deviceUid.c_str(),
@@ -542,13 +549,20 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor sensor) {
FirmwareVersion::VersionString, FirmwareVersion::VersionString,
manufacturer.c_str(), manufacturer.c_str(),
deviceUrl.c_str(), deviceUrl.c_str(),
strlen_P(sensor.devcl) > 0 ? ",\"dev_cla\":\"" : "", strlen_P(sensor.devcl) > 0 ? ",\"dev_cla\":\"" : "",
strlen_P(sensor.devcl) > 0 ? (char *) FPSTR(sensor.devcl) : "", strlen_P(sensor.devcl) > 0 ? (char *) FPSTR(sensor.devcl) : "",
strlen_P(sensor.devcl) > 0 ? "\"" : "", strlen_P(sensor.devcl) > 0 ? "\"" : "",
strlen_P(sensor.stacl) > 0 ? ",\"stat_cla\":\"" : "", strlen_P(sensor.stacl) > 0 ? ",\"stat_cla\":\"" : "",
strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : "", strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : "",
strlen_P(sensor.stacl) > 0 ? "\"" : "" strlen_P(sensor.stacl) > 0 ? "\"" : "",
strlen_P(sensor.uom) > 0 ? ",\"unit_of_meas\":\"" : "",
strlen_P(sensor.uom) > 0 ? (char *) FPSTR(sensor.uom) : "",
strlen_P(sensor.uom) > 0 ? "\"" : ""
); );
mqtt.publish(sensorTopic + "/" + deviceUid + "_" + uid + "/config", json, true, 0); mqtt.publish(sensorTopic + "/" + deviceUid + "_" + uid + "/config", json, true, 0);
loop(); loop();
} }
@@ -823,8 +837,26 @@ uint8_t HomeAssistantMqttHandler::getFormat() {
return 4; return 4;
} }
bool HomeAssistantMqttHandler::publishRaw(String data) { bool HomeAssistantMqttHandler::publishRaw(uint8_t* raw, size_t length) {
return false; if(strlen(mqttConfig.publishTopic) == 0 || !mqtt.connected())
return false;
if(length <= 0 || length > BufferSize) return false;
if(!dInit) {
// Not sure how this sensor should be defined in HA, so skipping for now
//publishSensor(DataSensor);
dInit = true;
}
String str = toHex(raw, length);
snprintf_P(json, BufferSize, PSTR("{\"data\":\"%s\"}"), str.c_str());
char topic[192];
snprintf_P(topic, 192, PSTR("%s/data"), mqttConfig.publishTopic);
bool ret = mqtt.publish(topic, json);
loop();
return ret;
} }
bool HomeAssistantMqttHandler::publishFirmware() { bool HomeAssistantMqttHandler::publishFirmware() {
@@ -857,7 +889,7 @@ void HomeAssistantMqttHandler::onMessage(String &topic, String &payload) {
if (debugger->isActive(RemoteDebug::INFO)) if (debugger->isActive(RemoteDebug::INFO))
#endif #endif
debugger->printf_P(PSTR("Received online status from HA, resetting sensor status\n")); debugger->printf_P(PSTR("Received online status from HA, resetting sensor status\n"));
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false; l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = fInit = dInit = false;
for(uint8_t i = 0; i < 32; i++) tInit[i] = false; for(uint8_t i = 0; i < 32; i++) tInit[i] = false;
priceImportInit = 0; priceImportInit = 0;
priceExportInit = 0; priceExportInit = 0;

View File

@@ -23,11 +23,9 @@ public:
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea); bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
bool publishRaw(String data); bool publishRaw(uint8_t* raw, size_t length);
bool publishFirmware(); bool publishFirmware();
bool postConnect();
void onMessage(String &topic, String &payload); void onMessage(String &topic, String &payload);
uint8_t getFormat(); uint8_t getFormat();

View File

@@ -10,22 +10,11 @@
#include "Uptime.h" #include "Uptime.h"
#include "AmsJsonGenerator.h" #include "AmsJsonGenerator.h"
bool JsonMqttHandler::postConnect() {
if(!subTopic.isEmpty() && !mqtt.subscribe(subTopic)) {
#if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::ERROR))
#endif
debugger->printf_P(PSTR(" Unable to subscribe to to [%s]\n"), subTopic.c_str());
return false;
}
return true;
}
bool JsonMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) { bool JsonMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
if(strlen(mqttConfig.publishTopic) == 0) { if(strlen(mqttConfig.publishTopic) == 0) {
return false; return false;
} }
if(!mqtt.connected()) { if(!connected()) {
return false; return false;
} }
@@ -306,7 +295,7 @@ bool JsonMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw)
} }
bool JsonMqttHandler::publishPrices(PriceService* ps) { bool JsonMqttHandler::publishPrices(PriceService* ps) {
if(strlen(mqttConfig.publishTopic) == 0 || !mqtt.connected()) if(strlen(mqttConfig.publishTopic) == 0 || !connected())
return false; return false;
if(!ps->hasPrice()) if(!ps->hasPrice())
return false; return false;
@@ -455,7 +444,7 @@ bool JsonMqttHandler::publishPrices(PriceService* ps) {
} }
bool JsonMqttHandler::publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea) { bool JsonMqttHandler::publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea) {
if(strlen(mqttConfig.publishTopic) == 0 || !mqtt.connected()) if(strlen(mqttConfig.publishTopic) == 0 || !connected())
return false; return false;
snprintf_P(json, BufferSize, PSTR("{\"id\":\"%s\",\"name\":\"%s\",\"up\":%d,\"vcc\":%.3f,\"rssi\":%d,\"temp\":%.2f,\"version\":\"%s\"}"), snprintf_P(json, BufferSize, PSTR("{\"id\":\"%s\",\"name\":\"%s\",\"up\":%d,\"vcc\":%.3f,\"rssi\":%d,\"temp\":%.2f,\"version\":\"%s\"}"),
@@ -483,8 +472,20 @@ uint8_t JsonMqttHandler::getFormat() {
return 0; return 0;
} }
bool JsonMqttHandler::publishRaw(String data) { bool JsonMqttHandler::publishRaw(uint8_t* raw, size_t length) {
return false; if(strlen(mqttConfig.publishTopic) == 0 || !mqtt.connected())
return false;
if(length <= 0 || length > BufferSize) return false;
String str = toHex(raw, length);
snprintf_P(json, BufferSize, PSTR("{\"data\":\"%s\"}"), str.c_str());
char topic[192];
snprintf_P(topic, 192, PSTR("%s/data"), mqttConfig.publishTopic);
bool ret = mqtt.publish(topic, json);
loop();
return ret;
} }
bool JsonMqttHandler::publishFirmware() { bool JsonMqttHandler::publishFirmware() {
@@ -502,7 +503,7 @@ bool JsonMqttHandler::publishFirmware() {
} }
void JsonMqttHandler::onMessage(String &topic, String &payload) { void JsonMqttHandler::onMessage(String &topic, String &payload) {
if(strlen(mqttConfig.publishTopic) == 0 || !mqtt.connected()) if(strlen(mqttConfig.publishTopic) == 0 || !connected())
return; return;
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)

View File

@@ -13,6 +13,7 @@
#endif #endif
#include "AmsData.h" #include "AmsData.h"
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "AmsMqttHandler.h"
class MeterCommunicator { class MeterCommunicator {
public: public:
@@ -24,6 +25,13 @@ public:
virtual bool isConfigChanged(); virtual bool isConfigChanged();
virtual void ackConfigChanged(); virtual void ackConfigChanged();
virtual void getCurrentConfig(MeterConfig& meterConfig); virtual void getCurrentConfig(MeterConfig& meterConfig);
virtual void setMqttHandlerForDebugging(AmsMqttHandler* mqttHandler) {
this->mqttDebug = mqttHandler;
};
protected:
AmsMqttHandler* mqttDebug = NULL;
}; };
#endif #endif

View File

@@ -14,7 +14,7 @@
#include "AmsConfiguration.h" #include "AmsConfiguration.h"
#include "DataParsers.h" #include "DataParsers.h"
#include "Timezone.h" #include "Timezone.h"
#include "PassthroughMqttHandler.h" #include "AmsMqttHandler.h"
#if defined(ESP8266) #if defined(ESP8266)
#include "SoftwareSerial.h" #include "SoftwareSerial.h"
@@ -36,7 +36,9 @@ public:
bool isConfigChanged(); bool isConfigChanged();
void ackConfigChanged(); void ackConfigChanged();
void getCurrentConfig(MeterConfig& meterConfig); void getCurrentConfig(MeterConfig& meterConfig);
void setPassthroughMqttHandler(PassthroughMqttHandler*); void setTimezone(Timezone* tz) {
this->tz = tz;
};
HardwareSerial* getHwSerial(); HardwareSerial* getHwSerial();
void rxerr(int err); void rxerr(int err);
@@ -51,8 +53,6 @@ protected:
bool configChanged = false; bool configChanged = false;
Timezone* tz; Timezone* tz;
PassthroughMqttHandler* pt = NULL;
uint8_t *hanBuffer = NULL; uint8_t *hanBuffer = NULL;
uint16_t hanBufferSize = 0; uint16_t hanBufferSize = 0;
Stream *hanSerial; Stream *hanSerial;

View File

@@ -27,13 +27,13 @@ IEC6205675::IEC6205675(const char* d, Timezone* tz, uint8_t useMeterType, MeterC
// Kaifa special case... // Kaifa special case...
if(useMeterType == AmsTypeKaifa && data->base.type == CosemTypeDLongUnsigned) { if(useMeterType == AmsTypeKaifa && data->base.type == CosemTypeDLongUnsigned) {
this->packageTimestamp = this->packageTimestamp > 0 ? tz->toUTC(this->packageTimestamp) : 0; this->packageTimestamp = this->packageTimestamp > 0 && tz != NULL ? tz->toUTC(this->packageTimestamp) : 0;
listType = 1; listType = 1;
meterType = AmsTypeKaifa; meterType = AmsTypeKaifa;
activeImportPower = ntohl(data->dlu.data); activeImportPower = ntohl(data->dlu.data);
lastUpdateMillis = millis64(); lastUpdateMillis = millis64();
} else if(data->base.type == CosemTypeOctetString) { } else if(data->base.type == CosemTypeOctetString) {
this->packageTimestamp = this->packageTimestamp > 0 ? tz->toUTC(this->packageTimestamp) : 0; this->packageTimestamp = this->packageTimestamp > 0 && tz != NULL ? tz->toUTC(this->packageTimestamp) : 0;
memcpy(str, data->oct.data, data->oct.length); memcpy(str, data->oct.data, data->oct.length);
str[data->oct.length] = 0x00; str[data->oct.length] = 0x00;
@@ -123,7 +123,7 @@ IEC6205675::IEC6205675(const char* d, Timezone* tz, uint8_t useMeterType, MeterC
if(data->oct.length == 0x0C) { if(data->oct.length == 0x0C) {
AmsOctetTimestamp* amst = (AmsOctetTimestamp*) data; AmsOctetTimestamp* amst = (AmsOctetTimestamp*) data;
time_t ts = decodeCosemDateTime(amst->dt); time_t ts = decodeCosemDateTime(amst->dt);
meterTimestamp = tz->toUTC(ts); meterTimestamp = tz != NULL ? tz->toUTC(ts) : ts;
} }
} }
} }
@@ -1121,7 +1121,7 @@ time_t IEC6205675::adjustForKnownIssues(CosemDateTime dt, Timezone* tz, uint8_t
// 21.09.24, the clock is now correct for Aidon // 21.09.24, the clock is now correct for Aidon
// 23.10.25, the clock is now correct for Kamstrup // 23.10.25, the clock is now correct for Kamstrup
ts -= 3600; ts -= 3600;
} else { } else if(tz != NULL) {
// Adjust from localtime to UTC // Adjust from localtime to UTC
ts = tz->toUTC(ts); ts = tz->toUTC(ts);
} }

View File

@@ -174,8 +174,8 @@ bool PassiveMeterCommunicator::loop() {
lastError = pos; lastError = pos;
printHanReadError(pos); printHanReadError(pos);
len += hanSerial->readBytes(hanBuffer+len, hanBufferSize-len); len += hanSerial->readBytes(hanBuffer+len, hanBufferSize-len);
if(pt != NULL) { if(mqttDebug != NULL) {
pt->publishBytes(hanBuffer, len); mqttDebug->publishRaw(hanBuffer, len);
} }
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
if (debugger->isActive(RemoteDebug::VERBOSE)) if (debugger->isActive(RemoteDebug::VERBOSE))
@@ -229,8 +229,8 @@ AmsData* PassiveMeterCommunicator::getData(AmsData& meterState) {
char* payload = ((char *) (hanBuffer)) + pos; char* payload = ((char *) (hanBuffer)) + pos;
if(maxDetectedPayloadSize < pos) maxDetectedPayloadSize = pos; if(maxDetectedPayloadSize < pos) maxDetectedPayloadSize = pos;
if(ctx.type == DATA_TAG_DLMS) { if(ctx.type == DATA_TAG_DLMS) {
if(pt != NULL) { if(mqttDebug != NULL) {
pt->publishBytes((uint8_t*) payload, ctx.length); mqttDebug->publishRaw((uint8_t*) payload, ctx.length);
} }
#if defined(AMS_REMOTE_DEBUG) #if defined(AMS_REMOTE_DEBUG)
@@ -405,8 +405,8 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co
if (debugger->isActive(RemoteDebug::VERBOSE)) if (debugger->isActive(RemoteDebug::VERBOSE))
#endif #endif
debugger->printf_P(PSTR("HDLC frame:\n")); debugger->printf_P(PSTR("HDLC frame:\n"));
if(pt != NULL) { if(mqttDebug != NULL) {
pt->publishBytes(buf, curLen); mqttDebug->publishRaw(buf, curLen);
} }
break; break;
case DATA_TAG_MBUS: case DATA_TAG_MBUS:
@@ -414,8 +414,8 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co
if (debugger->isActive(RemoteDebug::VERBOSE)) if (debugger->isActive(RemoteDebug::VERBOSE))
#endif #endif
debugger->printf_P(PSTR("MBUS frame:\n")); debugger->printf_P(PSTR("MBUS frame:\n"));
if(pt != NULL) { if(mqttDebug != NULL) {
pt->publishBytes(buf, curLen); mqttDebug->publishRaw(buf, curLen);
} }
break; break;
case DATA_TAG_GBT: case DATA_TAG_GBT:
@@ -447,8 +447,8 @@ int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &co
if (debugger->isActive(RemoteDebug::VERBOSE)) if (debugger->isActive(RemoteDebug::VERBOSE))
#endif #endif
debugger->printf_P(PSTR("DSMR frame:\n")); debugger->printf_P(PSTR("DSMR frame:\n"));
if(pt != NULL) { if(mqttDebug != NULL) {
pt->publishString((char*) buf); mqttDebug->publishRaw(buf, curLen);
} }
break; break;
case DATA_TAG_SNRM: case DATA_TAG_SNRM:

View File

@@ -242,9 +242,10 @@ float PriceService::getFixedPrice(uint8_t direction, int8_t hour) {
tmElements_t tm; tmElements_t tm;
breakTime(tz->toLocal(ts), tm); breakTime(tz->toLocal(ts), tm);
tm.Hour = hour;
tm.Minute = 0; tm.Minute = 0;
tm.Second = 0; tm.Second = 0;
breakTime(makeTime(tm) + (hour * SECS_PER_HOUR), tm); breakTime(makeTime(tm), tm);
float value = PRICE_NO_VALUE; float value = PRICE_NO_VALUE;
for (uint8_t i = 0; i < priceConfig.size(); i++) { for (uint8_t i = 0; i < priceConfig.size(); i++) {

View File

@@ -26,7 +26,7 @@ public:
bool publishTemperatures(AmsConfiguration*, HwTools*); bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(PriceService*); bool publishPrices(PriceService*);
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea); bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
bool publishRaw(String data); bool publishRaw(uint8_t* raw, size_t length);
void onMessage(String &topic, String &payload); void onMessage(String &topic, String &payload);

View File

@@ -10,7 +10,7 @@
#include "FirmwareVersion.h" #include "FirmwareVersion.h"
bool RawMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) { bool RawMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
if(topic.isEmpty() || !mqtt.connected()) if(topic.isEmpty() || !connected())
return false; return false;
AmsData data; AmsData data;
@@ -237,7 +237,7 @@ bool RawMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw)
} }
bool RawMqttHandler::publishPrices(PriceService* ps) { bool RawMqttHandler::publishPrices(PriceService* ps) {
if(topic.isEmpty() || !mqtt.connected()) if(topic.isEmpty() || !connected())
return false; return false;
if(!ps->hasPrice()) if(!ps->hasPrice())
return false; return false;
@@ -369,7 +369,7 @@ bool RawMqttHandler::publishPrices(PriceService* ps) {
} }
bool RawMqttHandler::publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea) { bool RawMqttHandler::publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea) {
if(topic.isEmpty() || !mqtt.connected()) if(topic.isEmpty() || !connected())
return false; return false;
mqtt.publish(topic + "/id", WiFi.macAddress(), true, 0); mqtt.publish(topic + "/id", WiFi.macAddress(), true, 0);
@@ -396,8 +396,16 @@ uint8_t RawMqttHandler::getFormat() {
return full ? 3 : 2; return full ? 3 : 2;
} }
bool RawMqttHandler::publishRaw(String data) { bool RawMqttHandler::publishRaw(uint8_t* raw, size_t length) {
return false; if(topic.isEmpty() || !connected())
return false;
if(length <= 0 || length > BufferSize) return false;
String str = toHex(raw, length);
bool ret = mqtt.publish(topic + "/data", str);
loop();
return ret;
} }
void RawMqttHandler::onMessage(String &topic, String &payload) { void RawMqttHandler::onMessage(String &topic, String &payload) {

View File

@@ -16,7 +16,8 @@
"day" : "day", "day" : "day",
"days" : "days", "days" : "days",
"month" : "month", "month" : "month",
"unknown" : "Unknown" "unknown" : "Unknown",
"now" : "Now"
}, },
"btn" : { "btn" : {
"reboot" : "Reboot", "reboot" : "Reboot",

View File

@@ -809,6 +809,9 @@ void handleNtp() {
ds.setTimezone(tz); ds.setTimezone(tz);
ea.setTimezone(tz); ea.setTimezone(tz);
ps->setTimezone(tz); ps->setTimezone(tz);
if(passiveMc != NULL) {
passiveMc->setTimezone(tz);
}
} }
config.ackNtpChange(); config.ackNtpChange();
@@ -902,7 +905,11 @@ void handleCloud() {
unsigned long handleMqtt() { unsigned long handleMqtt() {
unsigned long start = millis(); unsigned long start = millis();
if (mqttEnabled || config.isMqttChanged()) { if(!networkConnected) {
if(mqttHandler != NULL) {
mqttHandler->disconnect();
}
} else if (mqttEnabled || config.isMqttChanged()) {
if(mqttHandler == NULL || !mqttHandler->connected() || config.isMqttChanged()) { if(mqttHandler == NULL || !mqttHandler->connected() || config.isMqttChanged()) {
if(mqttHandler != NULL && config.isMqttChanged()) { if(mqttHandler != NULL && config.isMqttChanged()) {
mqttHandler->disconnect(); mqttHandler->disconnect();
@@ -1037,6 +1044,9 @@ void handleMeterConfig() {
debugE_P(PSTR("Unknown meter source selected: %d"), meterConfig.source); debugE_P(PSTR("Unknown meter source selected: %d"), meterConfig.source);
} }
ws.setMeterConfig(meterConfig.distributionSystem, meterConfig.mainFuse, meterConfig.productionCapacity); ws.setMeterConfig(meterConfig.distributionSystem, meterConfig.mainFuse, meterConfig.productionCapacity);
if(mc != NULL && Debug.isActive(RemoteDebug::DEBUG)) {
mc->setMqttHandlerForDebugging(mqttHandler);
}
config.ackMeterChanged(); config.ackMeterChanged();
} }
} }
@@ -1553,6 +1563,9 @@ void postConnect() {
if(!debug.telnet) { if(!debug.telnet) {
Debug.stop(); Debug.stop();
} }
if(mc != NULL && Debug.isActive(RemoteDebug::DEBUG)) {
mc->setMqttHandlerForDebugging(mqttHandler);
}
} else { } else {
Debug.stop(); Debug.stop();
} }
@@ -1628,6 +1641,7 @@ void MQTT_connect() {
if(mqttHandler->getFormat() != mqttConfig.payloadFormat) { if(mqttHandler->getFormat() != mqttConfig.payloadFormat) {
delete mqttHandler; delete mqttHandler;
mqttHandler = NULL; mqttHandler = NULL;
mc->setMqttHandlerForDebugging(NULL);
} else if(config.isMqttChanged()) { } else if(config.isMqttChanged()) {
mqttHandler->setConfig(mqttConfig); mqttHandler->setConfig(mqttConfig);
switch(mqttConfig.payloadFormat) { switch(mqttConfig.payloadFormat) {
@@ -1676,10 +1690,14 @@ void MQTT_connect() {
break; break;
case 255: case 255:
mqttHandler = new PassthroughMqttHandler(mqttConfig, &Debug, (char*) commonBuffer, &updater); mqttHandler = new PassthroughMqttHandler(mqttConfig, &Debug, (char*) commonBuffer, &updater);
mc->setMqttHandlerForDebugging(mqttHandler);
break; break;
} }
} }
ws.setMqttHandler(mqttHandler); ws.setMqttHandler(mqttHandler);
if(mc != NULL && Debug.isActive(RemoteDebug::DEBUG)) {
mc->setMqttHandlerForDebugging(mqttHandler);
}
if(mqttHandler != NULL) { if(mqttHandler != NULL) {
mqttHandler->connect(); mqttHandler->connect();