MQTT improvements (#1302)

* Update server_mqtt.cpp

* Update server_mqtt.cpp

* skipp all MQTT publishing until the next round if an error occures

* improve logging

* only use group for uid and topic if there is more than one number

* .

* .
This commit is contained in:
CaCO3
2022-11-09 18:25:24 +01:00
committed by GitHub
parent 2314d7ef18
commit 2b0e0f7d4e
7 changed files with 32 additions and 26 deletions

View File

@@ -790,8 +790,7 @@ bool ClassFlowCNNGeneral::doNeuralNetwork(string time)
string zw = "_num (p, m): " + to_string(_num) + " " + to_string(_numplus) + " " + to_string(_numminus); string zw = "_num (p, m): " + to_string(_num) + " " + to_string(_numplus) + " " + to_string(_numminus);
zw = zw + " _val (p, m): " + to_string(_val) + " " + to_string(_valplus) + " " + to_string(_valminus); zw = zw + " _val (p, m): " + to_string(_val) + " " + to_string(_valplus) + " " + to_string(_valminus);
zw = zw + " result: " + to_string(result) + " _fit: " + to_string(_fit); zw = zw + " result: " + to_string(result) + " _fit: " + to_string(_fit);
ESP_LOGD(TAG, "details cnn: %s", zw.c_str()); LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zw);
LogFile.WriteToFile(ESP_LOG_INFO, TAG, zw);
_result_save_file = result; _result_save_file = result;

View File

@@ -129,7 +129,7 @@ void ClassFlowImage::RemoveOldLogs()
} }
} }
} }
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Image folder deleted: " + std::to_string(deleted) + ". Image folder not deleted: " + std::to_string(notDeleted)); LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Image folder deleted: " + std::to_string(deleted) + ". Image folder not deleted: " + std::to_string(notDeleted));
closedir(dir); closedir(dir);
} }

View File

@@ -230,6 +230,8 @@ bool ClassFlowMQTT::doFlow(string zwtime)
{ {
std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers(); std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Publishing MQTT topics...");
for (int i = 0; i < (*NUMBERS).size(); ++i) for (int i = 0; i < (*NUMBERS).size(); ++i)
{ {
result = (*NUMBERS)[i]->ReturnValue; result = (*NUMBERS)[i]->ReturnValue;
@@ -245,7 +247,6 @@ bool ClassFlowMQTT::doFlow(string zwtime)
else else
namenumber = maintopic + "/" + namenumber + "/"; namenumber = maintopic + "/" + namenumber + "/";
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Publishing MQTT topics...");
if (result.length() > 0) if (result.length() > 0)
MQTTPublish(namenumber + "value", result, SetRetainFlag); MQTTPublish(namenumber + "value", result, SetRetainFlag);

View File

@@ -327,7 +327,7 @@ void ClassLogFile::RemoveOld()
} }
} }
} }
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "logfiles deleted: " + std::to_string(deleted) + " files not deleted (incl. leer.txt): " + std::to_string(notDeleted)); LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "logfiles deleted: " + std::to_string(deleted) + " files not deleted (incl. leer.txt): " + std::to_string(notDeleted));
closedir(dir); closedir(dir);

View File

