Battery monitor: "not available" values decode as NAN

Each battery monitor field has a reserved "not available" raw value:
voltage 0x7FFF, aux 0xFFFF, current 0x3FFFFF, consumed Ah 0xFFFFF and
SOC 0x3FF. The parser decoded them as readings, so an unsynchronised
shunt reported SOC 102.3 % and an unavailable current came out as
-0.001 A after sign extension. They are now NAN in the C core.
Time-to-go stays an integer, with 0xFFFF named VICTRONBLE_TTG_NA. The
Arduino wrapper maps the new NANs to 0, as it already did for aux.

Adds a host test vector with every field not available, and notes in
the README that a battery monitor has now been decoded live on Zephyr
(ESP32-S3, Heltec WiFi LoRa 32 V3).
This commit is contained in:
scottp committed 2026-11-11 13:13:57 +11:00
1 parent 318e221170
commit 560ea24f60
9 files changed
+108 -31

No files matched your search

+12 -1
View File
@@ -32,12 +32,23 @@ Under Arduino it supports **ESP32** (original, S and C series — tested on olde
- **AC Chargers**: Blue Smart IP22 / IP65 / IP67 chargers
- **Others**: Smart Battery Protect, Lynx Smart BMS, Smart Lithium batteries
Tested against real hardware: solar chargers (SmartSolar MPPT) and, since
November 2026, a battery monitor, decoded live over Zephyr on an ESP32-S3
(Heltec WiFi LoRa 32 V3) alongside two MPPTs. The shunt voltage agreed
with both chargers to within 0.03 V. That test ran on a full battery in
float, so the sign of the current has not yet been checked under charge or
discharge on real hardware (the host test vectors cover it). A battery monitor
sends "not available" values for fields it cannot report (SOC and consumed
Ah before it has synchronised, time-to-go while idle, aux with nothing
wired). The C core returns those as `NAN`, and time-to-go as
`VICTRONBLE_TTG_NA`; the Arduino wrapper returns 0.
## Hardware Requirements
- An ESP32 (original / S / C series) **or** an nRF52840 board (Adafruit/Seeed
Bluefruit core — e.g. Seeed XIAO nRF52840) for the Arduino API
- Or any Zephyr-supported board with a Bluetooth controller (tested on
nRF52840DK and RAK4631)
nRF52840DK, RAK4631 and Heltec WiFi LoRa 32 V3)
- Victron devices with BLE "Instant Readout" enabled
Under Arduino the BLE backend is selected automatically at compile time from
+15
View File
@@ -1,5 +1,20 @@
# Version History
## Unreleased
### Fixed
- Battery monitor fields that carry Victron's "not available" value are now
NAN in the C core: voltage (0x7FFF), current (0x3FFFFF), aux voltage and
temperature (0xFFFF), consumed Ah (0xFFFFF) and SOC (0x3FF). Before this,
an unsynchronised shunt reported SOC 102.3 % and an unavailable current
decoded as -0.001 A. Time-to-go keeps its integer 0xFFFF, now named
`VICTRONBLE_TTG_NA`. The Arduino wrapper maps the new NANs to 0, matching
its existing convention.
- Host test vector for a battery monitor with every field not available.
### Tested
- Battery monitor decoded live on Zephyr (ESP32-S3, Heltec WiFi LoRa 32 V3).
## 0.7.1 (2026-10-09)
### Fixed
+13 -7
View File
@@ -87,14 +87,20 @@ typedef struct {
float load_current; /* A, NAN if no load output */
} victronble_solar_charger_t;
/** Battery monitor time-to-go value meaning "not available". */
#define VICTRONBLE_TTG_NA 0xFFFFu
typedef struct {
float voltage; /* V */
float current; /* A */
float temperature; /* degC, NAN unless aux mode = temperature */
float aux_voltage; /* V, NAN unless aux mode = aux voltage */
uint16_t remaining_minutes; /* time-to-go; 0xFFFF = not available */
float consumed_ah; /* Ah, negative = consumed */
float soc; /* % */
float voltage; /* V, NAN if not available */
float current; /* A, NAN if not available */
float temperature; /* degC, NAN unless aux mode = temperature
* and a reading is available */
float aux_voltage; /* V, NAN unless aux mode = aux voltage
* and a reading is available */
uint16_t remaining_minutes; /* time-to-go; VICTRONBLE_TTG_NA = not
* available (e.g. idle or not synced) */
float consumed_ah; /* Ah, negative = consumed; NAN if n/a */
float soc; /* %, NAN if not available (not synced) */
uint8_t aux_mode; /* 0=aux V, 1=midpoint, 2=temperature, 3=none */
uint16_t alarm; /* raw 16-bit alarm bitmask */
} victronble_battery_monitor_t;
+4 -4
View File
@@ -130,13 +130,13 @@ void VictronBLE::storeRecord(DeviceEntry* entry, const victronble_record_t& rec)
case VICTRONBLE_DEV_BATTERY_MONITOR: {
entry->device.deviceType = DEVICE_TYPE_BATTERY_MONITOR;
VictronBatteryData& b = entry->device.battery;
b.voltage = rec.u.batmon.voltage;
b.current = rec.u.batmon.current;
b.voltage = nan_to_zero(rec.u.batmon.voltage);
b.current = nan_to_zero(rec.u.batmon.current);
b.temperature = nan_to_zero(rec.u.batmon.temperature);
b.auxVoltage = nan_to_zero(rec.u.batmon.aux_voltage);
b.remainingMinutes = rec.u.batmon.remaining_minutes;
b.consumedAh = rec.u.batmon.consumed_ah;
b.soc = rec.u.batmon.soc;
b.consumedAh = nan_to_zero(rec.u.batmon.consumed_ah);
b.soc = nan_to_zero(rec.u.batmon.soc);
b.alarmLowVoltage = (rec.u.batmon.alarm & 0x0001) != 0;
b.alarmHighVoltage = (rec.u.batmon.alarm & 0x0002) != 0;
b.alarmLowSOC = (rec.u.batmon.alarm & 0x0004) != 0;
+7 -7
View File
@@ -152,13 +152,13 @@ struct VictronACChargerData {
};
struct VictronBatteryData {
float voltage; // V
float current; // A
float temperature; // C (0 if aux is voltage)
float auxVoltage; // V (0 if aux is temperature)
uint16_t remainingMinutes;
float consumedAh; // Ah
float soc; // %
float voltage; // V (0 if not available)
float current; // A (0 if not available)
float temperature; // C (0 if aux is voltage or not available)
float auxVoltage; // V (0 if aux is temperature or not available)
uint16_t remainingMinutes; // 0xFFFF if not available
float consumedAh; // Ah (0 if not available)
float soc; // % (0 if not available)
bool alarmLowVoltage;
bool alarmHighVoltage;
bool alarmLowSOC;
+29 -12
View File
@@ -105,34 +105,51 @@ static bool parse_battery_monitor(const uint8_t *d, size_t len,
return false;
}
/* Every field has a "not available" raw value (all ones; 0x7FFF for the
* signed voltage). The shunt sends them before it has synchronised, and
* aux values when nothing is wired. Those map to NAN here, except TTG,
* which stays 0xFFFF (VICTRONBLE_TTG_NA) because it is an integer. */
r->remaining_minutes = get_le16(d); /* bits 0-15 */
r->voltage = (int16_t)get_le16(d + 2) * 0.01f; /* bits 16-31 */
uint16_t v_raw = get_le16(d + 2); /* bits 16-31 */
r->voltage = (v_raw != 0x7FFF) ? (int16_t)v_raw * 0.01f : NAN;
r->alarm = get_le16(d + 4); /* bits 32-47 */
/* Aux value (bits 48-63) interpreted per aux mode (bits 64-65) */
uint16_t aux_raw = get_le16(d + 6);
bool aux_ok = (aux_raw != 0xFFFF);
r->aux_mode = d[8] & 0x03;
r->aux_voltage = (r->aux_mode == 0) ? aux_raw * 0.01f : NAN;
r->temperature = (r->aux_mode == 2) ? aux_raw * 0.01f - 273.15f : NAN;
r->aux_voltage = (r->aux_mode == 0 && aux_ok) ? aux_raw * 0.01f : NAN;
r->temperature = (r->aux_mode == 2 && aux_ok) ?
aux_raw * 0.01f - 273.15f : NAN;
/* Battery current (bits 66-87), 22-bit signed, 0.001 A units */
int32_t current = (int32_t)(((uint32_t)(d[8] >> 2) & 0x3F) |
((uint32_t)d[9] << 6) |
((uint32_t)d[10] << 14));
if (current & 0x200000) {
current |= (int32_t)0xFFC00000; /* sign extend */
/* Battery current (bits 66-87), 22-bit signed, 0.001 A units. Test the
* not-available value before sign extension: 0x3FFFFF would otherwise
* read as a plausible -0.001 A. */
uint32_t i_raw = ((uint32_t)(d[8] >> 2) & 0x3F) |
((uint32_t)d[9] << 6) |
((uint32_t)d[10] << 14);
i_raw &= 0x3FFFFF;
if (i_raw == 0x3FFFFF) {
r->current = NAN;
} else {
int32_t current = (int32_t)i_raw;
if (current & 0x200000) {
current |= (int32_t)0xFFC00000; /* sign extend */
}
r->current = current * 0.001f;
}
r->current = current * 0.001f;
/* Consumed Ah (bits 88-107), 20-bit positive count, 0.1 Ah units,
* reported negative (amp-hours consumed). */
uint32_t consumed = (uint32_t)d[11] | ((uint32_t)d[12] << 8) |
((uint32_t)(d[13] & 0x0F) << 16);
r->consumed_ah = -((float)consumed * 0.1f);
r->consumed_ah = (consumed != 0xFFFFF) ?
-((float)consumed * 0.1f) : NAN;
/* SOC (bits 108-117), 10-bit, 0.1 % units */
uint16_t soc = (uint16_t)(((d[13] >> 4) | ((uint16_t)d[14] << 4)) & 0x3FF);
r->soc = soc * 0.1f;
r->soc = (soc != 0x3FF) ? soc * 0.1f : NAN;
return true;
}
+14
View File
@@ -76,6 +76,19 @@ batmon = pad21(pack_bits([
(855, 10), # SOC, 0.1 %
]))
# Battery monitor before it has synchronised, with nothing on aux: every
# field carries Victron's "not available" value.
batmon_na = pad21(pack_bits([
(0xFFFF, 16), # TTG n/a
(0x7FFF, 16), # voltage n/a
(0x0000, 16), # no alarms
(0xFFFF, 16), # aux n/a
(0, 2), # aux mode = aux voltage
(0x3FFFFF, 22), # current n/a
(0xFFFFF, 20), # consumed n/a
(0x3FF, 10), # SOC n/a
]))
# Inverter: inverting, 25.86 V, -12.34 A, -230 W, overload alarm.
inverter = pad21(bytes([9, 0]) + le16(2586) + le16(-1234 & 0xFFFF) +
((-230) & 0xFFFFFF).to_bytes(3, "little") + bytes([0x08]))
@@ -97,6 +110,7 @@ accharger = pad21(pack_bits([
VECTORS = [
("solar", frame(0x01, 0xA060, 0x00, 0x1234, solar)),
("batmon", frame(0x02, 0xA389, 0x00, 0xBEEF, batmon)),
("batmon_na", frame(0x02, 0xA389, 0x00, 0xBEF0, batmon_na)),
("inverter", frame(0x03, 0xA2FA, 0x00, 0x0001, inverter)),
("dcdc", frame(0x04, 0xA3C0, 0x00, 0xFFFF, dcdc)),
("accharger", frame(0x08, 0xA339, 0x00, 0x00C8, accharger)),
+13
View File
@@ -72,6 +72,19 @@ int main(void)
CHECK(feq(rec.u.batmon.consumed_ah, -50.0f));
CHECK(feq(rec.u.batmon.soc, 85.5f));
/* --- battery monitor, every field not available --- */
CHECK(decode(VEC_BATMON_NA, sizeof(VEC_BATMON_NA), key, &rec) == VICTRONBLE_OK);
CHECK(rec.type == VICTRONBLE_DEV_BATTERY_MONITOR);
CHECK(rec.u.batmon.remaining_minutes == VICTRONBLE_TTG_NA);
CHECK(isnan(rec.u.batmon.voltage));
CHECK(rec.u.batmon.alarm == 0);
CHECK(rec.u.batmon.aux_mode == 0);
CHECK(isnan(rec.u.batmon.aux_voltage));
CHECK(isnan(rec.u.batmon.temperature));
CHECK(isnan(rec.u.batmon.current));
CHECK(isnan(rec.u.batmon.consumed_ah));
CHECK(isnan(rec.u.batmon.soc));
/* --- inverter --- */
CHECK(decode(VEC_INVERTER, sizeof(VEC_INVERTER), key, &rec) == VICTRONBLE_OK);
CHECK(rec.type == VICTRONBLE_DEV_INVERTER);
+1
View File
@@ -6,6 +6,7 @@ static const char VEC_KEY_HEX[] = "0df4d0395b7d5d4f5a0d0af52e1b4c1e";
static const uint8_t VEC_SOLAR[] = { 0xe1, 0x02, 0x10, 0x60, 0xa0, 0x00, 0x01, 0x34, 0x12, 0x0d, 0x53, 0xb0, 0x25, 0x4c, 0x65, 0xd3, 0x4a, 0x92, 0x34, 0x70, 0x3a, 0x6c, 0x19, 0x73, 0x7e, 0x65, 0xf6, 0xa4, 0x42, 0xd0, 0x56 };
static const uint8_t VEC_BATMON[] = { 0xe1, 0x02, 0x10, 0x89, 0xa3, 0x00, 0x02, 0xef, 0xbe, 0x0d, 0x41, 0x08, 0x53, 0x2f, 0x0d, 0x44, 0xa5, 0x1c, 0x62, 0xa0, 0x51, 0xe9, 0x7c, 0x1f, 0xae, 0x2f, 0xe9, 0xe8, 0x2a, 0x0e, 0x15 };
static const uint8_t VEC_BATMON_NA[] = { 0xe1, 0x02, 0x10, 0x89, 0xa3, 0x00, 0x02, 0xf0, 0xbe, 0x0d, 0xdc, 0x6c, 0x19, 0x76, 0x1d, 0x5f, 0x39, 0xab, 0x97, 0x5e, 0x4c, 0xf0, 0x85, 0x67, 0xd5, 0xaa, 0x19, 0x98, 0xac, 0xf6, 0xf5 };
static const uint8_t VEC_INVERTER[] = { 0xe1, 0x02, 0x10, 0xfa, 0xa2, 0x00, 0x03, 0x01, 0x00, 0x0d, 0xf6, 0x2d, 0x11, 0x5e, 0x6f, 0x60, 0x17, 0x3f, 0x62, 0xeb, 0x75, 0xfe, 0x67, 0x69, 0xef, 0x59, 0x71, 0xb6, 0xb6, 0xd4, 0x2c };
static const uint8_t VEC_DCDC[] = { 0xe1, 0x02, 0x10, 0xc0, 0xa3, 0x00, 0x04, 0xff, 0xff, 0x0d, 0x66, 0xcc, 0x80, 0x55, 0xf1, 0x96, 0xe8, 0xb8, 0x15, 0x7f, 0x76, 0xd2, 0x4a, 0x5c, 0xeb, 0xf2, 0xbb, 0x6c, 0x9f, 0x58, 0x08 };
static const uint8_t VEC_ACCHARGER[] = { 0xe1, 0x02, 0x10, 0x39, 0xa3, 0x00, 0x08, 0xc8, 0x00, 0x0d, 0x10, 0xc5, 0xcd, 0xca, 0xbb, 0x29, 0x20, 0xda, 0xf8, 0x1e, 0xae, 0xf6, 0x8e, 0xce, 0xd4, 0xec, 0xb5, 0x6b, 0xa9, 0x99, 0x4a };