diff --git a/include/Configuration.h b/include/Configuration.h index 2782f4758..9f433faf3 100644 --- a/include/Configuration.h +++ b/include/Configuration.h @@ -126,6 +126,8 @@ struct POWERMETER_HTTP_SML_CONFIG_T { }; using PowerMeterHttpSmlConfig = struct POWERMETER_HTTP_SML_CONFIG_T; +enum BatteryVoltageUnit { Volts = 0, DeciVolts = 1, CentiVolts = 2, MilliVolts = 3 }; + struct CONFIG_T { struct { uint32_t Version; @@ -285,6 +287,7 @@ struct CONFIG_T { char MqttSocJsonPath[BATTERY_JSON_MAX_PATH_STRLEN + 1]; char MqttVoltageTopic[MQTT_MAX_TOPIC_STRLEN + 1]; char MqttVoltageJsonPath[BATTERY_JSON_MAX_PATH_STRLEN + 1]; + BatteryVoltageUnit MqttVoltageUnit; } Battery; struct { diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 6127eb877..9b3c7d4e4 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -260,6 +260,7 @@ bool ConfigurationClass::write() battery["mqtt_json_path"] = config.Battery.MqttSocJsonPath; battery["mqtt_voltage_topic"] = config.Battery.MqttVoltageTopic; battery["mqtt_voltage_json_path"] = config.Battery.MqttVoltageJsonPath; + battery["mqtt_voltage_unit"] = config.Battery.MqttVoltageUnit; JsonObject huawei = doc["huawei"].to(); huawei["enabled"] = config.Huawei.Enabled; @@ -609,6 +610,7 @@ bool ConfigurationClass::read() strlcpy(config.Battery.MqttSocJsonPath, battery["mqtt_json_path"] | "", sizeof(config.Battery.MqttSocJsonPath)); strlcpy(config.Battery.MqttVoltageTopic, battery["mqtt_voltage_topic"] | "", sizeof(config.Battery.MqttVoltageTopic)); strlcpy(config.Battery.MqttVoltageJsonPath, battery["mqtt_voltage_json_path"] | "", sizeof(config.Battery.MqttVoltageJsonPath)); + config.Battery.MqttVoltageUnit = battery["mqtt_voltage_unit"] | BatteryVoltageUnit::Volts; JsonObject huawei = doc["huawei"]; config.Huawei.Enabled = huawei["enabled"] | HUAWEI_ENABLED; diff --git a/src/MqttBattery.cpp b/src/MqttBattery.cpp index 137c56f06..544ff0322 100644 --- a/src/MqttBattery.cpp +++ b/src/MqttBattery.cpp @@ -90,8 +90,25 @@ void MqttBattery::onMqttMessageVoltage(espMqttClientTypes::MessageProperties con std::string(reinterpret_cast(payload), len), topic, jsonPath); + if (!voltage.has_value()) { return; } + auto const& config = Configuration.get(); + using Unit_t = BatteryVoltageUnit; + switch (config.Battery.MqttVoltageUnit) { + case Unit_t::DeciVolts: + *voltage /= 10; + break; + case Unit_t::CentiVolts: + *voltage /= 100; + break; + case Unit_t::MilliVolts: + *voltage /= 1000; + break; + default: + break; + } + // since this project is revolving around Hoymiles microinverters, which can // only handle up to 65V of input voltage at best, it is safe to assume that // an even higher voltage is implausible. diff --git a/src/WebApi_battery.cpp b/src/WebApi_battery.cpp index e5378cd99..a0badd3e1 100644 --- a/src/WebApi_battery.cpp +++ b/src/WebApi_battery.cpp @@ -30,7 +30,7 @@ void WebApiBatteryClass::onStatus(AsyncWebServerRequest* request) if (!WebApi.checkCredentialsReadonly(request)) { return; } - + AsyncJsonResponse* response = new AsyncJsonResponse(); auto& root = response->getRoot(); const CONFIG_T& config = Configuration.get(); @@ -44,6 +44,7 @@ void WebApiBatteryClass::onStatus(AsyncWebServerRequest* request) root["mqtt_soc_json_path"] = config.Battery.MqttSocJsonPath; root["mqtt_voltage_topic"] = config.Battery.MqttVoltageTopic; root["mqtt_voltage_json_path"] = config.Battery.MqttVoltageJsonPath; + root["mqtt_voltage_unit"] = config.Battery.MqttVoltageUnit; response->setLength(); request->send(response); @@ -85,6 +86,7 @@ void WebApiBatteryClass::onAdminPost(AsyncWebServerRequest* request) strlcpy(config.Battery.MqttSocJsonPath, root["mqtt_soc_json_path"].as().c_str(), sizeof(config.Battery.MqttSocJsonPath)); strlcpy(config.Battery.MqttVoltageTopic, root["mqtt_voltage_topic"].as().c_str(), sizeof(config.Battery.MqttVoltageTopic)); strlcpy(config.Battery.MqttVoltageJsonPath, root["mqtt_voltage_json_path"].as().c_str(), sizeof(config.Battery.MqttVoltageJsonPath)); + config.Battery.MqttVoltageUnit = static_cast(root["mqtt_voltage_unit"].as()); WebApi.writeConfig(retMsg); diff --git a/webapp/src/locales/de.json b/webapp/src/locales/de.json index 2bec0c90e..44cd5ec7d 100644 --- a/webapp/src/locales/de.json +++ b/webapp/src/locales/de.json @@ -676,6 +676,7 @@ "MqttVoltageConfiguration": "Einstellungen Spannung", "MqttJsonPath": "Optional: JSON-Pfad", "MqttJsonPathDescription": "Anwendungsspezifischer JSON-Pfad um den Wert in den JSON Nutzdatzen zu finden, z.B. 'electricLevel'. Leer lassen, falls die Nutzdaten des Topics einen numerischen Wert enthält.", + "MqttVoltageUnit": "Einheit", "MqttSocTopic": "Topic für SoC", "MqttVoltageTopic": "Topic für Spannung", "JkBmsConfiguration": "JK BMS Einstellungen", diff --git a/webapp/src/locales/en.json b/webapp/src/locales/en.json index 806a7250c..6a3be7793 100644 --- a/webapp/src/locales/en.json +++ b/webapp/src/locales/en.json @@ -679,6 +679,7 @@ "MqttVoltageConfiguration": "Voltage Settings", "MqttJsonPath": "Optional: JSON Path", "MqttJsonPathDescription": "Application specific JSON path to find the value in the JSON payload, e.g., 'electricLevel'. Leave empty if the topic's payload contains a plain numeric value.", + "MqttVoltageUnit": "Unit", "MqttSocTopic": "SoC Value Topic", "MqttVoltageTopic": "Voltage Value Topic", "JkBmsConfiguration": "JK BMS Settings", diff --git a/webapp/src/locales/fr.json b/webapp/src/locales/fr.json index 794332819..a91e9d335 100644 --- a/webapp/src/locales/fr.json +++ b/webapp/src/locales/fr.json @@ -602,6 +602,7 @@ "MqttVoltageConfiguration": "Voltage Settings", "MqttJsonPath": "Optional: JSON Path", "MqttJsonPathDescription": "Application specific JSON path to find the value in the JSON payload, e.g., 'electricLevel'. Leave empty if the topic's payload contains a plain numeric value.", + "MqttVoltageUnit": "Unit", "MqttSocTopic": "SoC Value Topic", "MqttVoltageTopic": "Voltage Value Topic", "JkBmsConfiguration": "JK BMS Settings", diff --git a/webapp/src/types/BatteryConfig.ts b/webapp/src/types/BatteryConfig.ts index f236523ab..348ed2a32 100644 --- a/webapp/src/types/BatteryConfig.ts +++ b/webapp/src/types/BatteryConfig.ts @@ -8,4 +8,5 @@ export interface BatteryConfig { mqtt_soc_json_path: string; mqtt_voltage_topic: string; mqtt_voltage_json_path: string; + mqtt_voltage_unit: number; } diff --git a/webapp/src/views/BatteryAdminView.vue b/webapp/src/views/BatteryAdminView.vue index 622af0de8..1f6d746f1 100644 --- a/webapp/src/views/BatteryAdminView.vue +++ b/webapp/src/views/BatteryAdminView.vue @@ -78,6 +78,18 @@ maxlength="128" :tooltip="$t('batteryadmin.MqttJsonPathDescription')" /> +
+ +
+ +
+
@@ -122,6 +134,12 @@ export default defineComponent({ { key: 0, value: 'Uart' }, { key: 1, value: 'Transceiver' }, ], + voltageUnitTypeList: [ + { key: 3, value: "mV" }, + { key: 2, value: "cV" }, + { key: 1, value: "dV" }, + { key: 0, value: "V" }, + ], }; }, created() {