Skip to content

Commit

Permalink
JK BMS: Support for MQTT (#432)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
schlimmchen authored and helgeerbe committed Sep 15, 2023
1 parent f7bd4a4 commit 954a98d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/BatteryStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ class JkBmsBatteryStats : public BatteryStats {

private:
JkBms::DataPointContainer _dataPoints;
mutable uint32_t _lastMqttPublish = 0;
mutable uint32_t _lastFullMqttPublish = 0;
};
4 changes: 4 additions & 0 deletions include/JkBmsDataPoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions src/BatteryStats.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <vector>
#include <algorithm>
#include "BatteryStats.h"
#include "Configuration.h"
#include "MqttSettings.h"
#include "JkBmsDataPoints.h"

Expand Down Expand Up @@ -145,6 +148,34 @@ 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
};

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)
Expand Down
4 changes: 2 additions & 2 deletions src/JkBmsController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
9 changes: 8 additions & 1 deletion src/JkBmsDataPoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 954a98d

Please sign in to comment.