Skip to content

Commit

Permalink
Merge pull request #971 from caco3/add-mqtt-log-messages
Browse files Browse the repository at this point in the history
Improve MQTT handling
  • Loading branch information
jomjol authored Sep 3, 2022
2 parents d36a4db + eafcdb0 commit 77427a8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
31 changes: 26 additions & 5 deletions code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "time_sntp.h"
#include "interface_mqtt.h"
#include "ClassFlowPostProcessing.h"
#include "ClassLogFile.h"

#include <time.h>

Expand Down Expand Up @@ -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;
Expand All @@ -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 is now enabled");
}

std::string result;
std::string resulterror = "";
Expand All @@ -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];
Expand Down
42 changes: 30 additions & 12 deletions code/components/jomjol_mqtt/interface_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
else {
bool MQTTPublish(std::string _key, std::string _content, int retained_flag){
if (!client) {
LogFile.WriteToFile("MQTT - client not initialized!");
return false;
}

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;
}


Expand Down Expand Up @@ -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)
{
Expand All @@ -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!");
}

}
Expand Down Expand Up @@ -185,6 +201,7 @@ void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::st

void MQTTconnected(){
if (mqtt_connected) {
LogFile.WriteToFile("MQTT - Connected");
if (connectFunktionMap != NULL) {
for(std::map<std::string, std::function<void()>>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) {
it->second();
Expand All @@ -196,6 +213,7 @@ void MQTTconnected(){
for(std::map<std::string, std::function<bool(std::string, char*, int)>>::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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_mqtt/interface_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion code/sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion code/sdkconfig.esp32cam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 77427a8

Please sign in to comment.