mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-11 14:07:00 +03:00
Update interface_mqtt.cpp
This commit is contained in:
@@ -52,7 +52,7 @@ bool MQTTPublish(std::string _key, std::string _content, int qos, bool retained_
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_DETAIL_ON
|
#ifdef DEBUG_DETAIL_ON
|
||||||
LogFile.WriteHeapInfo("MQTT Publish");
|
Logfile.WriteHeapInfo("MQTT Publish");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MQTT_Init(); // Re-Init client if not initialized yet/anymore
|
MQTT_Init(); // Re-Init client if not initialized yet/anymore
|
||||||
@@ -63,19 +63,19 @@ bool MQTTPublish(std::string _key, std::string _content, int qos, bool retained_
|
|||||||
#endif
|
#endif
|
||||||
int msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, qos, retained_flag);
|
int msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, qos, retained_flag);
|
||||||
#ifdef DEBUG_DETAIL_ON
|
#ifdef DEBUG_DETAIL_ON
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publish msg_id %d in %lld ms", msg_id, (esp_timer_get_time() - starttime)/1000);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publish msg_id %d in %lld ms", msg_id, (esp_timer_get_time() - starttime)/1000);
|
||||||
#endif
|
#endif
|
||||||
if (msg_id == -1) {
|
if (msg_id == -1) {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Failed to publish topic '" + _key + "', re-trying...");
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "Failed to publish topic '" + _key + "', re-trying...");
|
||||||
#ifdef DEBUG_DETAIL_ON
|
#ifdef DEBUG_DETAIL_ON
|
||||||
starttime = esp_timer_get_time();
|
starttime = esp_timer_get_time();
|
||||||
#endif
|
#endif
|
||||||
msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, qos, retained_flag);
|
msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, qos, retained_flag);
|
||||||
#ifdef DEBUG_DETAIL_ON
|
#ifdef DEBUG_DETAIL_ON
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publish msg_id %d in %lld ms", msg_id, (esp_timer_get_time() - starttime)/1000);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publish msg_id %d in %lld ms", msg_id, (esp_timer_get_time() - starttime)/1000);
|
||||||
#endif
|
#endif
|
||||||
if (msg_id == -1) {
|
if (msg_id == -1) {
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to publish topic '" + _key + "', skipping all MQTT publishings in this round!");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to publish topic '" + _key + "', skipping all MQTT publishings in this round!");
|
||||||
failedOnRound = getCountFlowRounds();
|
failedOnRound = getCountFlowRounds();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -86,11 +86,11 @@ bool MQTTPublish(std::string _key, std::string _content, int qos, bool retained_
|
|||||||
_content.append("..");
|
_content.append("..");
|
||||||
}
|
}
|
||||||
|
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Published topic: " + _key + ", content: " + _content + " (msg_id=" + std::to_string(msg_id) + ")");
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Published topic: " + _key + ", content: " + _content + " (msg_id=" + std::to_string(msg_id) + ")");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publish skipped. Client not initalized or not connected. (topic: " + _key + ")");
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Publish skipped. Client not initalized or not connected. (topic: " + _key + ")");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,38 +113,38 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
|||||||
case MQTT_EVENT_DISCONNECTED:
|
case MQTT_EVENT_DISCONNECTED:
|
||||||
mqtt_connected = false;
|
mqtt_connected = false;
|
||||||
MQTTReconnectCnt++;
|
MQTTReconnectCnt++;
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected, trying to reconnect");
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "Disconnected, trying to reconnect");
|
||||||
|
|
||||||
if (MQTTReconnectCnt >= 5) {
|
if (MQTTReconnectCnt >= 5) {
|
||||||
MQTTReconnectCnt = 0;
|
MQTTReconnectCnt = 0;
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Disconnected, multiple reconnect attempts failed, still retrying...");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Disconnected, multiple reconnect attempts failed, still retrying...");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_SUBSCRIBED:
|
case MQTT_EVENT_SUBSCRIBED:
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_UNSUBSCRIBED:
|
case MQTT_EVENT_UNSUBSCRIBED:
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_PUBLISHED:
|
case MQTT_EVENT_PUBLISHED:
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_DATA:
|
case MQTT_EVENT_DATA:
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_DATA");
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_DATA");
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "TOPIC=%.*s", event->topic_len, event->topic);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "TOPIC=%.*s", event->topic_len, event->topic);
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "DATA=%.*s", event->data_len, event->data);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "DATA=%.*s", event->data_len, event->data);
|
||||||
topic.assign(event->topic, event->topic_len);
|
topic.assign(event->topic, event->topic_len);
|
||||||
if (subscribeFunktionMap != NULL) {
|
if (subscribeFunktionMap != NULL) {
|
||||||
if (subscribeFunktionMap->find(topic) != subscribeFunktionMap->end()) {
|
if (subscribeFunktionMap->find(topic) != subscribeFunktionMap->end()) {
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "call subcribe function for topic %s", topic.c_str());
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "call subcribe function for topic %s", topic.c_str());
|
||||||
(*subscribeFunktionMap)[topic](topic, event->data, event->data_len);
|
(*subscribeFunktionMap)[topic](topic, event->data, event->data_len);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "no handler available\r\n");
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "no handler available\r\n");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -154,44 +154,44 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
|||||||
// The server does not support the level of the MQTT protocol requested by the client
|
// The server does not support the level of the MQTT protocol requested by the client
|
||||||
// NOTE: Only protocol 3.1.1 is supported (refer to setting in sdkconfig)
|
// NOTE: Only protocol 3.1.1 is supported (refer to setting in sdkconfig)
|
||||||
if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_PROTOCOL) {
|
if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_PROTOCOL) {
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, unacceptable protocol version (0x01)");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, unacceptable protocol version (0x01)");
|
||||||
}
|
}
|
||||||
// The client identifier is correct UTF-8 but not allowed by the server
|
// The client identifier is correct UTF-8 but not allowed by the server
|
||||||
// e.g. clientID empty (cannot be the case -> default set in firmware)
|
// e.g. clientID empty (cannot be the case -> default set in firmware)
|
||||||
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_ID_REJECTED) {
|
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_ID_REJECTED) {
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, identifier rejected (0x02)");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, identifier rejected (0x02)");
|
||||||
}
|
}
|
||||||
// The network connection has been made but the MQTT service is unavailable
|
// The network connection has been made but the MQTT service is unavailable
|
||||||
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_SERVER_UNAVAILABLE) {
|
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_SERVER_UNAVAILABLE) {
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, Server unavailable (0x03)");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, Server unavailable (0x03)");
|
||||||
}
|
}
|
||||||
// The data in the user name or password is malformed
|
// The data in the user name or password is malformed
|
||||||
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_BAD_USERNAME) {
|
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_BAD_USERNAME) {
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, malformed data in username or password (0x04)");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, malformed data in username or password (0x04)");
|
||||||
}
|
}
|
||||||
// The client is not authorized to connect
|
// The client is not authorized to connect
|
||||||
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_NOT_AUTHORIZED) {
|
else if (event->error_handle->connect_return_code == MQTT_CONNECTION_REFUSE_NOT_AUTHORIZED) {
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, not authorized. Check username/password (0x05)");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection refused, not authorized. Check username/password (0x05)");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Other event id:" + event->error_handle->connect_return_code);
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Other event id:" + event->error_handle->connect_return_code);
|
||||||
LogFile.WriteToFile(ESP_LOG_ERR, TAG, "Other event id:%d", event->error_handle->connect_return_code);
|
Logfile.WriteToFile(ESP_LOG_ERR, TAG, "Other event id:%d", event->error_handle->connect_return_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_DETAIL_ON
|
#ifdef DEBUG_DETAIL_ON
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_ERROR - esp_mqtt_error_codes:");
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTT_EVENT_ERROR - esp_mqtt_error_codes:");
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "error_type:%d", event->error_handle->error_type);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "error_type:%d", event->error_handle->error_type);
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "connect_return_code:%d", event->error_handle->connect_return_code);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "connect_return_code:%d", event->error_handle->connect_return_code);
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_transport_sock_errno:%d", event->error_handle->esp_transport_sock_errno);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_transport_sock_errno:%d", event->error_handle->esp_transport_sock_errno);
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_tls_last_esp_err:%d", event->error_handle->esp_tls_last_esp_err);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_tls_last_esp_err:%d", event->error_handle->esp_tls_last_esp_err);
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_tls_stack_err:%d", event->error_handle->esp_tls_stack_err);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_tls_stack_err:%d", event->error_handle->esp_tls_stack_err);
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_tls_cert_verify_flags:%d", event->error_handle->esp_tls_cert_verify_flags);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "esp_tls_cert_verify_flags:%d", event->error_handle->esp_tls_cert_verify_flags);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Other event id:%d", event->event_id);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Other event id:%d", event->event_id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -199,7 +199,7 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) {
|
|||||||
|
|
||||||
|
|
||||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Event dispatched from event loop base=%s, event_id=%d", base, (int)event_id);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Event dispatched from event loop base=%s, event_id=%d", base, (int)event_id);
|
||||||
mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data);
|
mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +210,7 @@ bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
|
|||||||
int _keepalive, bool _SetRetainFlag, void *_callbackOnConnected) {
|
int _keepalive, bool _SetRetainFlag, void *_callbackOnConnected) {
|
||||||
if ((_mqttURI.length() == 0) || (_maintopic.length() == 0) || (_clientid.length() == 0))
|
if ((_mqttURI.length() == 0) || (_maintopic.length() == 0) || (_clientid.length() == 0))
|
||||||
{
|
{
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init aborted! Config error (URI, MainTopic or ClientID missing)");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Init aborted! Config error (URI, MainTopic or ClientID missing)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,19 +228,19 @@ bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
|
|||||||
std::ifstream cert_ifs(_clientcertfilename);
|
std::ifstream cert_ifs(_clientcertfilename);
|
||||||
std::string cert_content((std::istreambuf_iterator<char>(cert_ifs)), (std::istreambuf_iterator<char>()));
|
std::string cert_content((std::istreambuf_iterator<char>(cert_ifs)), (std::istreambuf_iterator<char>()));
|
||||||
clientCert = cert_content;
|
clientCert = cert_content;
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using clientCert: " + _clientcertfilename);
|
Logfile.WriteToFile(ESP_LOG_INFO, TAG, "using clientCert: " + _clientcertfilename);
|
||||||
|
|
||||||
std::ifstream key_ifs(_clientkeyfilename);
|
std::ifstream key_ifs(_clientkeyfilename);
|
||||||
std::string key_content((std::istreambuf_iterator<char>(key_ifs)), (std::istreambuf_iterator<char>()));
|
std::string key_content((std::istreambuf_iterator<char>(key_ifs)), (std::istreambuf_iterator<char>()));
|
||||||
clientKey = key_content;
|
clientKey = key_content;
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using clientKey: " + _clientkeyfilename);
|
Logfile.WriteToFile(ESP_LOG_INFO, TAG, "using clientKey: " + _clientkeyfilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_cacertfilename.length() ){
|
if (_cacertfilename.length() ){
|
||||||
std::ifstream ifs(_cacertfilename);
|
std::ifstream ifs(_cacertfilename);
|
||||||
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||||
caCert = content;
|
caCert = content;
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "using caCert: " + _cacertfilename);
|
Logfile.WriteToFile(ESP_LOG_INFO, TAG, "using caCert: " + _cacertfilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_user.length() && _password.length()){
|
if (_user.length() && _password.length()){
|
||||||
@@ -249,10 +249,10 @@ bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __HIDE_PASSWORD
|
#ifdef __HIDE_PASSWORD
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "URI: " + uri + ", clientname: " + client_id + ", user: " + user + ", password: XXXXXXXX, maintopic: "
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "URI: " + uri + ", clientname: " + client_id + ", user: " + user + ", password: XXXXXXXX, maintopic: "
|
||||||
+ maintopic + ", last-will-topic: " + lwt_topic + ", keepAlive: " + std::to_string(keepalive) + ", RetainFlag: " + std::to_string(SetRetainFlag));
|
+ maintopic + ", last-will-topic: " + lwt_topic + ", keepAlive: " + std::to_string(keepalive) + ", RetainFlag: " + std::to_string(SetRetainFlag));
|
||||||
#else
|
#else
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "URI: " + uri + ", clientname: " + client_id + ", user: " + user + ", password: " + password + ", maintopic: "
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "URI: " + uri + ", clientname: " + client_id + ", user: " + user + ", password: " + password + ", maintopic: "
|
||||||
+ maintopic + ", last-will-topic: " + lwt_topic + ", keepAlive: " + std::to_string(keepalive) + ", RetainFlag: " + std::to_string(SetRetainFlag));
|
+ maintopic + ", last-will-topic: " + lwt_topic + ", keepAlive: " + std::to_string(keepalive) + ", RetainFlag: " + std::to_string(SetRetainFlag));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -269,16 +269,16 @@ int MQTT_Init() {
|
|||||||
if (mqtt_configOK) {
|
if (mqtt_configOK) {
|
||||||
mqtt_enabled = true;
|
mqtt_enabled = true;
|
||||||
} else {
|
} else {
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init called, but client is not yet configured.");
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init called, but client is not yet configured.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!getWIFIisConnected()) {
|
if (!getWIFIisConnected()) {
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init called, but WIFI is not yet connected.");
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init called, but WIFI is not yet connected.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Init");
|
Logfile.WriteToFile(ESP_LOG_INFO, TAG, "Init");
|
||||||
MQTTdestroy_client(false);
|
MQTTdestroy_client(false);
|
||||||
|
|
||||||
esp_mqtt_client_config_t mqtt_cfg = { };
|
esp_mqtt_client_config_t mqtt_cfg = { };
|
||||||
@@ -316,7 +316,7 @@ int MQTT_Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_DETAIL_ON
|
#ifdef DEBUG_DETAIL_ON
|
||||||
LogFile.WriteHeapInfo("MQTT Client Init");
|
Logfile.WriteHeapInfo("MQTT Client Init");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
client = esp_mqtt_client_init(&mqtt_cfg);
|
client = esp_mqtt_client_init(&mqtt_cfg);
|
||||||
@@ -325,30 +325,30 @@ int MQTT_Init() {
|
|||||||
esp_err_t ret = esp_mqtt_client_register_event(client, esp_mqtt_ID, mqtt_event_handler, client);
|
esp_err_t ret = esp_mqtt_client_register_event(client, esp_mqtt_ID, mqtt_event_handler, client);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Could not register event (ret=" + std::to_string(ret) + ")!");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Could not register event (ret=" + std::to_string(ret) + ")!");
|
||||||
mqtt_initialized = false;
|
mqtt_initialized = false;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_DETAIL_ON
|
#ifdef DEBUG_DETAIL_ON
|
||||||
LogFile.WriteHeapInfo("MQTT Client Start");
|
Logfile.WriteHeapInfo("MQTT Client Start");
|
||||||
#endif
|
#endif
|
||||||
ret = esp_mqtt_client_start(client);
|
ret = esp_mqtt_client_start(client);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Client start failed (retval=" + std::to_string(ret) + ")!");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Client start failed (retval=" + std::to_string(ret) + ")!");
|
||||||
mqtt_initialized = false;
|
mqtt_initialized = false;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Client started, waiting for established connection...");
|
Logfile.WriteToFile(ESP_LOG_INFO, TAG, "Client started, waiting for established connection...");
|
||||||
mqtt_initialized = true;
|
mqtt_initialized = true;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init failed, no handle created!");
|
Logfile.WriteToFile(ESP_LOG_ERROR, TAG, "Init failed, no handle created!");
|
||||||
mqtt_initialized = false;
|
mqtt_initialized = false;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -386,13 +386,13 @@ bool getMQTTisConnected() {
|
|||||||
|
|
||||||
bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
|
bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
|
||||||
{
|
{
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
||||||
|
|
||||||
if (_data_len > 0) {
|
if (_data_len > 0) {
|
||||||
MQTTCtrlFlowStart(_topic);
|
MQTTCtrlFlowStart(_topic);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "handler_flow_start: handler called, but no data");
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "handler_flow_start: handler called, but no data");
|
||||||
}
|
}
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -401,7 +401,7 @@ bool mqtt_handler_flow_start(std::string _topic, char* _data, int _data_len)
|
|||||||
|
|
||||||
bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
||||||
{
|
{
|
||||||
//ogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
//LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Handler called: topic %s, data %.*s", _topic.c_str(), _data_len, _data);
|
||||||
//example: {"numbersname": "main", "value": 12345.1234567}
|
//example: {"numbersname": "main", "value": 12345.1234567}
|
||||||
|
|
||||||
if (_data_len > 0) { // Check if data length > 0
|
if (_data_len > 0) { // Check if data length > 0
|
||||||
@@ -411,7 +411,7 @@ bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
|||||||
|
|
||||||
if (cJSON_IsString(numbersname) && (numbersname->valuestring != NULL)) { // Check if numbersname is valid
|
if (cJSON_IsString(numbersname) && (numbersname->valuestring != NULL)) { // Check if numbersname is valid
|
||||||
if (cJSON_IsNumber(value)) { // Check if value is a number
|
if (cJSON_IsNumber(value)) { // Check if value is a number
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handler_set_prevalue called: numbersname: " + std::string(numbersname->valuestring) +
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "handler_set_prevalue called: numbersname: " + std::string(numbersname->valuestring) +
|
||||||
", value: " + std::to_string(value->valuedouble));
|
", value: " + std::to_string(value->valuedouble));
|
||||||
if (flowctrl.UpdatePrevalue(std::to_string(value->valuedouble), std::string(numbersname->valuestring), true)) {
|
if (flowctrl.UpdatePrevalue(std::to_string(value->valuedouble), std::string(numbersname->valuestring), true)) {
|
||||||
cJSON_Delete(jsonData);
|
cJSON_Delete(jsonData);
|
||||||
@@ -419,16 +419,16 @@ bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "handler_set_prevalue: value not a valid number (\"value\": 12345.12345)");
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "handler_set_prevalue: value not a valid number (\"value\": 12345.12345)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "handler_set_prevalue: numbersname not a valid string (\"numbersname\": \"main\")");
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "handler_set_prevalue: numbersname not a valid string (\"numbersname\": \"main\")");
|
||||||
}
|
}
|
||||||
cJSON_Delete(jsonData);
|
cJSON_Delete(jsonData);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "handler_set_prevalue: handler called, but no data received");
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "handler_set_prevalue: handler called, but no data received");
|
||||||
}
|
}
|
||||||
|
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
@@ -437,12 +437,12 @@ bool mqtt_handler_set_prevalue(std::string _topic, char* _data, int _data_len)
|
|||||||
|
|
||||||
void MQTTconnected(){
|
void MQTTconnected(){
|
||||||
if (mqtt_connected) {
|
if (mqtt_connected) {
|
||||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connected to broker");
|
Logfile.WriteToFile(ESP_LOG_INFO, TAG, "Connected to broker");
|
||||||
|
|
||||||
if (connectFunktionMap != NULL) {
|
if (connectFunktionMap != NULL) {
|
||||||
for(std::map<std::string, std::function<void()>>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) {
|
for(std::map<std::string, std::function<void()>>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) {
|
||||||
it->second();
|
it->second();
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "call connect function %s", it->first.c_str());
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "call connect function %s", it->first.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,7 +458,7 @@ void MQTTconnected(){
|
|||||||
if (subscribeFunktionMap != NULL) {
|
if (subscribeFunktionMap != NULL) {
|
||||||
for(std::map<std::string, std::function<bool(std::string, char*, int)>>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) {
|
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);
|
int msg_id = esp_mqtt_client_subscribe(client, it->first.c_str(), 0);
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id));
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -471,13 +471,13 @@ void MQTTconnected(){
|
|||||||
|
|
||||||
|
|
||||||
void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTTregisteronnectFunction %s\r\n", name.c_str());
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "MQTTregisteronnectFunction %s\r\n", name.c_str());
|
||||||
if (connectFunktionMap == NULL) {
|
if (connectFunktionMap == NULL) {
|
||||||
connectFunktionMap = new std::map<std::string, std::function<void()>>();
|
connectFunktionMap = new std::map<std::string, std::function<void()>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((*connectFunktionMap)[name] != NULL) {
|
if ((*connectFunktionMap)[name] != NULL) {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "connect function %s already registred", name.c_str());
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "connect function %s already registred", name.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,7 +490,7 @@ void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
|
|||||||
|
|
||||||
|
|
||||||
void MQTTunregisterConnectFunction(std::string name){
|
void MQTTunregisterConnectFunction(std::string name){
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "unregisterConnnectFunction %s\r\n", name.c_str());
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "unregisterConnnectFunction %s\r\n", name.c_str());
|
||||||
if ((connectFunktionMap != NULL) && (connectFunktionMap->find(name) != connectFunktionMap->end())) {
|
if ((connectFunktionMap != NULL) && (connectFunktionMap->find(name) != connectFunktionMap->end())) {
|
||||||
connectFunktionMap->erase(name);
|
connectFunktionMap->erase(name);
|
||||||
}
|
}
|
||||||
@@ -498,13 +498,13 @@ void MQTTunregisterConnectFunction(std::string name){
|
|||||||
|
|
||||||
|
|
||||||
void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::string, char*, int)> func){
|
void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::string, char*, int)> func){
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "registerSubscribeFunction %s", topic.c_str());
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "registerSubscribeFunction %s", topic.c_str());
|
||||||
if (subscribeFunktionMap == NULL) {
|
if (subscribeFunktionMap == NULL) {
|
||||||
subscribeFunktionMap = new std::map<std::string, std::function<bool(std::string, char*, int)>>();
|
subscribeFunktionMap = new std::map<std::string, std::function<bool(std::string, char*, int)>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((*subscribeFunktionMap)[topic] != NULL) {
|
if ((*subscribeFunktionMap)[topic] != NULL) {
|
||||||
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "topic %s already registered for subscription", topic.c_str());
|
Logfile.WriteToFile(ESP_LOG_WARN, TAG, "topic %s already registered for subscription", topic.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -517,7 +517,7 @@ void MQTTdestroySubscribeFunction(){
|
|||||||
if (mqtt_connected) {
|
if (mqtt_connected) {
|
||||||
for(std::map<std::string, std::function<bool(std::string, char*, int)>>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) {
|
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_unsubscribe(client, it->first.c_str());
|
int msg_id = esp_mqtt_client_unsubscribe(client, it->first.c_str());
|
||||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "topic %s unsubscribe successful, msg_id=%d", it->first.c_str(), msg_id);
|
Logfile.WriteToFile(ESP_LOG_DEBUG, TAG, "topic %s unsubscribe successful, msg_id=%d", it->first.c_str(), msg_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user