From b9429309a0f2eb1c5565b437f16ac82ffb1a88e7 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sat, 12 Sep 2020 22:40:55 +0200 Subject: [PATCH] Fixed influx tags, DSMR 4.0 compatibility --- Metertrekker2MQTT.ino | 51 +++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/Metertrekker2MQTT.ino b/Metertrekker2MQTT.ino index a3b6236..e913024 100644 --- a/Metertrekker2MQTT.ino +++ b/Metertrekker2MQTT.ino @@ -61,6 +61,8 @@ bool mqtt_publish(const String &topic_path, const String &message, bool retain = unsigned int interval; unsigned int timeout; +int dsmr_version = -1; + void setup() { Serial.begin(115200); @@ -107,14 +109,21 @@ void loop() Serial.printf("Telegram length: %zu\n", read_length); - char received_crc[5]; - P1.readBytes(received_crc, 4); - received_crc[4] = 0; + bool telegram_valid; + if (dsmr_version >= 42) { // before DSMR 4.2, telegrams did not contain a CRC tag at the end + char received_crc[5]; + P1.readBytes(received_crc, 4); + received_crc[4] = 0; + + Serial.printf("read CRC: %s\r\n", received_crc); - Serial.printf("read CRC: %s\r\n", received_crc); + telegram_valid = crcVerifyTelegram((byte*)buffer_in, read_length, received_crc); + } else { + telegram_valid = bracketVerifyTelegram(buffer_in, read_length); + } - if (verifyTelegram((byte*)buffer_in, read_length, received_crc)) { - Serial.print("Telegram valid!\r\n\n"); + if (telegram_valid) { + if (dsmr_version >= 42) Serial.print("Telegram valid!\r\n\n"); // can't be so sure from just counting the brackets (< DSMR 4.2) first_telegram = false; last_telegram_time = millis(); @@ -166,7 +175,7 @@ void timeoutHandler() } // Verify a telegram using a given CRC16 code -bool verifyTelegram(const byte* telegram, size_t length, const char* check_crc) +bool crcVerifyTelegram(const byte* telegram, size_t length, const char* check_crc) { char calculated_crc[5] = ""; @@ -177,6 +186,24 @@ bool verifyTelegram(const byte* telegram, size_t length, const char* check_crc) return strncmp(calculated_crc, check_crc, 4) == 0; } +bool bracketVerifyTelegram(const char* telegram, size_t length) +{ + int counter = 0; + for (size_t i = 0; i < length; i++) + { + switch (telegram[i]) { + case '(': + counter++; + break; + case ')': + counter--; + break; + } + } + + return counter == 0; +} + // Parse telegram, process contained metrics void parseTelegram(char* telegram) { @@ -314,7 +341,10 @@ void parseTelegram(char* telegram) case METRIC_TYPE_META: - if (strcmp("0-0:1.0.0", metric->ident) == 0) { // timestamp + if (strcmp("1-3:0.2.8", metric->ident) == 0) { // SMR protocol version + dsmr_version = value.toInt(); + + } else if (strcmp("0-0:1.0.0", metric->ident) == 0) { // timestamp #ifdef INFLUX if (strlen(metric->influx_column) > 0) append_influx_value(influx_fields, metric->influx_column, value, true); @@ -326,13 +356,10 @@ void parseTelegram(char* telegram) append_influx_value(influx_gas_tags, metric->influx_column, value, true); #endif - // } else if (strcmp("1-3:0.2.8", metric->ident) == 0) { // SMR protocol version - // //TODO: use this value to adjust protocol handling - } else { #ifdef INFLUX if (strlen(metric->influx_column) > 0) { - append_influx_value(influx_tags, metric->influx_column, value, true); + append_influx_value(influx_tags, metric->influx_column, value, false); // no quotes around tag values! } #endif }