mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-07 12:06:58 +03:00
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:
@@ -4,6 +4,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "mqtt_client.h"
|
||||
#include "ClassLogFile.h"
|
||||
#include "server_tflite.h"
|
||||
|
||||
#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;
|
||||
|
||||
|
||||
int failedOnRound = -1;
|
||||
|
||||
esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
|
||||
// ESP_EVENT_ANY_ID
|
||||
|
||||
@@ -27,11 +30,18 @@ bool MQTTPublish(std::string _key, std::string _content, int retained_flag) {
|
||||
int msg_id;
|
||||
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) {
|
||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Not connected, trying to re-connect...");
|
||||
if (!MQTT_Init()) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
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
|
||||
failedOnRound = getCountFlowRounds();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -172,6 +183,7 @@ bool MQTT_Init() {
|
||||
mqtt_cfg.password = password.c_str();
|
||||
};
|
||||
|
||||
LogFile.WriteHeapInfo("MQTT Client Init");
|
||||
client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
if (client)
|
||||
{
|
||||
@@ -182,6 +194,7 @@ bool MQTT_Init() {
|
||||
return false;
|
||||
}
|
||||
|
||||
LogFile.WriteHeapInfo("MQTT Client Start");
|
||||
ret = esp_mqtt_client_start(client);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user