renamed mainerrortopic with lwt, only send it once on connecting, minor fixes and cleanups

This commit is contained in:
CaCO3
2022-10-26 10:34:15 +02:00
parent 6b47eef4cd
commit 98451c8fc7
2 changed files with 39 additions and 60 deletions

View File

@@ -22,7 +22,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
topicRate = ""; topicRate = "";
topicTimeStamp = ""; topicTimeStamp = "";
maintopic = ""; maintopic = "";
mainerrortopic = ""; lwt = "";
topicUptime = ""; topicUptime = "";
topicFreeMem = ""; topicFreeMem = "";
@@ -121,66 +121,51 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
{ {
maintopic = zerlegt[1]; maintopic = zerlegt[1];
} }
else { // Main topic not set, use the hostname
maintopic = hostname;
}
} }
#ifdef __HIDE_PASSWORD #ifdef __HIDE_PASSWORD
ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str()); ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str());
#else #else
ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str()); ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str());
#endif #endif
if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0)) if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0))
{ {
ESP_LOGD(TAG, "InitMQTTInit"); ESP_LOGD(TAG, "InitMQTTInit");
mainerrortopic = maintopic + "/connection"; lwt = maintopic + "/connection";
#ifdef __HIDE_PASSWORD #ifdef __HIDE_PASSWORD
ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str()); ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str());
#else #else
ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str()); ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str());
#endif #endif
if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive)) if (!MQTTInit(uri, clientname, user, password, lwt, keepAlive))
{ // Failed { // Failed
MQTTenable = false; MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot! 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 // Try sending LWT. If it fails, re-run init
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) if (!MQTTPublish(lwt, "connected", SetRetainFlag))
{ // Failed { // Failed
LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!"); LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!");
if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive)) if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive))
{ // Failed { // Failed
MQTTenable = false; MQTTenable = false;
return false; return false;
} }
// Try again sending LWT and quit if it fails
if (!MQTTPublish(lwt, "connected", SetRetainFlag))
{ // 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(ESP_LOG_ERROR, "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(ESP_LOG_ERROR, "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; MQTTenable = true;
return true; return true;
} }
@@ -194,24 +179,6 @@ string ClassFlowMQTT::GetMQTTMainTopic()
bool ClassFlowMQTT::doFlow(string zwtime) bool ClassFlowMQTT::doFlow(string zwtime)
{ {
// Try sending mainerrortopic. If it fails, re-run init
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
{ // Failed
LogFile.WriteToFile(ESP_LOG_WARN, "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!
}
}
// 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; std::string result;
std::string resulterror = ""; std::string resulterror = "";
std::string resultraw = ""; std::string resultraw = "";
@@ -221,16 +188,28 @@ bool ClassFlowMQTT::doFlow(string zwtime)
string zw = ""; string zw = "";
string namenumber = ""; string namenumber = "";
// 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"; zw = maintopic + "/" + "uptime";
char uptimeStr[11]; char uptimeStr[11];
sprintf(uptimeStr, "%ld", (long)getUpTime()); sprintf(uptimeStr, "%ld", (long)getUpTime());
MQTTPublish(zw, uptimeStr, SetRetainFlag);
// Try sending uptime. If it fails, re-run init
if (!MQTTPublish(zw, uptimeStr, SetRetainFlag))
{ // Failed
LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!");
if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive))
{ // Failed
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
// Try again and quit if it fails
if (!MQTTPublish(zw, uptimeStr, SetRetainFlag))
{ // Failed
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
}
zw = maintopic + "/" + "freeMem"; zw = maintopic + "/" + "freeMem";
char freeheapmem[11]; char freeheapmem[11];
sprintf(freeheapmem, "%zu", esp_get_free_heap_size()); sprintf(freeheapmem, "%zu", esp_get_free_heap_size());

View File

@@ -17,7 +17,7 @@ protected:
bool MQTTenable; bool MQTTenable;
int keepAlive; int keepAlive;
std::string maintopic, mainerrortopic; std::string maintopic, lwt;
void SetInitialParameter(void); void SetInitialParameter(void);
public: public: