From 63d166d1431f88e64d1a9dad830703c587171d68 Mon Sep 17 00:00:00 2001 From: Sancho Date: Thu, 5 Oct 2023 12:29:54 +0200 Subject: [PATCH] Per battery MQTT reporting --- PylontechMonitoring.ino | 43 +++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/PylontechMonitoring.ino b/PylontechMonitoring.ino index bb77b50..d2d0f90 100644 --- a/PylontechMonitoring.ino +++ b/PylontechMonitoring.ino @@ -9,8 +9,8 @@ //+++ START CONFIGURATION +++ //IMPORTANT: Specify your WIFI settings: -#define WIFI_SSID "---SID HERE!---" -#define WIFI_PASS "---PASSWORD!---" +#define WIFI_SSID "*** your ssid ***" +#define WIFI_PASS "*** your wifi pass ***" #define WIFI_HOSTNAME "PylontechBattery" //IMPORTANT: Uncomment this line if you want to enable MQTT (and fill correct MQTT_ values below): @@ -26,11 +26,11 @@ //NOTE 1: if you want to change what is pushed via MQTT - edit function: pushBatteryDataToMqtt. //NOTE 2: MQTT_TOPIC_ROOT is where battery will push MQTT topics. For example "soc" will be pushed to: "home/grid_battery/soc" -#define MQTT_SERVER "192.168.1.2" +#define MQTT_SERVER "*** your MQTT server ***" #define MQTT_PORT 1883 -#define MQTT_USER "---username---" -#define MQTT_PASSWORD "---password---" -#define MQTT_TOPIC_ROOT "homeassistant/sensor/grid_battery/" //this is where mqtt data will be pushed +#define MQTT_USER "*** your MQTT username ***" +#define MQTT_PASSWORD "*** your MQTT password ***" +#define MQTT_TOPIC_ROOT "pylontech/sensor/grid_battery/" //this is where mqtt data will be pushed #define MQTT_PUSH_FREQ_SEC 2 //maximum mqtt update frequency in seconds //+++ END CONFIGURATION +++ @@ -294,8 +294,8 @@ void handleRoot() { ESP.getFreeHeap(), WiFi.RSSI(), WiFi.SSID().c_str()); strncat(szTmp, "
Runtime log
", sizeof(szTmp)-1); - strncat(szTmp, "
Command:Power | Help | Event Log | Time
", sizeof(szTmp)-1); - strncat(szTmp, "
", sizeof(szTmp)-1); strncat(szTmp, "", sizeof(szTmp)-1); @@ -748,6 +748,33 @@ void pushBatteryDataToMqtt(const batteryStack& lastSentData, bool forceUpdate /* mqtt_publish_i(MQTT_TOPIC_ROOT "getPowerDC", g_stack.getPowerDC(), lastSentData.getPowerDC(), 1, forceUpdate); mqtt_publish_i(MQTT_TOPIC_ROOT "powerIN", g_stack.powerIN(), lastSentData.powerIN(), 1, forceUpdate); mqtt_publish_i(MQTT_TOPIC_ROOT "powerOUT", g_stack.powerOUT(), lastSentData.powerOUT(), 1, forceUpdate); + + // publishing details + for (int ix = 0; ix < g_stack.batteryCount; ix++) { + char ixBuff[50]; + String ixBattStr = MQTT_TOPIC_ROOT + String(ix) + "/voltage"; + ixBattStr.toCharArray(ixBuff, 50); + mqtt_publish_f(ixBuff, g_stack.batts[ix].voltage / 1000.0, lastSentData.batts[ix].voltage / 1000.0, 0, forceUpdate); + ixBattStr = MQTT_TOPIC_ROOT + String(ix) + "/current"; + ixBattStr.toCharArray(ixBuff, 50); + mqtt_publish_f(ixBuff, g_stack.batts[ix].current / 1000.0, lastSentData.batts[ix].current / 1000.0, 0, forceUpdate); + ixBattStr = MQTT_TOPIC_ROOT + String(ix) + "/soc"; + ixBattStr.toCharArray(ixBuff, 50); + mqtt_publish_i(ixBuff, g_stack.batts[ix].soc, lastSentData.batts[ix].soc, 0, forceUpdate); + ixBattStr = MQTT_TOPIC_ROOT + String(ix) + "/charging"; + ixBattStr.toCharArray(ixBuff, 50); + mqtt_publish_i(ixBuff, g_stack.batts[ix].isCharging()?1:0, lastSentData.batts[ix].isCharging()?1:0, 0, forceUpdate); + ixBattStr = MQTT_TOPIC_ROOT + String(ix) + "/discharging"; + ixBattStr.toCharArray(ixBuff, 50); + mqtt_publish_i(ixBuff, g_stack.batts[ix].isDischarging()?1:0, lastSentData.batts[ix].isDischarging()?1:0, 0, forceUpdate); + ixBattStr = MQTT_TOPIC_ROOT + String(ix) + "/idle"; + ixBattStr.toCharArray(ixBuff, 50); + mqtt_publish_i(ixBuff, g_stack.batts[ix].isIdle()?1:0, lastSentData.batts[ix].isIdle()?1:0, 0, forceUpdate); + ixBattStr = MQTT_TOPIC_ROOT + String(ix) + "/state"; + ixBattStr.toCharArray(ixBuff, 50); + mqtt_publish_s(ixBuff, g_stack.batts[ix].isIdle()?"Idle":g_stack.batts[ix].isCharging()?"Charging":g_stack.batts[ix].isDischarging()?"Discharging":"", lastSentData.batts[ix].isIdle()?"Idle":lastSentData.batts[ix].isCharging()?"Charging":lastSentData.batts[ix].isDischarging()?"Discharging":"", forceUpdate); + + } } void mqttLoop()