From 064ff27ea7d8c66f49c636d4ebc0a16b16a77301 Mon Sep 17 00:00:00 2001 From: amarofarinha <151563493+amarofarinha@users.noreply.github.com> Date: Sun, 26 May 2024 15:17:00 +0100 Subject: [PATCH] Prevent MQTT to publish cell voltage values before they are populated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In addition to checking the value of the variable number_of_cells, we also test the value of the last cell, as the value of the cells can be filled some time after initializing the number of cells. This test was also added before publishing the values cell_max_voltage and cell_min_voltage since they are ​​calculated from the cell data. --- Software/src/devboard/mqtt/mqtt.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Software/src/devboard/mqtt/mqtt.cpp b/Software/src/devboard/mqtt/mqtt.cpp index 06721579..3ea7ddbd 100644 --- a/Software/src/devboard/mqtt/mqtt.cpp +++ b/Software/src/devboard/mqtt/mqtt.cpp @@ -75,7 +75,7 @@ static void publish_cell_voltages(void) { doc.clear(); // clear after sending autoconfig } else { // If cell voltages haven't been populated... - if (datalayer.battery.info.number_of_cells == 0u) { + if (datalayer.battery.info.number_of_cells == 0u || datalayer.battery.status.cell_voltages_mV[datalayer.battery.info.number_of_cells-1]==0u) { return; } @@ -158,10 +158,12 @@ static void publish_common_info(void) { doc["temperature_max"] = ((float)((int16_t)datalayer.battery.status.temperature_max_dC)) / 10.0; doc["stat_batt_power"] = ((float)((int32_t)datalayer.battery.status.active_power_W)); doc["battery_current"] = ((float)((int16_t)datalayer.battery.status.current_dA)) / 10.0; - doc["cell_max_voltage"] = ((float)datalayer.battery.status.cell_max_voltage_mV) / 1000.0; - doc["cell_min_voltage"] = ((float)datalayer.battery.status.cell_min_voltage_mV) / 1000.0; doc["battery_voltage"] = ((float)datalayer.battery.status.voltage_dV) / 10.0; - + // publish only if cell voltages have been populated... + if (datalayer.battery.info.number_of_cells != 0u && datalayer.battery.status.cell_voltages_mV[datalayer.battery.info.number_of_cells-1]!=0u) { + doc["cell_max_voltage"] = ((float)datalayer.battery.status.cell_max_voltage_mV) / 1000.0; + doc["cell_min_voltage"] = ((float)datalayer.battery.status.cell_min_voltage_mV) / 1000.0; + } serializeJson(doc, mqtt_msg); if (!mqtt_publish(state_topic.c_str(), mqtt_msg, false)) { #ifdef DEBUG_VIA_USB