interface_mqtt.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "interface_mqtt.h"
  2. //#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  3. #include "esp_log.h"
  4. #include "mqtt_client.h"
  5. #include "ClassLogFile.h"
  6. #define __HIDE_PASSWORD
  7. static const char *TAG_INTERFACEMQTT = "interface_mqtt";
  8. std::map<std::string, std::function<void()>>* connectFunktionMap = NULL;
  9. std::map<std::string, std::function<bool(std::string, char*, int)>>* subscribeFunktionMap = NULL;
  10. // #define CONFIG_BROKER_URL "mqtt://192.168.178.43:1883"
  11. esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
  12. // ESP_EVENT_ANY_ID
  13. bool mqtt_connected = false;
  14. esp_mqtt_client_handle_t client = NULL;
  15. bool MQTTPublish(std::string _key, std::string _content, int retained_flag){
  16. int msg_id;
  17. std::string zw;
  18. msg_id = esp_mqtt_client_publish(client, _key.c_str(), _content.c_str(), 0, 1, retained_flag);
  19. if (msg_id < 0) {
  20. LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Failed to publish topic '" + _key + "'!");
  21. return false;
  22. }
  23. zw = "MQTT - Published topic: " + _key + ", content: " + _content + " (msg_id=" + std::to_string(msg_id) + ")";
  24. LogFile.WriteToFile(ESP_LOG_DEBUG, zw);
  25. return true;
  26. }
  27. static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
  28. {
  29. int msg_id;
  30. std::string topic = "";
  31. switch (event->event_id) {
  32. case MQTT_EVENT_BEFORE_CONNECT:
  33. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_BEFORE_CONNECT");
  34. break;
  35. case MQTT_EVENT_CONNECTED:
  36. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_CONNECTED");
  37. mqtt_connected = true;
  38. MQTTconnected();
  39. break;
  40. case MQTT_EVENT_DISCONNECTED:
  41. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_DISCONNECTED");
  42. esp_mqtt_client_reconnect(client);
  43. break;
  44. case MQTT_EVENT_SUBSCRIBED:
  45. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
  46. msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
  47. ESP_LOGI(TAG_INTERFACEMQTT, "sent publish successful, msg_id=%d", msg_id);
  48. break;
  49. case MQTT_EVENT_UNSUBSCRIBED:
  50. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
  51. break;
  52. case MQTT_EVENT_PUBLISHED:
  53. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
  54. break;
  55. case MQTT_EVENT_DATA:
  56. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_DATA");
  57. ESP_LOGI(TAG_INTERFACEMQTT, "TOPIC=%.*s\r\n", event->topic_len, event->topic);
  58. ESP_LOGI(TAG_INTERFACEMQTT, "DATA=%.*s\r\n", event->data_len, event->data);
  59. topic.assign(event->topic, event->topic_len);
  60. if (subscribeFunktionMap != NULL) {
  61. if (subscribeFunktionMap->find(topic) != subscribeFunktionMap->end()) {
  62. ESP_LOGD(TAG_INTERFACEMQTT, "call handler function\r\n");
  63. (*subscribeFunktionMap)[topic](topic, event->data, event->data_len);
  64. }
  65. } else {
  66. ESP_LOGW(TAG_INTERFACEMQTT, "no handler available\r\n");
  67. }
  68. break;
  69. case MQTT_EVENT_ERROR:
  70. ESP_LOGI(TAG_INTERFACEMQTT, "MQTT_EVENT_ERROR");
  71. break;
  72. default:
  73. ESP_LOGI(TAG_INTERFACEMQTT, "Other event id:%d", event->event_id);
  74. break;
  75. }
  76. return ESP_OK;
  77. }
  78. static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
  79. ESP_LOGD(TAG_INTERFACEMQTT, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
  80. mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data);
  81. }
  82. bool MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _LWTContext, int _keepalive){
  83. std::string _zwmessage = "connection lost";
  84. int _lzw = _zwmessage.length();
  85. esp_mqtt_client_config_t mqtt_cfg = {
  86. .uri = _mqttURI.c_str(),
  87. .client_id = _clientid.c_str(),
  88. .lwt_topic = _LWTContext.c_str(),
  89. .lwt_msg = _zwmessage.c_str(),
  90. .lwt_retain = 1,
  91. .lwt_msg_len = _lzw,
  92. .keepalive = _keepalive
  93. };
  94. LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init (client ID: " + _clientid + ")");
  95. if (_user.length() && _password.length()){
  96. mqtt_cfg.username = _user.c_str();
  97. mqtt_cfg.password = _password.c_str();
  98. #ifdef __HIDE_PASSWORD
  99. ESP_LOGI(TAG_INTERFACEMQTT, "Connect to MQTT: %s, XXXXXXXX", mqtt_cfg.username);
  100. #else
  101. ESP_LOGI(TAG_INTERFACEMQTT, "Connect to MQTT: %s, %s", mqtt_cfg.username, mqtt_cfg.password);
  102. #endif
  103. };
  104. MQTTdestroy();
  105. client = esp_mqtt_client_init(&mqtt_cfg);
  106. if (client)
  107. {
  108. if (esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client) != ESP_OK)
  109. {
  110. LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not register event!");
  111. return false;
  112. }
  113. if (esp_mqtt_client_start(client) != ESP_OK)
  114. {
  115. LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not start client!");
  116. return false;
  117. }
  118. /* if(!MQTTPublish(_LWTContext, "", 1))
  119. {
  120. LogFile.WriteToFile("MQTT - Could not publish LWT!");
  121. return false;
  122. }*/
  123. }
  124. else
  125. {
  126. LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not Init client!");
  127. return false;
  128. }
  129. LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init successful");
  130. return true;
  131. }
  132. void MQTTdestroy() {
  133. if (client != NULL) {
  134. esp_mqtt_client_stop(client);
  135. esp_mqtt_client_destroy(client);
  136. }
  137. }
  138. bool MQTTisConnected() {
  139. return mqtt_connected;
  140. }
  141. void MQTTregisterConnectFunction(std::string name, std::function<void()> func){
  142. ESP_LOGD(TAG_INTERFACEMQTT, "MQTTregisteronnectFunction %s\r\n", name.c_str());
  143. if (connectFunktionMap == NULL) {
  144. connectFunktionMap = new std::map<std::string, std::function<void()>>();
  145. }
  146. if ((*connectFunktionMap)[name] != NULL) {
  147. ESP_LOGW(TAG_INTERFACEMQTT, "connect function %s already registred", name.c_str());
  148. return;
  149. }
  150. (*connectFunktionMap)[name] = func;
  151. if (mqtt_connected) {
  152. func();
  153. }
  154. }
  155. void MQTTunregisterConnectFunction(std::string name){
  156. ESP_LOGD(TAG_INTERFACEMQTT, "MQTTregisteronnectFunction %s\r\n", name.c_str());
  157. if ((connectFunktionMap != NULL) && (connectFunktionMap->find(name) != connectFunktionMap->end())) {
  158. connectFunktionMap->erase(name);
  159. }
  160. }
  161. void MQTTregisterSubscribeFunction(std::string topic, std::function<bool(std::string, char*, int)> func){
  162. ESP_LOGD(TAG_INTERFACEMQTT, "MQTTregisterSubscribeFunction %s\r\n", topic.c_str());
  163. if (subscribeFunktionMap == NULL) {
  164. subscribeFunktionMap = new std::map<std::string, std::function<bool(std::string, char*, int)>>();
  165. }
  166. if ((*subscribeFunktionMap)[topic] != NULL) {
  167. ESP_LOGW(TAG_INTERFACEMQTT, "topic %s already registred for subscription", topic.c_str());
  168. return;
  169. }
  170. (*subscribeFunktionMap)[topic] = func;
  171. if (mqtt_connected) {
  172. int msg_id = esp_mqtt_client_subscribe(client, topic.c_str(), 0);
  173. ESP_LOGD(TAG_INTERFACEMQTT, "topic %s subscribe successful, msg_id=%d", topic.c_str(), msg_id);
  174. }
  175. }
  176. void MQTTconnected(){
  177. if (mqtt_connected) {
  178. LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Connected");
  179. if (connectFunktionMap != NULL) {
  180. for(std::map<std::string, std::function<void()>>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) {
  181. it->second();
  182. ESP_LOGD(TAG_INTERFACEMQTT, "call connect function %s", it->first.c_str());
  183. }
  184. }
  185. if (subscribeFunktionMap != NULL) {
  186. for(std::map<std::string, std::function<bool(std::string, char*, int)>>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) {
  187. int msg_id = esp_mqtt_client_subscribe(client, it->first.c_str(), 0);
  188. LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id));
  189. }
  190. }
  191. }
  192. }
  193. void MQTTdestroySubscribeFunction(){
  194. if (subscribeFunktionMap != NULL) {
  195. if (mqtt_connected) {
  196. for(std::map<std::string, std::function<bool(std::string, char*, int)>>::iterator it = subscribeFunktionMap->begin(); it != subscribeFunktionMap->end(); ++it) {
  197. int msg_id = esp_mqtt_client_unsubscribe(client, it->first.c_str());
  198. ESP_LOGI(TAG_INTERFACEMQTT, "topic %s unsubscribe successful, msg_id=%d", it->first.c_str(), msg_id);
  199. }
  200. }
  201. subscribeFunktionMap->clear();
  202. delete subscribeFunktionMap;
  203. subscribeFunktionMap = NULL;
  204. }
  205. }