Skip to content

Commit

Permalink
Pytes battery: Add support for native CAN protocol
Browse files Browse the repository at this point in the history
The recently added PytesCanReceiver.cpp implements the Victron CAN protocol.

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
```

to retrun to DIP-switch based protocol setting:
```
login config
setprt DIP
logout
```

The protocol switch should take effect immediately.

Tested on Pytes E-Box 4850
  • Loading branch information
ranma committed Sep 22, 2024
1 parent e5230a0 commit 091fca3
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 20 deletions.
7 changes: 5 additions & 2 deletions include/BatteryStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ class PytesBatteryStats : public BatteryStats {
float _dischargeVoltageLimit;

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

float _temperature;

Expand All @@ -218,8 +220,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 Down
17 changes: 15 additions & 2 deletions src/BatteryStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ void PytesBatteryStats::getLiveViewData(JsonVariant& root) const
addLiveViewValue(root, "chargeCurrentLimitation", _chargeCurrentLimit, "A", 1);
addLiveViewValue(root, "dischargeVoltageLimitation", _dischargeVoltageLimit, "V", 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 @@ -201,6 +204,10 @@ void PytesBatteryStats::getLiveViewData(JsonVariant& root) const
}
addLiveViewTextValue(root, "chargeImmediately", (_chargeImmediately?"yes":"no"));

if (_balance != -1) {
addLiveViewTextValue(root, "balancingActive", (_balance?"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 @@ -430,6 +437,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
2 changes: 2 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 @@ -181,6 +182,7 @@ void MqttHandleBatteryHassClass::loop()
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 091fca3

Please sign in to comment.