From ba692fd89f753b34b1b200aebd921febf0d1b7fb Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen <schlimmchen@posteo.net> Date: Sat, 9 Sep 2023 18:29:15 +0200 Subject: [PATCH 1/4] JK BMS: avoid trailing whitespace in debug output --- src/JkBmsController.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JkBmsController.cpp b/src/JkBmsController.cpp index b1ef8b5aa..b6062dcf0 100644 --- a/src/JkBmsController.cpp +++ b/src/JkBmsController.cpp @@ -312,9 +312,9 @@ void Controller::frameComplete() ts, _buffer.size()); for (size_t ctr = 0; ctr < _buffer.size(); ++ctr) { if (ctr % 16 == 0) { - MessageOutput.printf("\r\n[%11.3f] JK BMS: ", ts); + MessageOutput.printf("\r\n[%11.3f] JK BMS:", ts); } - MessageOutput.printf("%02x ", _buffer[ctr]); + MessageOutput.printf(" %02x", _buffer[ctr]); } MessageOutput.println(); } From 84a02f8714b617c1bd1b5c4a2443dfa26e98a4dc Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen <schlimmchen@posteo.net> Date: Sat, 9 Sep 2023 18:31:00 +0200 Subject: [PATCH 2/4] JK BMS: publish data points through MQTT --- src/BatteryStats.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/BatteryStats.cpp b/src/BatteryStats.cpp index 2c4997e97..0698b162b 100644 --- a/src/BatteryStats.cpp +++ b/src/BatteryStats.cpp @@ -1,4 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later +#include <vector> +#include <algorithm> #include "BatteryStats.h" #include "MqttSettings.h" #include "JkBmsDataPoints.h" @@ -145,6 +147,22 @@ void PylontechBatteryStats::mqttPublish() const void JkBmsBatteryStats::mqttPublish() const { BatteryStats::mqttPublish(); + + using Label = JkBms::DataPointLabel; + + static std::vector<Label> mqttSkip = { + Label::CellsMilliVolt, // complex data format + Label::ModificationPassword, // sensitive data + Label::BatterySoCPercent // already published by base class + }; + + for (auto iter = _dataPoints.cbegin(); iter != _dataPoints.cend(); ++iter) { + auto skipMatch = std::find(mqttSkip.begin(), mqttSkip.end(), iter->first); + if (skipMatch != mqttSkip.end()) { continue; } + + String topic((std::string("battery/") + iter->second.getLabelText()).c_str()); + MqttSettings.publish(topic, iter->second.getValueText().c_str()); + } } void JkBmsBatteryStats::updateFrom(JkBms::DataPointContainer const& dp) From 0fcad491062062e96cf12274411ba80342f3e79f Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen <schlimmchen@posteo.net> Date: Sun, 10 Sep 2023 22:23:48 +0200 Subject: [PATCH 3/4] JK BMS: updateFrom: skip data points with equal value this changes the interpretation of the timestamp in data containers that are merely updated from other data containers: this is the oldest timestamp known where the value was as recorded by the data point in its respective container. the data container constructed from an answer will -- naturally -- have the timetamps of its data points set to the time they were constructed. --- include/JkBmsDataPoints.h | 4 ++++ src/JkBmsDataPoints.cpp | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/JkBmsDataPoints.h b/include/JkBmsDataPoints.h index db7cd37ab..bf3a0f9bd 100644 --- a/include/JkBmsDataPoints.h +++ b/include/JkBmsDataPoints.h @@ -185,6 +185,10 @@ class DataPoint { std::string const& getUnitText() const { return _strUnit; } uint32_t getTimestamp() const { return _timestamp; } + bool operator==(DataPoint const& other) const { + return _value == other._value; + } + private: std::string _strLabel; std::string _strValue; diff --git a/src/JkBmsDataPoints.cpp b/src/JkBmsDataPoints.cpp index 471b9daf1..c9377ee06 100644 --- a/src/JkBmsDataPoints.cpp +++ b/src/JkBmsDataPoints.cpp @@ -48,7 +48,14 @@ std::string dataPointValueToStr(tCells const& v) { void DataPointContainer::updateFrom(DataPointContainer const& source) { for (auto iter = source.cbegin(); iter != source.cend(); ++iter) { - _dataPoints.erase(iter->first); + auto pos = _dataPoints.find(iter->first); + + if (pos != _dataPoints.end()) { + // do not update existing data points with the same value + if (pos->second == iter->second) { continue; } + + _dataPoints.erase(pos); + } _dataPoints.insert(*iter); } } From 1a099dc97d633f149c98faf22c938f53215a5841 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen <schlimmchen@posteo.net> Date: Sun, 10 Sep 2023 22:26:46 +0200 Subject: [PATCH 4/4] JK BMS: only publish changed values to MQTT broker all values are still published once every minute if the MQTT retain flag is NOT set. otherwise, the constant values are only published once on startup. --- include/BatteryStats.h | 2 ++ src/BatteryStats.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/BatteryStats.h b/include/BatteryStats.h index ebdca11fd..6dc816a8c 100644 --- a/include/BatteryStats.h +++ b/include/BatteryStats.h @@ -95,4 +95,6 @@ class JkBmsBatteryStats : public BatteryStats { private: JkBms::DataPointContainer _dataPoints; + mutable uint32_t _lastMqttPublish = 0; + mutable uint32_t _lastFullMqttPublish = 0; }; diff --git a/src/BatteryStats.cpp b/src/BatteryStats.cpp index 0698b162b..fb1d2f2de 100644 --- a/src/BatteryStats.cpp +++ b/src/BatteryStats.cpp @@ -2,6 +2,7 @@ #include <vector> #include <algorithm> #include "BatteryStats.h" +#include "Configuration.h" #include "MqttSettings.h" #include "JkBmsDataPoints.h" @@ -156,13 +157,25 @@ void JkBmsBatteryStats::mqttPublish() const Label::BatterySoCPercent // already published by base class }; + CONFIG_T& config = Configuration.get(); + + // publish all topics every minute, unless the retain flag is enabled + bool fullPublish = _lastFullMqttPublish + 60 * 1000 < millis(); + fullPublish &= !config.Mqtt_Retain; + for (auto iter = _dataPoints.cbegin(); iter != _dataPoints.cend(); ++iter) { + // skip data points that did not change since last published + if (!fullPublish && iter->second.getTimestamp() < _lastMqttPublish) { continue; } + auto skipMatch = std::find(mqttSkip.begin(), mqttSkip.end(), iter->first); if (skipMatch != mqttSkip.end()) { continue; } String topic((std::string("battery/") + iter->second.getLabelText()).c_str()); MqttSettings.publish(topic, iter->second.getValueText().c_str()); } + + _lastMqttPublish = millis(); + if (fullPublish) { _lastFullMqttPublish = _lastMqttPublish; } } void JkBmsBatteryStats::updateFrom(JkBms::DataPointContainer const& dp)