added callback on connected

This commit is contained in:
CaCO3
2022-10-27 12:18:23 +02:00
parent f4ae688527
commit 7a280bc7c6
4 changed files with 52 additions and 45 deletions

View File

@@ -16,7 +16,31 @@
static const char *TAG = "class_flow_MQTT"; static const char *TAG = "class_flow_MQTT";
extern ClassFlowControll tfliteflow; void publishRuntimeData(std::string maintopic, int SetRetainFlag) {
char tmp_char[50];
sprintf(tmp_char, "%ld", (long)getUpTime());
MQTTPublish(maintopic + "/" + "uptime", std::string(tmp_char), SetRetainFlag);
sprintf(tmp_char, "%zu", esp_get_free_heap_size());
MQTTPublish(maintopic + "/" + "freeMem", std::string(tmp_char), SetRetainFlag);
sprintf(tmp_char, "%d", get_WIFI_RSSI());
MQTTPublish(maintopic + "/" + "wifiRSSI", std::string(tmp_char), SetRetainFlag);
sprintf(tmp_char, "%d", (int)temperatureRead());
MQTTPublish(maintopic + "/" + "CPUtemp", std::string(tmp_char), SetRetainFlag);
}
void GotConnected(std::string maintopic, int SetRetainFlag) {
MQTTPublish(maintopic + "/" + "mac", getMac(), SetRetainFlag);
MQTTPublish(maintopic + "/" + "ip", *getIPAddress(), SetRetainFlag);
MQTTPublish(maintopic + "/" + "hostname", hostname, SetRetainFlag);
publishRuntimeData(maintopic, SetRetainFlag);
}
void ClassFlowMQTT::SetInitialParameter(void) void ClassFlowMQTT::SetInitialParameter(void)
{ {
@@ -25,7 +49,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
topicError = ""; topicError = "";
topicRate = ""; topicRate = "";
topicTimeStamp = ""; topicTimeStamp = "";
maintopic = ""; maintopic = hostname;
topicUptime = ""; topicUptime = "";
topicFreeMem = ""; topicFreeMem = "";
@@ -133,25 +157,15 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
{ {
maintopic = zerlegt[1]; maintopic = zerlegt[1];
} }
else { // Main topic not set, use the hostname
maintopic = hostname;
}
} }
MQTT_Configure(uri, clientname, user, password, maintopic, "connection", keepAlive); MQTT_Configure(uri, clientname, user, password, maintopic, "connection", keepAlive, SetRetainFlag, (void *)&GotConnected);
if (!MQTT_Init()) { if (!MQTT_Init()) {
if (!MQTT_Init()) { // Retry if (!MQTT_Init()) { // Retry
return false; return false;
} }
} }
MQTTPublish(maintopic + "/" + "mac", getMac(), SetRetainFlag);
MQTTPublish(maintopic + "/" + "ip", *getIPAddress(), SetRetainFlag);
MQTTPublish(maintopic + "/" + "hostname", hostname, SetRetainFlag);
publishRuntimeData();
return true; return true;
} }
@@ -161,22 +175,6 @@ string ClassFlowMQTT::GetMQTTMainTopic()
return maintopic; return maintopic;
} }
void ClassFlowMQTT::publishRuntimeData() {
char tmp_char[50];
sprintf(tmp_char, "%ld", (long)getUpTime());
MQTTPublish(maintopic + "/" + "uptime", std::string(tmp_char), SetRetainFlag);
sprintf(tmp_char, "%zu", esp_get_free_heap_size());
MQTTPublish(maintopic + "/" + "freeMem", std::string(tmp_char), SetRetainFlag);
sprintf(tmp_char, "%d", get_WIFI_RSSI());
MQTTPublish(maintopic + "/" + "wifiRSSI", std::string(tmp_char), SetRetainFlag);
sprintf(tmp_char, "%d", (int)temperatureRead());
MQTTPublish(maintopic + "/" + "CPUtemp", std::string(tmp_char), SetRetainFlag);
}
bool ClassFlowMQTT::doFlow(string zwtime) bool ClassFlowMQTT::doFlow(string zwtime)
{ {
@@ -189,7 +187,7 @@ bool ClassFlowMQTT::doFlow(string zwtime)
string zw = ""; string zw = "";
string namenumber = ""; string namenumber = "";
publishRuntimeData(); publishRuntimeData(maintopic, SetRetainFlag);
if (flowpostprocessing) if (flowpostprocessing)
{ {

View File

@@ -27,7 +27,6 @@ public:
string GetMQTTMainTopic(); string GetMQTTMainTopic();
bool ReadParameter(FILE* pfile, string& aktparamgraph); bool ReadParameter(FILE* pfile, string& aktparamgraph);
void publishRuntimeData();
bool doFlow(string time); bool doFlow(string time);
string name(){return "ClassFlowMQTT";}; string name(){return "ClassFlowMQTT";};
}; };

View File

@@ -19,8 +19,9 @@ esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
bool mqtt_connected = false; bool mqtt_connected = false;
esp_mqtt_client_handle_t client = NULL; esp_mqtt_client_handle_t client = NULL;
std::string uri, client_id, lwt_topic, user, password; std::string uri, client_id, lwt_topic, user, password, maintopic;
int keepalive; int keepalive, SetRetainFlag;
void (*callbackOnConnected)(std::string, int) = NULL;
bool MQTTPublish(std::string _key, std::string _content, int retained_flag) { bool MQTTPublish(std::string _key, std::string _content, int retained_flag) {
@@ -115,13 +116,14 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
} }
void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _maintopic, std::string _lwt, int _keepalive){ void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
std::string _maintopic, std::string _lwt, int _keepalive, int _SetRetainFlag, void *_callbackOnConnected){
#ifdef __HIDE_PASSWORD #ifdef __HIDE_PASSWORD
LogFile.WriteToFile(ESP_LOG_INFO, "MQTT Configuration: uri: " + _mqttURI + ", clientname: " + _clientid + LogFile.WriteToFile(ESP_LOG_INFO, "MQTT Configuration: uri: " + _mqttURI + ", clientname: " + _clientid +
", user: " + _user + ", password: XXXXXXXX, last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); ", user: " + _user + ", password: XXXXXXXX, maintopic: " + _maintopic + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive));
#else #else
LogFile.WriteToFile(ESP_LOG_INFO, "MQTT Configuration: uri: " + _mqttURI + ", clientname: " + _clientid + LogFile.WriteToFile(ESP_LOG_INFO, "MQTT Configuration: uri: " + _mqttURI + ", clientname: " + _clientid +
", user: " + _user + ", password: " + _password + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); ", user: " + _user + ", password: " + _password + ", maintopic: " + _maintopic + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive));
#endif #endif
@@ -129,17 +131,18 @@ void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
client_id = _clientid; client_id = _clientid;
lwt_topic = _maintopic + "/" + _lwt; lwt_topic = _maintopic + "/" + _lwt;
keepalive = _keepalive; keepalive = _keepalive;
SetRetainFlag = _SetRetainFlag;
maintopic = _maintopic;
callbackOnConnected = ( void (*)(std::string, int) )(_callbackOnConnected);
if (_user.length() && _password.length()){ if (_user.length() && _password.length()){
user = _user; user = _user;
password = _password; password = _password;
} }
LogFile.WriteToFile(ESP_LOG_INFO, "MQTT maintopic: " + _maintopic);
} }
bool MQTT_Init() { bool MQTT_Init() {
esp_err_t ret;
LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init"); LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init");
MQTTdestroy_client(); MQTTdestroy_client();
@@ -164,14 +167,16 @@ bool MQTT_Init() {
client = esp_mqtt_client_init(&mqtt_cfg); client = esp_mqtt_client_init(&mqtt_cfg);
if (client) if (client)
{ {
if (esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client) != ESP_OK) ret = esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client);
if (ret != ESP_OK)
{ {
LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not register event!"); LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not register event (ret=" + std::to_string(ret) + ")!");
return false; return false;
} }
if (esp_mqtt_client_start(client) != ESP_OK) ret = esp_mqtt_client_start(client);
if (ret != ESP_OK)
{ {
LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not start client!"); LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not start client (ret=" + std::to_string(ret) + ")!");
return false; return false;
} }
} }
@@ -260,6 +265,10 @@ void MQTTconnected(){
LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id)); LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id));
} }
} }
if (callbackOnConnected) {
callbackOnConnected(maintopic, SetRetainFlag);
}
} }
} }

View File

@@ -5,7 +5,8 @@
#include <map> #include <map>
#include <functional> #include <functional>
void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _maintopic, std::string _lwt, int _keepalive); void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
std::string _maintopic, std::string _lwt, int _keepalive, int SetRetainFlag, void *callbackOnConnected);
bool MQTT_Init(); bool MQTT_Init();
void MQTTdestroy_client(); void MQTTdestroy_client();