mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-11 22:16:56 +03:00
Merge pull request #980 from caco3/re-init-mqtt-on-publish-error
Try to publish. If it fails, run MQTT init and publish again.
This commit is contained in:
@@ -32,9 +32,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
|
|||||||
ListFlowControll = NULL;
|
ListFlowControll = NULL;
|
||||||
disabled = false;
|
disabled = false;
|
||||||
MQTTenable = false;
|
MQTTenable = false;
|
||||||
|
keepAlive = 600; // TODO This must be greater than the Flow Interval!
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ClassFlowMQTT::ClassFlowMQTT()
|
ClassFlowMQTT::ClassFlowMQTT()
|
||||||
@@ -125,15 +123,50 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
|
|||||||
printf("InitMQTTInit\n");
|
printf("InitMQTTInit\n");
|
||||||
mainerrortopic = maintopic + "/connection";
|
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());
|
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 (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive))
|
||||||
if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) {
|
{ // Failed
|
||||||
MQTTenable = true;
|
MQTTenable = false;
|
||||||
}
|
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
|
||||||
else {
|
|
||||||
MQTTenable = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,18 +179,43 @@ string ClassFlowMQTT::GetMQTTMainTopic()
|
|||||||
|
|
||||||
bool ClassFlowMQTT::doFlow(string zwtime)
|
bool ClassFlowMQTT::doFlow(string zwtime)
|
||||||
{
|
{
|
||||||
if (!MQTTenable) {
|
// if (!MQTTenable) {
|
||||||
LogFile.WriteToFile("MQTT not enabled!");
|
// 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);
|
// Try sending mainerrortopic. If it fails, re-run init
|
||||||
if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) {
|
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
|
||||||
MQTTenable = true;
|
{ // Failed
|
||||||
}
|
LogFile.WriteToFile("MQTT - Re-running init...!");
|
||||||
else { // Failed
|
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!
|
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;
|
std::string result;
|
||||||
@@ -169,12 +227,10 @@ bool ClassFlowMQTT::doFlow(string zwtime)
|
|||||||
string zw = "";
|
string zw = "";
|
||||||
string namenumber = "";
|
string namenumber = "";
|
||||||
|
|
||||||
if (MQTTPublish(mainerrortopic, "connected")) {
|
// if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
|
||||||
MQTTenable = true;
|
//{ // Failed, skip other topics
|
||||||
}
|
// return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
|
||||||
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";
|
zw = maintopic + "/" + "uptime";
|
||||||
char uptimeStr[11];
|
char uptimeStr[11];
|
||||||
@@ -184,7 +240,10 @@ bool ClassFlowMQTT::doFlow(string zwtime)
|
|||||||
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());
|
||||||
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";
|
zw = maintopic + "/" + "wifiRSSI";
|
||||||
char rssi[11];
|
char rssi[11];
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ protected:
|
|||||||
std::string user, password;
|
std::string user, password;
|
||||||
int SetRetainFlag;
|
int SetRetainFlag;
|
||||||
bool MQTTenable;
|
bool MQTTenable;
|
||||||
|
int keepAlive;
|
||||||
|
|
||||||
std::string maintopic, mainerrortopic;
|
std::string maintopic, mainerrortopic;
|
||||||
void SetInitialParameter(void);
|
void SetInitialParameter(void);
|
||||||
|
|||||||
@@ -20,22 +20,36 @@ bool mqtt_connected = false;
|
|||||||
esp_mqtt_client_handle_t client = NULL;
|
esp_mqtt_client_handle_t client = NULL;
|
||||||
|
|
||||||
bool MQTTPublish(std::string _key, std::string _content, int retained_flag){
|
bool MQTTPublish(std::string _key, std::string _content, int retained_flag){
|
||||||
if (!client) {
|
|
||||||
LogFile.WriteToFile("MQTT - client not initialized!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!mqtt_connected) {
|
// if (!client) {
|
||||||
LogFile.WriteToFile("MQTT - Can not publish, not connected!");
|
// LogFile.WriteToFile("MQTT - client not initialized!");
|
||||||
ESP_LOGW(TAG_INTERFACEMQTT, "Problem with Publish, client=%d, mqtt_connected %d", (int) client, (int) mqtt_connected);
|
// 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 (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;
|
return false;
|
||||||
}
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
int msg_id;
|
int msg_id;
|
||||||
std::string zw;
|
std::string zw;
|
||||||
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("MQTT - Failed to publish + " + _key + ", no connection!");
|
LogFile.WriteToFile("MQTT - Failed to publish '" + _key + "'!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
zw = "MQTT - sent publish successful in MQTTPublish, msg_id=" + std::to_string(msg_id) + ", " + _key + ", " + _content;
|
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";
|
std::string _zwmessage = "connection lost";
|
||||||
|
|
||||||
int _lzw = _zwmessage.length();
|
int _lzw = _zwmessage.length();
|
||||||
@@ -141,20 +155,30 @@ void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, st
|
|||||||
if (client)
|
if (client)
|
||||||
{
|
{
|
||||||
if (esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client) != ESP_OK)
|
if (esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client) != ESP_OK)
|
||||||
|
{
|
||||||
LogFile.WriteToFile("MQTT - Could not register event!");
|
LogFile.WriteToFile("MQTT - Could not register event!");
|
||||||
if (esp_mqtt_client_start(client) != ESP_OK)
|
return false;
|
||||||
LogFile.WriteToFile("MQTT - Could not start client!");
|
|
||||||
|
|
||||||
if(MQTTPublish(_LWTContext, "", 1)) {
|
|
||||||
LogFile.WriteToFile("MQTT - Client init successful");
|
|
||||||
}
|
}
|
||||||
|
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
|
else
|
||||||
{
|
{
|
||||||
LogFile.WriteToFile("MQTT - Could not Init client!");
|
LogFile.WriteToFile("MQTT - Could not Init client!");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LogFile.WriteToFile("MQTT - Init successful");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
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 MQTTdestroy();
|
||||||
|
|
||||||
//void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user = "", std::string _password = "");
|
//void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user = "", std::string _password = "");
|
||||||
|
|||||||
Reference in New Issue
Block a user