Skip to content

Commit

Permalink
The recently added PytesCanReceiver.cpp implements the Victron CAN pr…
Browse files Browse the repository at this point in the history
…otocol.

This change additionally adds support for the native Pytes CAN
protocol messages.

Features only supported in Pytes protocol:
- High-resolution state of charge / full and remaining mAh
- Charge cycle counter
- Balancing state

Features only supported in Victron protocol:
- FW version
- Serial number

Note that the only known way to select the native Pytes protocol is
via the serial console (Cisco-compatible cables work):

```
login config
setprt PYTES
logout
```

to return to Victron protocol use:
```
login config
setprt VICTRON
logout
```

The protocol switch will take effect immediately.

Tested on Pytes E-Box 4850

See #1188
  • Loading branch information
ranma committed Sep 7, 2024
1 parent cec4003 commit 69ed2b8
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/BatteryCanReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BatteryCanReceiver : public BatteryProvider {
uint16_t readUnsignedInt16(uint8_t *data);
int16_t readSignedInt16(uint8_t *data);
uint32_t readUnsignedInt32(uint8_t *data);
float scaleValue(int16_t value, float factor);
float scaleValue(int32_t value, float factor);
bool getBit(uint8_t value, uint8_t bit);

bool _verboseLogging = true;
Expand Down
10 changes: 8 additions & 2 deletions include/BatteryStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class PytesBatteryStats : public BatteryStats {
public:
void getLiveViewData(JsonVariant& root) const final;
void mqttPublish() const final;
bool getImmediateChargingRequest() const { return _chargeImmediately; } ;
float getChargeCurrentLimitation() const { return _chargeCurrentLimit; } ;

private:
Expand All @@ -154,6 +155,8 @@ class PytesBatteryStats : public BatteryStats {
float _dischargeCurrentLimit;

uint16_t _stateOfHealth;
int _chargeCycles = -1;
int _balance = -1;

float _temperature;

Expand All @@ -173,8 +176,9 @@ class PytesBatteryStats : public BatteryStats {
uint8_t _moduleCountBlockingCharge;
uint8_t _moduleCountBlockingDischarge;

uint16_t _totalCapacity;
uint16_t _availableCapacity;
float _totalCapacity;
float _availableCapacity;
uint8_t _capacityPrecision = 0; // decimal places

float _chargedEnergy = -1;
float _dischargedEnergy = -1;
Expand All @@ -200,6 +204,8 @@ class PytesBatteryStats : public BatteryStats {
bool _warningHighTemperatureCharge;
bool _warningInternalFailure;
bool _warningCellImbalance;

bool _chargeImmediately;
};

class JkBmsBatteryStats : public BatteryStats {
Expand Down
2 changes: 1 addition & 1 deletion src/BatteryCanReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ uint32_t BatteryCanReceiver::readUnsignedInt32(uint8_t *data)
return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
}

float BatteryCanReceiver::scaleValue(int16_t value, float factor)
float BatteryCanReceiver::scaleValue(int32_t value, float factor)
{
return value * factor;
}
Expand Down
19 changes: 17 additions & 2 deletions src/BatteryStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ void PytesBatteryStats::getLiveViewData(JsonVariant& root) const
addLiveViewValue(root, "dischargeVoltageLimitation", _dischargeVoltageLimit, "V", 1);
addLiveViewValue(root, "dischargeCurrentLimitation", _dischargeCurrentLimit, "A", 1);
addLiveViewValue(root, "stateOfHealth", _stateOfHealth, "%", 0);
if (_chargeCycles != -1) {
addLiveViewValue(root, "chargeCycles", _chargeCycles, "", 0);
}
addLiveViewValue(root, "temperature", _temperature, "°C", 1);

addLiveViewValue(root, "capacity", _totalCapacity, "Ah", 0);
addLiveViewValue(root, "availableCapacity", _availableCapacity, "Ah", 0);
addLiveViewValue(root, "capacity", _totalCapacity, "Ah", _capacityPrecision);
addLiveViewValue(root, "availableCapacity", _availableCapacity, "Ah", _capacityPrecision);

if (_chargedEnergy != -1) {
addLiveViewValue(root, "chargedEnergy", _chargedEnergy, "kWh", 1);
Expand All @@ -142,6 +145,11 @@ void PytesBatteryStats::getLiveViewData(JsonVariant& root) const
addLiveViewValue(root, "dischargedEnergy", _dischargedEnergy, "kWh", 1);
}

if (_balance != -1) {
addLiveViewTextValue(root, "balancingActive", (_balance?"yes":"no"));
}
addLiveViewTextValue(root, "chargeImmediately", (_chargeImmediately?"yes":"no"));

addLiveViewInSection(root, "cells", "cellMinVoltage", static_cast<float>(_cellMinMilliVolt)/1000, "V", 3);
addLiveViewInSection(root, "cells", "cellMaxVoltage", static_cast<float>(_cellMaxMilliVolt)/1000, "V", 3);
addLiveViewInSection(root, "cells", "cellDiffVoltage", (_cellMaxMilliVolt - _cellMinMilliVolt), "mV", 0);
Expand Down Expand Up @@ -347,6 +355,12 @@ void PytesBatteryStats::mqttPublish() const
MqttSettings.publish("battery/settings/dischargeVoltageLimitation", String(_dischargeVoltageLimit));

MqttSettings.publish("battery/stateOfHealth", String(_stateOfHealth));
if (_chargeCycles != -1) {
MqttSettings.publish("battery/chargeCycles", String(_chargeCycles));
}
if (_balance != -1) {
MqttSettings.publish("battery/balancingActive", String(_balance ? 1 : 0));
}
MqttSettings.publish("battery/temperature", String(_temperature));

if (_chargedEnergy != -1) {
Expand Down Expand Up @@ -396,6 +410,7 @@ void PytesBatteryStats::mqttPublish() const
MqttSettings.publish("battery/warning/highTemperatureCharge", String(_warningHighTemperatureCharge));
MqttSettings.publish("battery/warning/bmsInternal", String(_warningInternalFailure));
MqttSettings.publish("battery/warning/cellImbalance", String(_warningCellImbalance));
MqttSettings.publish("battery/charging/chargeImmediately", String(_chargeImmediately));
}

void JkBmsBatteryStats::mqttPublish() const
Expand Down
4 changes: 4 additions & 0 deletions src/MqttHandleBatteryHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void MqttHandleBatteryHassClass::loop()
publishSensor("Current", "mdi:current-dc", "current", "current", "measurement", "A");
publishSensor("State of Health (SOH)", "mdi:heart-plus", "stateOfHealth", NULL, "measurement", "%");
publishSensor("Temperature", "mdi:thermometer", "temperature", "temperature", "measurement", "°C");
publishSensor("Charge Cycles", "mdi:counter", "chargeCycles");

publishSensor("Charged Energy", NULL, "chargedEnergy", "energy", "total_increasing", "kWh");
publishSensor("Discharged Energy", NULL, "dischargedEnergy", "energy", "total_increasing", "kWh");
Expand Down Expand Up @@ -180,6 +181,9 @@ void MqttHandleBatteryHassClass::loop()
publishBinarySensor("Warning Temperature high (charge)", "mdi:thermometer-high", "warning/highTemperatureCharge", "1", "0");
publishBinarySensor("Warning BMS internal", "mdi:alert-outline", "warning/bmsInternal", "1", "0");
publishBinarySensor("Warning Cell Imbalance", "mdi:alert-outline", "warning/cellImbalance", "1", "0");

publishBinarySensor("Balancing Active", "mdi:scale-balance", "balancingActive", "1", "0");
publishBinarySensor("Charge immediately", "mdi:alert", "charging/chargeImmediately", "1", "0");
break;
}

Expand Down
Loading

0 comments on commit 69ed2b8

Please sign in to comment.