JK BMS: Support for MQTT (#432)

* JK BMS: avoid trailing whitespace in debug output

* JK BMS: publish data points through MQTT

* 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.

* 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.
This commit is contained in:
Bernhard Kirchen 2023-09-13 12:14:29 +02:00 committed by GitHub
parent 88a5117007
commit 24018a1432
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 3 deletions

View File

@ -95,4 +95,6 @@ class JkBmsBatteryStats : public BatteryStats {
private: private:
JkBms::DataPointContainer _dataPoints; JkBms::DataPointContainer _dataPoints;
mutable uint32_t _lastMqttPublish = 0;
mutable uint32_t _lastFullMqttPublish = 0;
}; };

View File

@ -185,6 +185,10 @@ class DataPoint {
std::string const& getUnitText() const { return _strUnit; } std::string const& getUnitText() const { return _strUnit; }
uint32_t getTimestamp() const { return _timestamp; } uint32_t getTimestamp() const { return _timestamp; }
bool operator==(DataPoint const& other) const {
return _value == other._value;
}
private: private:
std::string _strLabel; std::string _strLabel;
std::string _strValue; std::string _strValue;

View File

@ -1,5 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include <vector>
#include <algorithm>
#include "BatteryStats.h" #include "BatteryStats.h"
#include "Configuration.h"
#include "MqttSettings.h" #include "MqttSettings.h"
#include "JkBmsDataPoints.h" #include "JkBmsDataPoints.h"
@ -145,6 +148,34 @@ void PylontechBatteryStats::mqttPublish() const
void JkBmsBatteryStats::mqttPublish() const void JkBmsBatteryStats::mqttPublish() const
{ {
BatteryStats::mqttPublish(); 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
};
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) void JkBmsBatteryStats::updateFrom(JkBms::DataPointContainer const& dp)

View File

@ -312,9 +312,9 @@ void Controller::frameComplete()
ts, _buffer.size()); ts, _buffer.size());
for (size_t ctr = 0; ctr < _buffer.size(); ++ctr) { for (size_t ctr = 0; ctr < _buffer.size(); ++ctr) {
if (ctr % 16 == 0) { 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(); MessageOutput.println();
} }

View File

@ -48,7 +48,14 @@ std::string dataPointValueToStr(tCells const& v) {
void DataPointContainer::updateFrom(DataPointContainer const& source) void DataPointContainer::updateFrom(DataPointContainer const& source)
{ {
for (auto iter = source.cbegin(); iter != source.cend(); ++iter) { 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); _dataPoints.insert(*iter);
} }
} }