From fb8d9e51a6a508fdcaf5c8c010c4243213da6e2b Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 29 Jan 2022 19:53:59 +0100 Subject: [PATCH] Option to send raw data frame to MQTT --- src/AmsToMqttBridge.ino | 23 +++++++++++++++++++++-- src/hexutils.cpp | 4 ++-- src/hexutils.h | 4 ++-- src/web/AmsWebServer.cpp | 1 + web/mqtt.html | 5 +++-- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 7549e4d9..ad9862b2 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -28,6 +28,7 @@ ADC_MODE(ADC_VCC); #include #include +#include "hexutils.h" #include "HwTools.h" #include "entsoe/EntsoeApi.h" @@ -785,8 +786,10 @@ bool readHanPort() { debugD("Authentication tag:"); debugPrint(hc->authentication_tag, 0, 12); } + if(mqttEnabled && mqtt != NULL && mqttHandler == NULL) { + mqtt->publish(topic.c_str(), toHex(buf, len)); + } len = 0; - while(hanSerial->available()) hanSerial->read(); if(pos > 0) { debugD("Valid data, start at byte %d", pos); data = IEC6205675(((char *) (buf)) + pos, meterState.getMeterType(), meterConfig.distributionSystem, timestamp, hc); @@ -842,6 +845,9 @@ bool readHanPort() { } } else if(currentMeterType == 2) { String payload = hanSerial->readString(); + if(mqttEnabled && mqtt != NULL && mqttHandler == NULL) { + mqtt->publish(topic.c_str(), payload); + } data = IEC6205621(payload); if(data.getListType() == 0) { currentMeterType = 1; @@ -1047,7 +1053,20 @@ void MQTT_connect() { mqtt->disconnect(); yield(); } else { - mqtt = new MQTTClient(512); + uint16_t size = 128; + switch(mqttConfig.payloadFormat) { + case 0: // JSON + size = 768; + break; + case 3: // Domoticz + size = 256; + break; + case 255: // Raw frame + size = 1024; + break; + } + + mqtt = new MQTTClient(size); ws.setMqtt(mqtt); } diff --git a/src/hexutils.cpp b/src/hexutils.cpp index dcbf3c03..230cedc4 100644 --- a/src/hexutils.cpp +++ b/src/hexutils.cpp @@ -4,7 +4,7 @@ String toHex(uint8_t* in) { return toHex(in, sizeof(in)*2); } -String toHex(uint8_t* in, uint8_t size) { +String toHex(uint8_t* in, uint16_t size) { String hex; for(int i = 0; i < size; i++) { if(in[i] < 0x10) { @@ -16,7 +16,7 @@ String toHex(uint8_t* in, uint8_t size) { return hex; } -void fromHex(uint8_t *out, String in, uint8_t size) { +void fromHex(uint8_t *out, String in, uint16_t size) { for(int i = 0; i < size*2; i += 2) { out[i/2] = strtol(in.substring(i, i+2).c_str(), 0, 16); } diff --git a/src/hexutils.h b/src/hexutils.h index 3f25773d..c5fa278c 100644 --- a/src/hexutils.h +++ b/src/hexutils.h @@ -5,7 +5,7 @@ #include "Arduino.h" String toHex(uint8_t* in); -String toHex(uint8_t* in, uint8_t size); -void fromHex(uint8_t *out, String in, uint8_t size); +String toHex(uint8_t* in, uint16_t size); +void fromHex(uint8_t *out, String in, uint16_t size); #endif \ No newline at end of file diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 774eece0..a02af330 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -551,6 +551,7 @@ void AmsWebServer::configMqttHtml() { for(int i = 0; i<4; i++) { html.replace("{f" + String(i) + "}", mqtt.payloadFormat == i ? "selected" : ""); } + html.replace("{f255}", mqtt.payloadFormat == 255 ? "selected" : ""); html.replace("{s}", mqtt.ssl ? "checked" : ""); diff --git a/web/mqtt.html b/web/mqtt.html index 9fefb1d7..bfd2b79f 100644 --- a/web/mqtt.html +++ b/web/mqtt.html @@ -59,9 +59,10 @@