Compare commits

...

5 Commits

Author SHA1 Message Date
Gunnar Skjold
7a70bd7511 Some adjustments to MQTT 2023-12-24 07:10:58 +01:00
Gunnar Skjold
c583c2b44a Fixed mqtt changed flag 2023-12-24 07:03:05 +01:00
Gunnar Skjold
4c28d512a1 Changed mqtt disconnect to avoid exception 2023-12-24 06:59:40 +01:00
Gunnar Skjold
1775d0abce Fixed bug 2023-12-24 06:30:36 +01:00
Gunnar Skjold
8e65f1fe14 Fixed bug 2023-12-24 06:27:24 +01:00
4 changed files with 35 additions and 47 deletions

View File

@@ -17,6 +17,7 @@ class AmsMqttHandler {
public:
AmsMqttHandler(MqttConfig& mqttConfig, RemoteDebug* debugger, char* buf) {
this->mqttConfig = mqttConfig;
this->mqttConfigChanged = true;
this->debugger = debugger;
this->json = buf;
mqtt.dropOverflow(true);
@@ -50,6 +51,7 @@ public:
protected:
RemoteDebug* debugger;
MqttConfig mqttConfig;
bool mqttConfigChanged = true;
MQTTClient mqtt = MQTTClient(256);
unsigned long lastMqttRetry = -10000;
bool caVerification = true;

View File

@@ -9,6 +9,7 @@ void AmsMqttHandler::setCaVerification(bool caVerification) {
void AmsMqttHandler::setConfig(MqttConfig& mqttConfig) {
this->mqttConfig = mqttConfig;
this->mqttConfigChanged = true;
}
bool AmsMqttHandler::connect() {
@@ -19,6 +20,8 @@ bool AmsMqttHandler::connect() {
lastMqttRetry = millis();
time_t epoch = time(nullptr);
WiFiClient *actualClient = NULL;
if(mqttConfig.ssl) {
if(epoch < FirmwareVersion::BuildEpoch) {
@@ -33,7 +36,9 @@ bool AmsMqttHandler::connect() {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("ESP8266 firmware does not have enough memory...\n"));
return false;
#endif
}
if(mqttConfigChanged) {
if(caVerification && LittleFS.begin()) {
File file;
@@ -48,8 +53,6 @@ bool AmsMqttHandler::connect() {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("CA accepted\n"));
} else {
if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("CA was rejected\n"));
delete mqttSecureClient;
mqttSecureClient = NULL;
return false;
}
#endif
@@ -97,29 +100,21 @@ bool AmsMqttHandler::connect() {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("CA verification disabled\n"));
mqttSecureClient->setInsecure();
}
if(mqttClient != NULL) {
mqttClient->stop();
delete mqttClient;
}
mqttClient = mqttSecureClient;
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("MQTT SSL setup complete (%dkb free heap)\n"), ESP.getFreeHeap());
}
} else if(mqttSecureClient != NULL) {
mqttSecureClient->stop();
delete mqttSecureClient;
mqttSecureClient = NULL;
mqttClient = NULL;
}
if(mqttClient == NULL) {
actualClient = mqttSecureClient;
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("MQTT SSL setup complete (%dkb free heap)\n"), ESP.getFreeHeap());
} else {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("No SSL, using client without SSL support\n"));
mqttClient = new WiFiClient();
if(mqttClient == NULL) {
mqttClient = new WiFiClient();
}
actualClient = mqttClient;
}
mqttConfigChanged = false;
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to MQTT %s:%d\n"), mqttConfig.host, mqttConfig.port);
mqtt.begin(mqttConfig.host, mqttConfig.port, *mqttClient);
mqtt.begin(mqttConfig.host, mqttConfig.port, *actualClient);
#if defined(ESP8266)
if(mqttSecureClient) {
@@ -160,15 +155,6 @@ void AmsMqttHandler::disconnect() {
mqtt.loop();
delay(10);
yield();
if(mqttClient != NULL) {
mqttClient->stop();
delete mqttClient;
mqttClient = NULL;
if(mqttSecureClient != NULL) {
mqttSecureClient = NULL;
}
}
}
lwmqtt_err_t AmsMqttHandler::lastError() {

View File

@@ -56,7 +56,7 @@ public:
statusTopic = F("homeassistant/status");
discoveryTopic = F("homeassistant/sensor/");
}
// strcpy(this->mqttConfig.subscribeTopic, statusTopic.c_str());
strcpy(this->mqttConfig.subscribeTopic, statusTopic.c_str());
};
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi);

View File

@@ -23,24 +23,24 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En
if(time(nullptr) < FirmwareVersion::BuildEpoch)
return false;
// if(data->getListType() >= 3) { // publish energy counts
// publishList3(data, ea);
// loop();
// }
if(data->getListType() >= 3) { // publish energy counts
publishList3(data, ea);
loop();
}
// if(data->getListType() == 1) { // publish power counts
// publishList1(data, ea);
// } else if(data->getListType() <= 3) { // publish power counts and volts/amps
// publishList2(data, ea);
// } else if(data->getListType() == 4) { // publish power counts and volts/amps/phase power and PF
// publishList4(data, ea);
// }
if(data->getListType() == 1) { // publish power counts
publishList1(data, ea);
} else if(data->getListType() <= 3) { // publish power counts and volts/amps
publishList2(data, ea);
} else if(data->getListType() == 4) { // publish power counts and volts/amps/phase power and PF
publishList4(data, ea);
}
loop();
// if(ea->isInitialized()) {
// publishRealtime(data, ea, eapi);
// loop();
// }
if(ea->isInitialized()) {
publishRealtime(data, ea, eapi);
loop();
}
return true;
}
@@ -319,7 +319,7 @@ bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) {
bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea) {
if(topic.isEmpty() || !mqtt.connected())
return false;
publishSystemSensors();
if(hw->getTemperature() > -50) publishTemperatureSensor(0, "");