Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JK BMS: Support for MQTT #432

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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