diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp index 49a82104..b82f7afe 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -32,9 +32,7 @@ void ClassFlowMQTT::SetInitialParameter(void) ListFlowControll = NULL; disabled = false; MQTTenable = false; - - - + keepAlive = 600; // TODO This must be greater than the Flow Interval! } ClassFlowMQTT::ClassFlowMQTT() @@ -125,15 +123,50 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph) printf("InitMQTTInit\n"); 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); - if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) { - MQTTenable = true; - } - else { - MQTTenable = true; + if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive)) + { // Failed + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! } } + + // Try sending mainerrortopic. If it fails, re-run init + if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + { // Failed + LogFile.WriteToFile("MQTT - Re-running init...!"); + if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) + { // Failed + MQTTenable = false; + return false; + } + } + + // Try again and quit if it fails + if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + { // Failed + MQTTenable = false; + return false; + } + + + + /* if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + { // Failed + LogFile.WriteToFile("MQTT - Could not publish connection status!"); + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + }*/ + + /* if(!MQTTPublish(_LWTContext, "", 1)) + { + LogFile.WriteToFile("MQTT - Could not publish LWT!"); + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + }*/ + + + MQTTenable = true; return true; } @@ -146,18 +179,43 @@ string ClassFlowMQTT::GetMQTTMainTopic() bool ClassFlowMQTT::doFlow(string zwtime) { - if (!MQTTenable) { - LogFile.WriteToFile("MQTT not enabled!"); + // if (!MQTTenable) { + // LogFile.WriteToFile("MQTT not enabled!"); + // + // // Try again to init it + // if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) + // { // Failed + // MQTTenable = false; + // return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + // } + // + // if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + // { // Failed + // MQTTenable = false; + // return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + // } + // + // LogFile.WriteToFile("MQTT is now enabled"); + // MQTTenable = true; + // } - // 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 + + // Try sending mainerrortopic. If it fails, re-run init + if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + { // Failed + LogFile.WriteToFile("MQTT - Re-running init...!"); + if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) + { // Failed + MQTTenable = false; return true; // We need to return true despite we failed, else it will retry 5x and then reboot! - } - LogFile.WriteToFile("MQTT is now enabled"); + } + } + + // Try again and quit if it fails + if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + { // Failed + MQTTenable = false; + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! } std::string result; @@ -169,12 +227,10 @@ bool ClassFlowMQTT::doFlow(string zwtime) string zw = ""; string namenumber = ""; - 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! - } + // if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) + //{ // 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]; @@ -184,7 +240,10 @@ bool ClassFlowMQTT::doFlow(string zwtime) zw = maintopic + "/" + "freeMem"; char freeheapmem[11]; sprintf(freeheapmem, "%zu", esp_get_free_heap_size()); - MQTTPublish(zw, freeheapmem, SetRetainFlag); + if (!MQTTPublish(zw, freeheapmem, SetRetainFlag)) + { // Failed, skip other topics + return true; // We need to return true despite we failed, else it will retry 5x and then reboot! + } zw = maintopic + "/" + "wifiRSSI"; char rssi[11]; diff --git a/code/components/jomjol_flowcontroll/ClassFlowMQTT.h b/code/components/jomjol_flowcontroll/ClassFlowMQTT.h index 816389b1..3a990c7d 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowMQTT.h +++ b/code/components/jomjol_flowcontroll/ClassFlowMQTT.h @@ -15,6 +15,7 @@ protected: std::string user, password; int SetRetainFlag; bool MQTTenable; + int keepAlive; std::string maintopic, mainerrortopic; void SetInitialParameter(void); diff --git a/code/components/jomjol_mqtt/interface_mqtt.cpp b/code/components/jomjol_mqtt/interface_mqtt.cpp index a11003ac..556a50d8 100644 --- a/code/components/jomjol_mqtt/interface_mqtt.cpp +++ b/code/components/jomjol_mqtt/interface_mqtt.cpp @@ -20,22 +20,36 @@ bool mqtt_connected = false; esp_mqtt_client_handle_t client = NULL; bool MQTTPublish(std::string _key, std::string _content, int retained_flag){ - if (!client) { - LogFile.WriteToFile("MQTT - client not initialized!"); - return false; - } + + // if (!client) { + // LogFile.WriteToFile("MQTT - client not initialized!"); + // return false; + // } + // LogFile.WriteToFile("MQTT - client initialized!"); // Debug + // + // 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; + // } + // LogFile.WriteToFile("MQTT - connected!"); // Debug - 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; + /* if (client && mqtt_connected) { + LogFile.WriteToFile("MQTT - connected!"); // Debug } + else { // init needed + if (!MQTTInit(this->uri, this->clientname, this->user, password, mainerrortopic, keepAlive)) // validate{ + { // Failed + 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!"); + LogFile.WriteToFile("MQTT - Failed to publish '" + _key + "'!"); return false; } zw = "MQTT - sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content; @@ -102,7 +116,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ } -void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _LWTContext, int _keepalive){ +bool MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _LWTContext, int _keepalive){ std::string _zwmessage = "connection lost"; int _lzw = _zwmessage.length(); @@ -141,20 +155,30 @@ void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st if (client) { if (esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client) != ESP_OK) + { LogFile.WriteToFile("MQTT - Could not register event!"); - if (esp_mqtt_client_start(client) != ESP_OK) - LogFile.WriteToFile("MQTT - Could not start client!"); - - if(MQTTPublish(_LWTContext, "", 1)) { - LogFile.WriteToFile("MQTT - Client init successful"); + return false; } + if (esp_mqtt_client_start(client) != ESP_OK) + { + LogFile.WriteToFile("MQTT - Could not start client!"); + return false; + } + + /* if(!MQTTPublish(_LWTContext, "", 1)) + { + LogFile.WriteToFile("MQTT - Could not publish LWT!"); + return false; + }*/ } else { LogFile.WriteToFile("MQTT - Could not Init client!"); + return false; } - + LogFile.WriteToFile("MQTT - Init successful"); + return true; } /* @@ -256,7 +280,7 @@ void MQTTconnected(){ } } - if (subscribeFunktionMap != NULL) { + if (subscribeFunktionMap != NULL) { 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); diff --git a/code/components/jomjol_mqtt/interface_mqtt.h b/code/components/jomjol_mqtt/interface_mqtt.h index a6a8b96d..06210782 100644 --- a/code/components/jomjol_mqtt/interface_mqtt.h +++ b/code/components/jomjol_mqtt/interface_mqtt.h @@ -5,7 +5,7 @@ #include #include -void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _LWTContext, int _keepalive); +bool MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _LWTContext, int _keepalive); void MQTTdestroy(); //void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user = "", std::string _password = "");