Merge branch 'development'
This commit is contained in:
commit
4e489febfe
@ -95,4 +95,6 @@ class JkBmsBatteryStats : public BatteryStats {
|
||||
|
||||
private:
|
||||
JkBms::DataPointContainer _dataPoints;
|
||||
mutable uint32_t _lastMqttPublish = 0;
|
||||
mutable uint32_t _lastFullMqttPublish = 0;
|
||||
};
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
class HttpPowerMeterClass {
|
||||
public:
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -61,7 +61,6 @@ class Silent : public Print {
|
||||
static Silent MessageOutputDummy;
|
||||
|
||||
VeDirectFrameHandler::VeDirectFrameHandler() :
|
||||
//mStop(false), // don't know what Victron uses this for, not using
|
||||
_msgOut(&MessageOutputDummy),
|
||||
_state(IDLE),
|
||||
_checksum(0),
|
||||
@ -79,6 +78,7 @@ VeDirectFrameHandler::VeDirectFrameHandler() :
|
||||
void VeDirectFrameHandler::setVerboseLogging(bool verboseLogging)
|
||||
{
|
||||
_verboseLogging = verboseLogging;
|
||||
if (!_verboseLogging) { _debugIn = 0; }
|
||||
}
|
||||
|
||||
void VeDirectFrameHandler::init(int8_t rx, int8_t tx, Print* msgOut, bool verboseLogging)
|
||||
@ -127,13 +127,14 @@ void VeDirectFrameHandler::loop()
|
||||
*/
|
||||
void VeDirectFrameHandler::rxData(uint8_t inbyte)
|
||||
{
|
||||
if (_verboseLogging) {
|
||||
_debugBuffer[_debugIn] = inbyte;
|
||||
_debugIn = (_debugIn + 1) % _debugBuffer.size();
|
||||
if (0 == _debugIn) {
|
||||
_msgOut->println("[VE.Direct] ERROR: debug buffer overrun!");
|
||||
}
|
||||
}
|
||||
|
||||
//if (mStop) return;
|
||||
if ( (inbyte == ':') && (_state != CHECKSUM) ) {
|
||||
_prevState = _state; //hex frame can interrupt TEXT
|
||||
_state = RECORD_HEX;
|
||||
@ -284,7 +285,6 @@ void VeDirectFrameHandler::textRxEvent(char * name, char * value) {
|
||||
else if (strcmp(name, "H23") == 0) {
|
||||
_tmpFrame.H23 = atoi(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -14,19 +14,9 @@
|
||||
#include <Arduino.h>
|
||||
#include <array>
|
||||
|
||||
#ifndef VICTRON_PIN_TX
|
||||
#define VICTRON_PIN_TX 21 // HardwareSerial TX Pin
|
||||
#endif
|
||||
|
||||
#ifndef VICTRON_PIN_RX
|
||||
#define VICTRON_PIN_RX 22 // HardwareSerial RX Pin
|
||||
#endif
|
||||
|
||||
#define VE_MAX_NAME_LEN 9 // VE.Direct Protocol: max name size is 9 including /0
|
||||
#define VE_MAX_VALUE_LEN 33 // VE.Direct Protocol: max value size is 33 including /0
|
||||
#define VE_MAX_HEX_LEN 100 // Maximum size of hex frame - max payload 34 byte (=68 char) + safe buffer
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint16_t PID; // product id
|
||||
char SER[VE_MAX_VALUE_LEN]; // serial number
|
||||
@ -109,7 +99,6 @@ private:
|
||||
void frameEndEvent(bool); // copy temp struct to public struct
|
||||
int hexRxEvent(uint8_t);
|
||||
|
||||
//bool mStop; // not sure what Victron uses this for, not using
|
||||
Print* _msgOut;
|
||||
bool _verboseLogging;
|
||||
int _state; // current state
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include <FirebaseJson.h>
|
||||
#include <Crypto.h>
|
||||
#include <SHA256.h>
|
||||
#include <memory>
|
||||
|
||||
void HttpPowerMeterClass::init()
|
||||
{
|
||||
@ -23,39 +24,36 @@ bool HttpPowerMeterClass::updateValues()
|
||||
|
||||
char response[2000],
|
||||
errorMessage[256];
|
||||
bool success = true;
|
||||
|
||||
for (uint8_t i = 0; i < POWERMETER_MAX_PHASES; i++) {
|
||||
POWERMETER_HTTP_PHASE_CONFIG_T phaseConfig = config.Powermeter_Http_Phase[i];
|
||||
|
||||
if (!phaseConfig.Enabled || !success) {
|
||||
if (!phaseConfig.Enabled) {
|
||||
power[i] = 0.0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i == 0 || config.PowerMeter_HttpIndividualRequests) {
|
||||
if (!httpRequest(phaseConfig.Url, phaseConfig.AuthType, phaseConfig.Username, phaseConfig.Password, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout,
|
||||
if (httpRequest(phaseConfig.Url, phaseConfig.AuthType, phaseConfig.Username, phaseConfig.Password, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout,
|
||||
response, sizeof(response), errorMessage, sizeof(errorMessage))) {
|
||||
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed. Error: %s\r\n",
|
||||
i + 1, errorMessage);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!getFloatValueByJsonPath(response, phaseConfig.JsonPath, power[i])) {
|
||||
MessageOutput.printf("[HttpPowerMeter] Couldn't find a value with Json query \"%s\"\r\n", phaseConfig.JsonPath);
|
||||
success = false;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed. Error: %s\r\n",
|
||||
i + 1, errorMessage);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char* username, const char* password, const char* httpHeader, const char* httpValue, uint32_t timeout,
|
||||
char* response, size_t responseSize, char* error, size_t errorSize)
|
||||
{
|
||||
WiFiClient* wifiClient = NULL;
|
||||
HTTPClient httpClient;
|
||||
|
||||
String newUrl = url;
|
||||
String urlProtocol;
|
||||
@ -77,16 +75,21 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
|
||||
newUrl += urlUri;
|
||||
}
|
||||
|
||||
// secureWifiClient MUST be created before HTTPClient
|
||||
// see discussion: https://github.com/helgeerbe/OpenDTU-OnBattery/issues/381
|
||||
std::unique_ptr<WiFiClient> wifiClient;
|
||||
|
||||
if (urlProtocol == "https") {
|
||||
wifiClient = new WiFiClientSecure;
|
||||
reinterpret_cast<WiFiClientSecure*>(wifiClient)->setInsecure();
|
||||
auto secureWifiClient = std::make_unique<WiFiClientSecure>();
|
||||
secureWifiClient->setInsecure();
|
||||
wifiClient = std::move(secureWifiClient);
|
||||
} else {
|
||||
wifiClient = new WiFiClient;
|
||||
wifiClient = std::make_unique<WiFiClient>();
|
||||
}
|
||||
|
||||
HTTPClient httpClient;
|
||||
if (!httpClient.begin(*wifiClient, newUrl)) {
|
||||
snprintf_P(error, errorSize, "httpClient.begin(%s) failed", newUrl.c_str());
|
||||
delete wifiClient;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -179,7 +182,6 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
|
||||
}
|
||||
|
||||
httpClient.end();
|
||||
delete wifiClient;
|
||||
|
||||
if (error[0] != '\0') {
|
||||
return false;
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user