diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp index db469b0..f5ce161 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) @@ -29,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; }; 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());