diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index 6f270cd6..6c546afd 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -6,6 +6,7 @@ #include "time_sntp.h" #include "interface_mqtt.h" #include "ClassFlowPostProcessing.h" +#include "ClassLogFile.h" #include @@ -125,8 +126,12 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph) mainerrortopic = maintopic + "/connection"; printf("Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s\n", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str()); MQTTInit(uri, clientname, user, password, mainerrortopic, 60); - MQTTPublish(mainerrortopic, "connected", SetRetainFlag); - MQTTenable = true; + if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) { + MQTTenable = true; + } + else { + MQTTenable = true; + } } return true; @@ -141,8 +146,19 @@ string ClassFlowMQTT::GetMQTTMainTopic() bool ClassFlowMQTT::doFlow(string zwtime) { - if (!MQTTenable) - return true; + if (!MQTTenable) { + LogFile.WriteToFile("MQTT not enabled!"); + + // Try again to init it + MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, 60); + if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) { + MQTTenable = true; + } + else { // Failed + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + } + } + LogFile.WriteToFile("MQTT enabled"); std::string result; std::string resulterror = ""; @@ -153,7 +169,12 @@ bool ClassFlowMQTT::doFlow(string zwtime) string zw = ""; string namenumber = ""; - MQTTPublish(mainerrortopic, "connected"); + if (MQTTPublish(mainerrortopic, "connected")) { + MQTTenable = true; + } + else { // Failed, skip other topics + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + } zw = maintopic + "/" + "uptime"; char uptimeStr[11]; diff --git a/code/components/jomjol_mqtt/interface_mqtt.cpp b/code/components/jomjol_mqtt/interface_mqtt.cpp index ef98aff4..0641a45e 100644 --- a/code/components/jomjol_mqtt/interface_mqtt.cpp +++ b/code/components/jomjol_mqtt/interface_mqtt.cpp @@ -19,18 +19,29 @@ esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY; bool mqtt_connected = false; esp_mqtt_client_handle_t client = NULL; -void MQTTPublish(std::string _key, std::string _content, int retained_flag){ - if (client && mqtt_connected) { - int msg_id; - std::string zw; - msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag); - zw = "sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content; - if (debugdetail) LogFile.WriteToFile(zw); - ESP_LOGD(TAG_INTERFACEMQTT, "sent publish successful in MQTTPublish, msg_id=%d, %s, %s", msg_id, _key.c_str(), _content.c_str()); +bool MQTTPublish(std::string _key, std::string _content, int retained_flag){ + if (!client) { + LogFile.WriteToFile("MQTT - client not initialized!"); + return false; } - else { + + if (!mqtt_connected) { + LogFile.WriteToFile("MQTT - Can not publish, not connected!"); ESP_LOGW(TAG_INTERFACEMQTT, "Problem with Publish, client=%d, mqtt_connected %d", (int) client, (int) mqtt_connected); + return false; } + + int msg_id; + std::string zw; + msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag); + if (msg_id < 0) { + LogFile.WriteToFile("MQTT - Failed to publish + " + _key + ", no connection!"); + return false; + } + zw = "MQTT - sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content; + if (debugdetail) LogFile.WriteToFile(zw); + ESP_LOGD(TAG_INTERFACEMQTT, "sent publish successful in MQTTPublish, msg_id=%d, %s, %s", msg_id, _key.c_str(), _content.c_str()); + return true; } @@ -105,12 +116,15 @@ void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st .keepalive = _keepalive }; + LogFile.WriteToFile("MQTT - Init"); + if (_user.length() && _password.length()){ mqtt_cfg.username = _user.c_str(); mqtt_cfg.password = _password.c_str(); ESP_LOGI(TAG_INTERFACEMQTT, "Connect to MQTT: %s, %s", mqtt_cfg.username, mqtt_cfg.password); }; + MQTTdestroy(); client = esp_mqtt_client_init(&mqtt_cfg); if (client) { @@ -119,11 +133,13 @@ void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st if (esp_mqtt_client_start(client) != ESP_OK) LogFile.WriteToFile("MQTT - Could not start client!"); - MQTTPublish(_LWTContext, "", 1); + if(MQTTPublish(_LWTContext, "", 1)) { + LogFile.WriteToFile("MQTT - Client init successful"); + } } else { - LogFile.WriteToFile("MQTT - Could not Init MQTT Client!"); + LogFile.WriteToFile("MQTT - Could not Init client!"); } } @@ -185,6 +201,7 @@ void MQTTregisterSubscribeFunction(std::string topic, std::function>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) { it->second(); @@ -196,6 +213,7 @@ void MQTTconnected(){ for(std::map>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) { int msg_id = esp_mqtt_client_subscribe(client, it->first.c_str(), 0); ESP_LOGD(TAG_INTERFACEMQTT, "topic %s subscribe successful, msg_id=%d", it->first.c_str(), msg_id); + LogFile.WriteToFile("MQTT - topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id)); } } } diff --git a/code/components/jomjol_mqtt/interface_mqtt.h b/code/components/jomjol_mqtt/interface_mqtt.h index 397d1787..a6a8b96d 100644 --- a/code/components/jomjol_mqtt/interface_mqtt.h +++ b/code/components/jomjol_mqtt/interface_mqtt.h @@ -10,7 +10,7 @@ void MQTTdestroy(); //void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user = "", std::string _password = ""); -void MQTTPublish(std::string _key, std::string _content, int retained_flag = 1); // retained Flag as Standart +bool MQTTPublish(std::string _key, std::string _content, int retained_flag = 1); // retained Flag as Standart bool MQTTisConnected(); diff --git a/code/sdkconfig b/code/sdkconfig index 225d3a06..9b8daa3b 100644 --- a/code/sdkconfig +++ b/code/sdkconfig @@ -1028,7 +1028,7 @@ CONFIG_MQTT_TRANSPORT_SSL=y CONFIG_MQTT_TRANSPORT_WEBSOCKET=y CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y # CONFIG_MQTT_MSG_ID_INCREMENTAL is not set -# CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set +CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set # CONFIG_MQTT_REPORT_DELETED_MESSAGES is not set # CONFIG_MQTT_USE_CUSTOM_CONFIG is not set # CONFIG_MQTT_TASK_CORE_SELECTION_ENABLED is not set diff --git a/code/sdkconfig.esp32cam b/code/sdkconfig.esp32cam index 5437b6a8..5510ad43 100644 --- a/code/sdkconfig.esp32cam +++ b/code/sdkconfig.esp32cam @@ -1036,7 +1036,7 @@ CONFIG_MQTT_TRANSPORT_SSL=y CONFIG_MQTT_TRANSPORT_WEBSOCKET=y CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y # CONFIG_MQTT_MSG_ID_INCREMENTAL is not set -# CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set +CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED=y # CONFIG_MQTT_REPORT_DELETED_MESSAGES is not set # CONFIG_MQTT_USE_CUSTOM_CONFIG is not set # CONFIG_MQTT_TASK_CORE_SELECTION_ENABLED is not set