From 03089e92cbe13cdaa16ff5850732a90c99277213 Mon Sep 17 00:00:00 2001 From: Daniel Ekman Date: Tue, 21 Dec 2021 20:40:17 +0100 Subject: [PATCH] basic stuff implemented --- src/AmsToMqttBridge.ino | 7 ---- src/mqtt/HomeAssistantMqttHandler.cpp | 54 +++++++++++++++------------ src/mqtt/HomeAssistantMqttHandler.h | 2 +- web/ha1.json | 3 ++ web/{json2ha.json => ha2.json} | 0 web/{json3ha.json => ha3.json} | 0 web/{json3hapf.json => ha3pf.json} | 0 web/hadiscover1.json | 15 ++++++++ web/json1ha.json | 3 -- 9 files changed, 50 insertions(+), 34 deletions(-) create mode 100644 web/ha1.json rename web/{json2ha.json => ha2.json} (100%) rename web/{json3ha.json => ha3.json} (100%) rename web/{json3hapf.json => ha3pf.json} (100%) create mode 100644 web/hadiscover1.json delete mode 100644 web/json1ha.json diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index f029c22b..6f00b570 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -315,7 +315,6 @@ bool longPressActive = false; bool wifiConnected = false; unsigned long lastTemperatureRead = 0; -unsigned long lastSystemMessage = 0; unsigned long lastErrorBlink = 0; int lastError = 0; @@ -474,12 +473,6 @@ void loop() { } if(readHanPort() || now - meterState.getLastUpdateMillis() > 30000) { - if(now - lastSystemMessage > 15000) { - if(mqtt != NULL && mqttHandler != NULL && WiFi.getMode() != WIFI_AP && WiFi.status() == WL_CONNECTED && mqtt->connected() && !topic.isEmpty()) { - lastSystemMessage = now; - mqttHandler->publishSystem(&hw); - } - } if(now - lastTemperatureRead > 15000) { unsigned long start = millis(); hw.updateTemperatures(); diff --git a/src/mqtt/HomeAssistantMqttHandler.cpp b/src/mqtt/HomeAssistantMqttHandler.cpp index 10ed07ec..d10630c6 100644 --- a/src/mqtt/HomeAssistantMqttHandler.cpp +++ b/src/mqtt/HomeAssistantMqttHandler.cpp @@ -1,10 +1,10 @@ #include "HomeAssistantMqttHandler.h" #include "hexutils.h" #include "Uptime.h" -#include "web/root/json1ha_json.h" -#include "web/root/json2ha_json.h" -#include "web/root/json3ha_json.h" -#include "web/root/json3hapf_json.h" +#include "web/root/ha1_json.h" +#include "web/root/ha2_json.h" +#include "web/root/ha3_json.h" +#include "web/root/ha3pf_json.h" #include "web/root/jsonsys_json.h" #include "web/root/jsonprices_json.h" @@ -12,21 +12,22 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { if(topic.isEmpty() || !mqtt->connected()) return false; + listType = data->getListType(); // for discovery stuff in publishSystem() if(data->getListType() == 1) { char json[192]; - snprintf_P(json, sizeof(json), JSON1HA_JSON, + snprintf_P(json, sizeof(json), HA1_JSON, data->getActiveImportPower() ); - return mqtt->publish(topic + "/power", json); + return mqtt->publish(topic + "/sensor", json); } else if(data->getListType() == 2) { char json[384]; - snprintf_P(json, sizeof(json), JSON2HA_JSON, + snprintf_P(json, sizeof(json), HA2_JSON, data->getListId().c_str(), data->getMeterId().c_str(), data->getMeterModel().c_str(), data->getActiveImportPower(), data->getReactiveImportPower(), - data->getActiveExportPower(), + sequence, //data->getActiveExportPower(), data->getReactiveExportPower(), data->getL1Current(), data->getL2Current(), @@ -39,7 +40,7 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { } else if(data->getListType() == 3) { if(data->getPowerFactor() == 0) { char json[512]; - snprintf_P(json, sizeof(json), JSON3HA_JSON, + snprintf_P(json, sizeof(json), HA3_JSON, data->getListId().c_str(), data->getMeterId().c_str(), data->getMeterModel().c_str(), @@ -62,7 +63,7 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState) { return mqtt->publish(topic + "/sensor", json); } else { char json[768]; - snprintf_P(json, sizeof(json), JSON3HAPF_JSON, + snprintf_P(json, sizeof(json), HA3PF_JSON, data->getListId().c_str(), data->getMeterId().c_str(), data->getMeterModel().c_str(), @@ -226,19 +227,26 @@ bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) { } bool HomeAssistantMqttHandler::publishSystem(HwTools* hw) { - if(init || topic.isEmpty() || !mqtt->connected()) + if(topic.isEmpty() || !mqtt->connected()){ + sequence = 0; return false; - //if(!topic.endsWith("/")) topic += "/"; + } - char json[192]; - snprintf_P(json, sizeof(json), JSONSYS_JSON, - WiFi.macAddress().c_str(), - clientId.c_str(), - (uint32_t) (millis64()/1000), - hw->getVcc(), - hw->getWifiRssi(), - hw->getTemperature() - ); - init = mqtt->publish(topic + "/state", json); - return init; + if(sequence % 3 == 0){ + char json[192]; + snprintf_P(json, sizeof(json), JSONSYS_JSON, + WiFi.macAddress().c_str(), + clientId.c_str(), + (uint32_t) (millis64()/1000), + hw->getVcc(), + hw->getWifiRssi(), + hw->getTemperature() + ); + mqtt->publish(topic + "/state", json); + } + if(sequence % 6 == 1 && listType > 0){ // every 60 ams message, publish mqtt discovery + mqtt->publish(topic + "/discovery", "{\"discovery\":1}"); // test + } + if(listType>0) sequence++; + return true; } diff --git a/src/mqtt/HomeAssistantMqttHandler.h b/src/mqtt/HomeAssistantMqttHandler.h index 14c652b2..4725d303 100644 --- a/src/mqtt/HomeAssistantMqttHandler.h +++ b/src/mqtt/HomeAssistantMqttHandler.h @@ -19,6 +19,6 @@ private: String clientId; String topic; HwTools* hw; - bool init = false; + uint8_t sequence = 0, listType = 0; }; #endif diff --git a/web/ha1.json b/web/ha1.json new file mode 100644 index 00000000..62d98528 --- /dev/null +++ b/web/ha1.json @@ -0,0 +1,3 @@ +{ + "P" : %d, +} diff --git a/web/json2ha.json b/web/ha2.json similarity index 100% rename from web/json2ha.json rename to web/ha2.json diff --git a/web/json3ha.json b/web/ha3.json similarity index 100% rename from web/json3ha.json rename to web/ha3.json diff --git a/web/json3hapf.json b/web/ha3pf.json similarity index 100% rename from web/json3hapf.json rename to web/ha3pf.json diff --git a/web/hadiscover1.json b/web/hadiscover1.json new file mode 100644 index 00000000..de8f64fa --- /dev/null +++ b/web/hadiscover1.json @@ -0,0 +1,15 @@ +{ + "name" : "%s", + "stat_t" : "%s", + "json_attr_t" : "%s", + "unit_of_meas" : "%s", + "val_tpl" : "{{value_json['%s']}}", + "uniq_id" : "%s", + "dev" : { + "ids" : [ "%s" ], + "name" : "AMS reader", + "mdl" : "ESP32", + "sw" : "2.0.0", + "mf" : "AmsToMqttBridge" + } + } \ No newline at end of file diff --git a/web/json1ha.json b/web/json1ha.json deleted file mode 100644 index 9fa1f374..00000000 --- a/web/json1ha.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "P" : %d -}