@@ -4,6 +4,7 @@
#include "esp_log.h" #include "esp_log.h"
#include "mqtt_client.h" #include "mqtt_client.h"
#include "ClassLogFile.h" #include "ClassLogFile.h"
#include "server_tflite.h"
#define __HIDE_PASSWORD #define __HIDE_PASSWORD
@@ -13,6 +14,8 @@ std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
std::map<std::string, std::function<bool(std::string, char*, int)>>* subscribeFunktionMap = NULL; std::map<std::string, std::function<bool(std::string, char*, int)>>* subscribeFunktionMap = NULL;
int failedOnRound = -1;
esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY; esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
// ESP_EVENT_ANY_ID // ESP_EVENT_ANY_ID
@@ -27,11 +30,18 @@ bool MQTTPublish(std::string _key, std::string _content, int retained_flag) {
int msg_id; int msg_id;
std::string zw; std::string zw;
if (failedOnRound == getCountFlowRounds()) { // we already failed in this round, do not retry until the next round
return true; // Fail quietly
}
LogFile.WriteHeapInfo("MQTT Publish");
if (!mqtt_connected) { if (!mqtt_connected) {
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Not connected, trying to re-connect..."); LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Not connected, trying to re-connect...");
if (!MQTT_Init()) { if (!MQTT_Init()) {
if (!MQTT_Init()) { // Retry if (!MQTT_Init()) { // Retry
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to init!"); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to init, skipping all MQTT publishings in this round!");
failedOnRound = getCountFlowRounds();
return false; return false;
} }
} }
@@ -43,8 +53,9 @@ bool MQTTPublish(std::string _key, std::string _content, int retained_flag) {
msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag); msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag);
if (msg_id < 0) { if (msg_id < 0) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to publish topic '" + _key + "'!"); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to publish topic '" + _key + "', skipping all MQTT publishings in this round!");
mqtt_connected = false; // Force re-init on next call mqtt_connected = false; // Force re-init on next call
failedOnRound = getCountFlowRounds();
return false; return false;
} }
} }
@@ -172,6 +183,7 @@ bool MQTT_Init() {
mqtt_cfg.password = password.c_str(); mqtt_cfg.password = password.c_str();
}; };
LogFile.WriteHeapInfo("MQTT Client Init");
client = esp_mqtt_client_init(&mqtt_cfg); client = esp_mqtt_client_init(&mqtt_cfg);
if (client) if (client)
{ {
@@ -182,6 +194,7 @@ bool MQTT_Init() {
return false; return false;
} }
LogFile.WriteHeapInfo("MQTT Client Start");
ret = esp_mqtt_client_start(client); ret = esp_mqtt_client_start(client);
if (ret != ESP_OK) if (ret != ESP_OK)
{ {

View File

@@ -52,39 +52,30 @@ void sendHomeAssistantDiscoveryTopic(std::string group, std::string field,
version = std::string(libfive_git_branch()) + " (" + std::string(libfive_git_revision()) + ")"; version = std::string(libfive_git_branch()) + " (" + std::string(libfive_git_revision()) + ")";
} }
std::string topic;
std::string topicFull; std::string topicFull;
std::string topicT; std::string configTopic;
std::string payload; std::string payload;
std::string nl = "\n"; std::string nl = "\n";
if (group == "") { configTopic = field;
topic = field;
topicT = field;
}
else {
topic = group + "/" + field;
topicT = group + "_" + field;
}
if ((*NUMBERS).size() > 1) { // There is more than one meter, prepend the group so we can differentiate them if (group != "" && (*NUMBERS).size() > 1) { // There is more than one meter, prepend the group so we can differentiate them
if (group != "") { // But only if the group is set configTopic = group + "_" + field;
name = group + " " + name; name = group + " " + name;
}
} }
if (field == "problem") { // Special binary sensor which is based on error topic if (field == "problem") { // Special binary sensor which is based on error topic
topicFull = "homeassistant/binary_sensor/" + maintopic + "/" + topicT + "/config"; topicFull = "homeassistant/binary_sensor/" + maintopic + "/" + configTopic + "/config";
} }
else { else {
topicFull = "homeassistant/sensor/" + maintopic + "/" + topicT + "/config"; topicFull = "homeassistant/sensor/" + maintopic + "/" + configTopic + "/config";
} }
/* See https://www.home-assistant.io/docs/mqtt/discovery/ */ /* See https://www.home-assistant.io/docs/mqtt/discovery/ */
payload = "{" + nl + payload = "{" + nl +
"\"~\": \"" + maintopic + "\"," + nl + "\"~\": \"" + maintopic + "\"," + nl +
"\"unique_id\": \"" + maintopic + "-" + topicT + "\"," + nl + "\"unique_id\": \"" + maintopic + "-" + configTopic + "\"," + nl +
"\"object_id\": \"" + maintopic + "_" + topicT + "\"," + nl + // This used to generate the Entity ID "\"object_id\": \"" + maintopic + "_" + configTopic + "\"," + nl + // This used to generate the Entity ID
"\"name\": \"" + name + "\"," + nl + "\"name\": \"" + name + "\"," + nl +
"\"icon\": \"mdi:" + icon + "\"," + nl; "\"icon\": \"mdi:" + icon + "\"," + nl;

View File

@@ -17,6 +17,8 @@ bool isSetupModusActive();
std::string GetMQTTMainTopic(); std::string GetMQTTMainTopic();
int getCountFlowRounds();
esp_err_t GetJPG(std::string _filename, httpd_req_t *req); esp_err_t GetJPG(std::string _filename, httpd_req_t *req);
esp_err_t GetRawJPG(httpd_req_t *req); esp_err_t GetRawJPG(httpd_req_t *req);