From de8672b9cbe637c4699cf4dd386954b728db0dcb Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 25 Mar 2024 01:27:41 +0700 Subject: [PATCH 1/3] add MQTT availability topic --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index 1d2816c..80fd0d3 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -294,7 +294,10 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt ESP_LOGE("ESPMegaIoT", "MQTT Connection failed: Username or password not set but MQTT use_auth is true"); return false; } - if (mqtt.connect(client_id, mqtt_user, mqtt_password)) + // Create availability topic + char availability_topic[base_topic_length + 15]; + sprintf(availability_topic, "%s/availability", this->mqtt_config.base_topic); + if (mqtt.connect(client_id, mqtt_user, mqtt_password, availability_topic, 0, true, "offline")) { sessionKeepAlive(); mqttSubscribe(); @@ -307,6 +310,7 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt } } mqtt_connected = true; + mqtt.publish(availability_topic, "online", true); return true; } mqtt_connected = false; @@ -328,7 +332,10 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); ESP_LOGD("ESPMegaIoT", "Binding MQTT callback"); mqtt.setCallback(boundCallback); - if (mqtt.connect(client_id)) + // Create availability topic + char availability_topic[base_topic_length + 15]; + sprintf(availability_topic, "%s/availability", this->mqtt_config.base_topic); + if (mqtt.connect(client_id, availability_topic, 0, true, "offline")) { ESP_LOGD("ESPMegaIoT", "MQTT Connected, Calling session keep alive"); sessionKeepAlive(); @@ -345,6 +352,7 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt } ESP_LOGI("ESPMegaIoT", "MQTT Connected OK."); mqtt_connected = true; + mqtt.publish(availability_topic, "online", true); return true; } ESP_LOGW("ESPMegaIoT", "MQTT Connection failed: %d", mqtt.state()); From 3a9488d4f81d0071335d3b6568d47d2913c33dac Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 25 Mar 2024 11:11:50 +0700 Subject: [PATCH 2/3] fix conversion loop bug --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp index db469b0..b0ea648 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp @@ -6,6 +6,7 @@ CurrentTransformerCard::CurrentTransformerCard(AnalogCard* analogCard, uint8_t p this->pin = pin; this->voltage = voltage; this->adcToCurrent = adcToCurrent; + this->conversionInterval = conversionInterval; } void CurrentTransformerCard::bindFRAM(FRAM *fram, uint32_t framAddress) From 199a89a700f00d09b3cea2f5175ce07940b6d175 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 25 Mar 2024 14:16:15 +0700 Subject: [PATCH 3/3] move ct conversion counter --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp | 2 +- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp index b0ea648..f5ce161 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp @@ -30,7 +30,7 @@ void CurrentTransformerCard::loop() if (this->lastConversionTime == 0) { this->lastConversionTime = millis(); } - static uint32_t lastConversionLoopTime = 0; + if (millis() - lastConversionLoopTime > this->conversionInterval) { this->beginConversion(); lastConversionLoopTime = millis(); diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp index 9dfa44a..708a3f9 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.hpp @@ -46,5 +46,6 @@ class CurrentTransformerCard : public ExpansionCard std::function adcToCurrent; // std::function that convert adc value to current in amps uint8_t handler_count = 0; std::map> callbacks; + uint32_t lastConversionLoopTime; };