From 9b9cd461ca79c613d00671f1de13a0c1e32f46e2 Mon Sep 17 00:00:00 2001 From: BenniG82 Date: Sun, 18 Jun 2023 15:06:43 +0200 Subject: [PATCH 1/2] Publish more information via MQTT under wbec-Topic - Added connection state of wbec (online/offline) - Offline state is set automatically via MQTTs last will feature - Added voltages of all 3 phases - Added current current limit - Added pcb temperature - Added result code of modbus communication - Added wifi RSSI - Added wifi channel - Added plug state - Added charge state --- src/mqtt.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 0b8dacf..ade83a2 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -13,6 +13,11 @@ const uint8_t m = 2; +const char* lastWillTopic = "wbec/connection"; +const char* lastWillMsgOff = "offline"; +const char* lastWillMsgOn = "online"; +const uint8_t lastWillQos = 1; +const bool lastWillRetain = true; WiFiClient espClient; PubSubClient client(espClient); @@ -158,9 +163,9 @@ void reconnect() { // Attempt to connect boolean con = false; if (strcmp(cfgMqttUser, "") != 0 && strcmp(cfgMqttPass, "") != 0) { - con = client.connect(clientId, cfgMqttUser, cfgMqttPass); + con = client.connect(clientId, cfgMqttUser, cfgMqttPass, lastWillTopic, lastWillQos, lastWillRetain, lastWillMsgOff); } else { - con = client.connect(clientId); + con = client.connect(clientId, lastWillTopic, lastWillQos, lastWillRetain, lastWillMsgOff); } if (con) { @@ -320,7 +325,41 @@ void mqtt_publish(uint8_t i) { snprintf_P(value, sizeof(value), PSTR("%.1f"), (float)content[i][ph+1]/10.0); // L1 = 2, L2 = 3, L3 = 4 client.publish(topic, value, retain); } + + for (uint8_t ph = 1; ph <= 3; ph++) { + snprintf_P(topic, sizeof(topic), PSTR("%s/voltL%d"), header, ph); + snprintf_P(value, sizeof(value), PSTR("%d"), content[i][ph+5]); // L1 = 2, L2 = 3, L3 = 4 + client.publish(topic, value, retain); + } + snprintf_P(topic, sizeof(topic), PSTR("%s/currLimit"), header); + snprintf_P(value, sizeof(value), PSTR("%.1f"), (float)content[i][53]/10.0); + client.publish(topic, value, retain); + + snprintf_P(topic, sizeof(topic), PSTR("%s/pcbTemp"), header); + snprintf_P(value, sizeof(value), PSTR("%.1f"), (float)content[i][5]/10.0); + client.publish(topic, value, retain); + + snprintf_P(topic, sizeof(topic), PSTR("%s/resCode"), header); + snprintf_P(value, sizeof(value), PSTR("%s"), String(modbusResultCode[i], HEX)); + client.publish(topic, value, retain); + + int qrssi = WiFi.RSSI(); + snprintf_P(topic, sizeof(topic), PSTR("%s/wifiRssi"), header); + snprintf_P(value, sizeof(value), PSTR("%d"), qrssi); + client.publish(topic, value, retain); + snprintf_P(topic, sizeof(topic), PSTR("%s/wifiChannel"), header); + snprintf_P(value, sizeof(value), PSTR("%d"), WiFi.channel()); + client.publish(topic, value, retain); + + snprintf_P(topic, sizeof(topic), PSTR("%s/plug_state"), header); + snprintf_P(value, sizeof(value), PSTR("%s"), ps?"true":"false"); + client.publish(topic, value, retain); + snprintf_P(topic, sizeof(topic), PSTR("%s/charge_state"), header); + snprintf_P(value, sizeof(value), PSTR("%s"), cs?"true":"false"); + client.publish(topic, value, retain); + + // publish values from inverter if (strcmp(cfgInverterIp, "") != 0) { snprintf_P(header, sizeof(header), PSTR("wbec/inverter")); @@ -346,6 +385,9 @@ void mqtt_publish(uint8_t i) { snprintf_P(value, sizeof(value), PSTR("%ld"), pv_getWatt()); client.publish(topic, value, retain); } + + // Wbec-Connection Status + client.publish(lastWillTopic, lastWillMsgOn, lastWillRetain); } void mqtt_log(const char *output, const char *msg) { From 40ceb3c25d178625fd7304c917dbaadaf1c3db18 Mon Sep 17 00:00:00 2001 From: BenniG82 Date: Sun, 18 Jun 2023 15:37:23 +0200 Subject: [PATCH 2/2] Rework naming to camelCase --- src/mqtt.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mqtt.cpp b/src/mqtt.cpp index ade83a2..69cf25a 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -351,11 +351,11 @@ void mqtt_publish(uint8_t i) { snprintf_P(value, sizeof(value), PSTR("%d"), WiFi.channel()); client.publish(topic, value, retain); - snprintf_P(topic, sizeof(topic), PSTR("%s/plug_state"), header); + snprintf_P(topic, sizeof(topic), PSTR("%s/plugState"), header); snprintf_P(value, sizeof(value), PSTR("%s"), ps?"true":"false"); client.publish(topic, value, retain); - snprintf_P(topic, sizeof(topic), PSTR("%s/charge_state"), header); + snprintf_P(topic, sizeof(topic), PSTR("%s/chargeState"), header); snprintf_P(value, sizeof(value), PSTR("%s"), cs?"true":"false"); client.publish(topic, value, retain);