Skip to content

Commit 8438020

Browse files
authored
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
1 parent 9252a81 commit 8438020

15 files changed

Lines changed: 96 additions & 33 deletions

File tree

lib/AmsMqttHandler/include/AmsMqttHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class AmsMqttHandler {
5757
virtual bool publishTemperatures(AmsConfiguration*, HwTools*) { return false; };
5858
virtual bool publishPrices(PriceService* ps) { return false; };
5959
virtual bool publishSystem(HwTools*, PriceService*, EnergyAccounting*) { return false; };
60-
virtual bool publishRaw(String data) { return false; };
60+
virtual bool publishRaw(uint8_t* raw, size_t length) { return false; };
6161
virtual bool publishFirmware() { return false; };
6262
virtual void onMessage(String &topic, String &payload) {};
6363

lib/DomoticzMqttHandler/include/DomoticzMqttHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DomoticzMqttHandler : public AmsMqttHandler {
2525
bool publishTemperatures(AmsConfiguration*, HwTools*);
2626
bool publishPrices(PriceService*);
2727
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
28-
bool publishRaw(String data);
28+
bool publishRaw(uint8_t* raw, size_t length);
2929

3030
void onMessage(String &topic, String &payload);
3131

lib/DomoticzMqttHandler/src/DomoticzMqttHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ uint8_t DomoticzMqttHandler::getFormat() {
103103
return 3;
104104
}
105105

106-
bool DomoticzMqttHandler::publishRaw(String data) {
106+
bool DomoticzMqttHandler::publishRaw(uint8_t* raw, size_t length) {
107107
return false;
108108
}
109109

lib/HomeAssistantMqttHandler/include/HomeAssistantMqttHandler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class HomeAssistantMqttHandler : public AmsMqttHandler {
2727
bool publishTemperatures(AmsConfiguration*, HwTools*);
2828
bool publishPrices(PriceService*);
2929
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
30-
bool publishRaw(String data);
30+
bool publishRaw(uint8_t* raw, size_t length);
3131
bool publishFirmware();
3232

3333
bool postConnect();
@@ -51,7 +51,7 @@ class HomeAssistantMqttHandler : public AmsMqttHandler {
5151
String updateTopic;
5252
String sensorNamePrefix;
5353

54-
bool l1Init, l2Init, l2eInit, l3Init, l3eInit, l4Init, l4eInit, rtInit, rteInit, pInit, sInit, rInit, fInit;
54+
bool l1Init, l2Init, l2eInit, l3Init, l3eInit, l4Init, l4eInit, rtInit, rteInit, pInit, sInit, rInit, fInit, dInit;
5555
bool tInit[32] = {false};
5656
uint8_t priceImportInit = 0, priceExportInit = 0;
5757
uint32_t lastThresholdPublish = 0;

lib/HomeAssistantMqttHandler/include/HomeAssistantStatic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,6 @@ const HomeAssistantSensor SystemSensors[SystemSensorCount] PROGMEM = {
124124

125125
const HomeAssistantSensor TemperatureSensor PROGMEM = {"Temperature sensor %s", "/temperatures", "temperatures['%s']", 900, "°C", "temperature", "measurement", ""};
126126

127+
const HomeAssistantSensor DataSensor PROGMEM = {"Data", "/data", "data", 900, "", "", "", ""};
127128

128129
#endif

lib/HomeAssistantMqttHandler/json/hadiscover.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"stat_t" : "%s%s",
44
"uniq_id" : "%s_%s",
55
"obj_id" : "%s_%s",
6-
"unit_of_meas" : "%s",
76
"val_tpl" : "{{ value_json.%s | is_defined }}",
87
"expire_after" : %d,
98
"dev" : {
@@ -13,5 +12,8 @@
1312
"sw" : "%s",
1413
"mf" : "%s",
1514
"cu" : "%s"
16-
}%s%s%s%s%s%s
15+
}
16+
%s%s%s
17+
%s%s%s
18+
%s%s%s
1719
}

lib/HomeAssistantMqttHandler/src/HomeAssistantMqttHandler.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#endif
2121

2222
void HomeAssistantMqttHandler::setHomeAssistantConfig(HomeAssistantConfig config, char* hostname) {
23-
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = fInit = false;
23+
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = fInit = dInit = false;
2424

2525
if(strlen(config.discoveryNameTag) > 0) {
2626
snprintf_P(json, 128, PSTR("AMS reader (%s)"), config.discoveryNameTag);
@@ -541,7 +541,6 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor sensor) {
541541
mqttConfig.publishTopic, sensor.topic,
542542
deviceUid.c_str(), uid.c_str(),
543543
deviceUid.c_str(), uid.c_str(),
544-
sensor.uom,
545544
sensor.path,
546545
sensor.ttl,
547546
deviceUid.c_str(),
@@ -550,13 +549,20 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor sensor) {
550549
FirmwareVersion::VersionString,
551550
manufacturer.c_str(),
552551
deviceUrl.c_str(),
552+
553553
strlen_P(sensor.devcl) > 0 ? ",\"dev_cla\":\"" : "",
554554
strlen_P(sensor.devcl) > 0 ? (char *) FPSTR(sensor.devcl) : "",
555555
strlen_P(sensor.devcl) > 0 ? "\"" : "",
556+
556557
strlen_P(sensor.stacl) > 0 ? ",\"stat_cla\":\"" : "",
557558
strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : "",
558-
strlen_P(sensor.stacl) > 0 ? "\"" : ""
559+
strlen_P(sensor.stacl) > 0 ? "\"" : "",
560+
561+
strlen_P(sensor.uom) > 0 ? ",\"unit_of_meas\":\"" : "",
562+
strlen_P(sensor.uom) > 0 ? (char *) FPSTR(sensor.uom) : "",
563+
strlen_P(sensor.uom) > 0 ? "\"" : ""
559564
);
565+
560566
mqtt.publish(sensorTopic + "/" + deviceUid + "_" + uid + "/config", json, true, 0);
561567
loop();
562568
}
@@ -831,8 +837,26 @@ uint8_t HomeAssistantMqttHandler::getFormat() {
831837
return 4;
832838
}
833839

834-
bool HomeAssistantMqttHandler::publishRaw(String data) {
835-
return false;
840+
bool HomeAssistantMqttHandler::publishRaw(uint8_t* raw, size_t length) {
841+
if(strlen(mqttConfig.publishTopic) == 0 || !mqtt.connected())
842+
return false;
843+
844+
if(length <= 0 || length > BufferSize) return false;
845+
846+
if(!dInit) {
847+
// Not sure how this sensor should be defined in HA, so skipping for now
848+
//publishSensor(DataSensor);
849+
dInit = true;
850+
}
851+
852+
String str = toHex(raw, length);
853+
854+
snprintf_P(json, BufferSize, PSTR("{\"data\":\"%s\"}"), str.c_str());
855+
char topic[192];
856+
snprintf_P(topic, 192, PSTR("%s/data"), mqttConfig.publishTopic);
857+
bool ret = mqtt.publish(topic, json);
858+
loop();
859+
return ret;
836860
}
837861

838862
bool HomeAssistantMqttHandler::publishFirmware() {
@@ -865,7 +889,7 @@ void HomeAssistantMqttHandler::onMessage(String &topic, String &payload) {
865889
if (debugger->isActive(RemoteDebug::INFO))
866890
#endif
867891
debugger->printf_P(PSTR("Received online status from HA, resetting sensor status\n"));
868-
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = false;
892+
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = rInit = fInit = dInit = false;
869893
for(uint8_t i = 0; i < 32; i++) tInit[i] = false;
870894
priceImportInit = 0;
871895
priceExportInit = 0;

lib/JsonMqttHandler/include/JsonMqttHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class JsonMqttHandler : public AmsMqttHandler {
2323
bool publishTemperatures(AmsConfiguration*, HwTools*);
2424
bool publishPrices(PriceService*);
2525
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
26-
bool publishRaw(String data);
26+
bool publishRaw(uint8_t* raw, size_t length);
2727
bool publishFirmware();
2828

2929
void onMessage(String &topic, String &payload);

lib/JsonMqttHandler/src/JsonMqttHandler.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,20 @@ uint8_t JsonMqttHandler::getFormat() {
472472
return 0;
473473
}
474474

475-
bool JsonMqttHandler::publishRaw(String data) {
476-
return false;
475+
bool JsonMqttHandler::publishRaw(uint8_t* raw, size_t length) {
476+
if(strlen(mqttConfig.publishTopic) == 0 || !mqtt.connected())
477+
return false;
478+
479+
if(length <= 0 || length > BufferSize) return false;
480+
481+
String str = toHex(raw, length);
482+
483+
snprintf_P(json, BufferSize, PSTR("{\"data\":\"%s\"}"), str.c_str());
484+
char topic[192];
485+
snprintf_P(topic, 192, PSTR("%s/data"), mqttConfig.publishTopic);
486+
bool ret = mqtt.publish(topic, json);
487+
loop();
488+
return ret;
477489
}
478490

479491
bool JsonMqttHandler::publishFirmware() {

lib/MeterCommunicators/include/MeterCommunicator.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#endif
1414
#include "AmsData.h"
1515
#include "AmsConfiguration.h"
16+
#include "AmsMqttHandler.h"
1617

1718
class MeterCommunicator {
1819
public:
@@ -24,6 +25,13 @@ class MeterCommunicator {
2425
virtual bool isConfigChanged();
2526
virtual void ackConfigChanged();
2627
virtual void getCurrentConfig(MeterConfig& meterConfig);
28+
virtual void setMqttHandlerForDebugging(AmsMqttHandler* mqttHandler) {
29+
this->mqttDebug = mqttHandler;
30+
};
31+
32+
protected:
33+
AmsMqttHandler* mqttDebug = NULL;
34+
2735
};
2836

2937
#endif

0 commit comments

Comments
 (0)