This commit is contained in:
Siwat Sirichai 2024-03-30 01:30:03 +07:00
commit 9725a8d867
3 changed files with 13 additions and 3 deletions

View File

@ -6,6 +6,7 @@ CurrentTransformerCard::CurrentTransformerCard(AnalogCard* analogCard, uint8_t p
this->pin = pin; this->pin = pin;
this->voltage = voltage; this->voltage = voltage;
this->adcToCurrent = adcToCurrent; this->adcToCurrent = adcToCurrent;
this->conversionInterval = conversionInterval;
} }
void CurrentTransformerCard::bindFRAM(FRAM *fram, uint32_t framAddress) void CurrentTransformerCard::bindFRAM(FRAM *fram, uint32_t framAddress)
@ -29,7 +30,7 @@ void CurrentTransformerCard::loop()
if (this->lastConversionTime == 0) { if (this->lastConversionTime == 0) {
this->lastConversionTime = millis(); this->lastConversionTime = millis();
} }
static uint32_t lastConversionLoopTime = 0;
if (millis() - lastConversionLoopTime > this->conversionInterval) { if (millis() - lastConversionLoopTime > this->conversionInterval) {
this->beginConversion(); this->beginConversion();
lastConversionLoopTime = millis(); lastConversionLoopTime = millis();

View File

@ -46,5 +46,6 @@ class CurrentTransformerCard : public ExpansionCard
std::function<float(uint16_t)> adcToCurrent; // std::function that convert adc value to current in amps std::function<float(uint16_t)> adcToCurrent; // std::function that convert adc value to current in amps
uint8_t handler_count = 0; uint8_t handler_count = 0;
std::map<uint8_t,std::function<void(float, double)>> callbacks; std::map<uint8_t,std::function<void(float, double)>> callbacks;
uint32_t lastConversionLoopTime;
}; };

View File

@ -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"); ESP_LOGE("ESPMegaIoT", "MQTT Connection failed: Username or password not set but MQTT use_auth is true");
return false; 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(); sessionKeepAlive();
mqttSubscribe(); mqttSubscribe();
@ -307,6 +310,7 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt
} }
} }
mqtt_connected = true; mqtt_connected = true;
mqtt.publish(availability_topic, "online", true);
return true; return true;
} }
mqtt_connected = false; 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); auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
ESP_LOGD("ESPMegaIoT", "Binding MQTT callback"); ESP_LOGD("ESPMegaIoT", "Binding MQTT callback");
mqtt.setCallback(boundCallback); 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"); ESP_LOGD("ESPMegaIoT", "MQTT Connected, Calling session keep alive");
sessionKeepAlive(); sessionKeepAlive();
@ -345,6 +352,7 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt
} }
ESP_LOGI("ESPMegaIoT", "MQTT Connected OK."); ESP_LOGI("ESPMegaIoT", "MQTT Connected OK.");
mqtt_connected = true; mqtt_connected = true;
mqtt.publish(availability_topic, "online", true);
return true; return true;
} }
ESP_LOGW("ESPMegaIoT", "MQTT Connection failed: %d", mqtt.state()); ESP_LOGW("ESPMegaIoT", "MQTT Connection failed: %d", mqtt.state());