Skip to content

Commit

Permalink
Fix: properly format Victron MPPT firmware version
Browse files Browse the repository at this point in the history
  • Loading branch information
schlimmchen committed May 31, 2024
1 parent 90eb25f commit 88314f9
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 18 deletions.
38 changes: 38 additions & 0 deletions lib/VeDirectFrameHandler/VeDirectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,44 @@ frozen::string const& veStruct::getPidAsString() const
return getAsString(values, productID_PID);
}

/*
* This function returns the firmware version as an integer, disregarding
* release candidate marks.
*/
uint32_t veStruct::getFwVersionAsInteger() const
{
char const* strVersion = firmwareVer_FW;

// VE.Direct protocol manual states that the first char can be a non-digit,
// in which case that char represents a release candidate version
if (strVersion[0] < '0' || strVersion[0] > '9') { ++strVersion; }

return static_cast<uint32_t>(strtoul(strVersion, nullptr, 10));
}

/*
* This function returns the firmware version as readable text.
*/
String veStruct::getFwVersionFormatted() const
{
char const* strVersion = firmwareVer_FW;

// VE.Direct protocol manual states that the first char can be a non-digit,
// in which case that char represents a release candidate version
if (strVersion[0] < '0' || strVersion[0] > '9') { ++strVersion; }

String res(strVersion[0]);
res += ".";
res += strVersion + 1;

if (strVersion > firmwareVer_FW) {
res += "-rc-";
res += firmwareVer_FW[0];
}

return res;
}

/*
* This function returns the state of operations (CS) as readable text.
*/
Expand Down
5 changes: 4 additions & 1 deletion lib/VeDirectFrameHandler/VeDirectData.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

#include <frozen/string.h>
#include <frozen/map.h>
#include <Arduino.h>

#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 productID_PID = 0; // product id
char serialNr_SER[VE_MAX_VALUE_LEN]; // serial number
char firmwareNr_FW[VE_MAX_VALUE_LEN]; // firmware release number
char firmwareVer_FW[VE_MAX_VALUE_LEN]; // firmware release number
uint32_t batteryVoltage_V_mV = 0; // battery voltage in mV
int32_t batteryCurrent_I_mA = 0; // battery current in mA (can be negative)
float mpptEfficiency_Percent = 0; // efficiency in percent (calculated, moving average)

frozen::string const& getPidAsString() const; // product ID as string
uint32_t getFwVersionAsInteger() const;
String getFwVersionFormatted() const;
} veStruct;

struct veMpptStruct : veStruct {
Expand Down
2 changes: 1 addition & 1 deletion lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void VeDirectFrameHandler<T>::processTextData(std::string const& name, std::stri
}

if (name == "FW") {
strcpy(_tmpFrame.firmwareNr_FW, value.c_str());
strcpy(_tmpFrame.firmwareVer_FW, value.c_str());
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/VeDirectFrameHandler/VeDirectMpptController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void VeDirectMpptController::frameValidEvent() {
// charger periodically sends human readable (TEXT) data to the serial port. For firmware
// versions v1.53 and above, the charger always periodically sends TEXT data to the serial port.
// --> We just use hex commandes for firmware >= 1.53 to keep text messages alive
if (atoi(_tmpFrame.firmwareNr_FW) < 153) { return; }
if (_tmpFrame.getFwVersionAsInteger() < 153) { return; }

using Command = VeDirectHexCommand;
using Register = VeDirectHexRegister;
Expand Down
2 changes: 1 addition & 1 deletion src/MqttHandleVedirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void MqttHandleVedirectClass::publish_mppt_data(const VeDirectMpptController::da

PUBLISH(productID_PID, "PID", currentData.getPidAsString().data());
PUBLISH(serialNr_SER, "SER", currentData.serialNr_SER);
PUBLISH(firmwareNr_FW, "FW", currentData.firmwareNr_FW);
PUBLISH(firmwareVer_FW, "FW", currentData.firmwareVer_FW);
PUBLISH(loadOutputState_LOAD, "LOAD", (currentData.loadOutputState_LOAD ? "ON" : "OFF"));
PUBLISH(currentState_CS, "CS", currentData.getCsAsString().data());
PUBLISH(errorCode_ERR, "ERR", currentData.getErrAsString().data());
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_ws_vedirect_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void WebApiWsVedirectLiveClass::generateCommonJsonResponse(JsonVariant& root, bo

void WebApiWsVedirectLiveClass::populateJson(const JsonObject &root, const VeDirectMpptController::data_t &mpptData) {
root["product_id"] = mpptData.getPidAsString();
root["firmware_version"] = String(mpptData.firmwareNr_FW);
root["firmware_version"] = mpptData.getFwVersionFormatted();

const JsonObject values = root["values"].to<JsonObject>();

Expand Down
6 changes: 3 additions & 3 deletions webapp/src/components/VedirectView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
{{ item.product_id }}
</div>
<div style="padding-right: 2em;">
{{ $t('vedirecthome.SerialNumber') }} {{ serial }}
{{ $t('vedirecthome.SerialNumber') }}: {{ serial }}
</div>
<div style="padding-right: 2em;">
{{ $t('vedirecthome.FirmwareNumber') }} {{ item.firmware_version }}
{{ $t('vedirecthome.FirmwareVersion') }}: {{ item.firmware_version }}
</div>
<div style="padding-right: 2em;">
{{ $t('vedirecthome.DataAge') }} {{ $t('vedirecthome.Seconds', {'val': Math.floor(item.data_age_ms / 1000)}) }}
{{ $t('vedirecthome.DataAge') }}: {{ $t('vedirecthome.Seconds', {'val': Math.floor(item.data_age_ms / 1000)}) }}
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@
"LoadingInverter": "Warte auf Daten... (kann bis zu 10 Sekunden dauern)"
},
"vedirecthome": {
"SerialNumber": "Seriennummer: ",
"FirmwareNumber": "Firmware Version: ",
"DataAge": "letzte Aktualisierung: ",
"SerialNumber": "Seriennummer",
"FirmwareVersion": "Firmware-Version",
"DataAge": "letzte Aktualisierung",
"Seconds": "vor {val} Sekunden",
"Property": "Eigenschaft",
"Value": "Wert",
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@
"LoadingInverter": "Waiting for data... (can take up to 10 seconds)"
},
"vedirecthome": {
"SerialNumber": "Serial Number: ",
"FirmwareNumber": "Firmware Number: ",
"DataAge": "Data Age: ",
"SerialNumber": "Serial Number",
"FirmwareVersion": "Firmware Version",
"DataAge": "Data Age",
"Seconds": "{val} seconds",
"Property": "Property",
"Value": "Value",
Expand Down
8 changes: 4 additions & 4 deletions webapp/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@
"LoadingInverter": "Waiting for data... (can take up to 10 seconds)"
},
"vedirecthome": {
"SerialNumber": "Serial Number: ",
"FirmwareNumber": "Firmware Number: ",
"DataAge": "Data Age: ",
"Seconds": "{val} seconds",
"SerialNumber": "Numéro de série",
"FirmwareVersion": "Version du Firmware",
"DataAge": "Âge des données",
"Seconds": "{val} secondes",
"Property": "Property",
"Value": "Value",
"Unit": "Unit",
Expand Down

0 comments on commit 88314f9

Please sign in to comment.