mqtt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* MQTT (over TCP) Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "esp_wifi.h"
  12. #include "esp_system.h"
  13. #include "nvs_flash.h"
  14. #include "esp_event.h"
  15. #include "esp_netif.h"
  16. //#include "protocol_examples_common.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "freertos/semphr.h"
  20. #include "freertos/queue.h"
  21. #include "lwip/sockets.h"
  22. #include "lwip/dns.h"
  23. #include "lwip/netdb.h"
  24. #include "esp_log.h"
  25. #include "mqtt_client.h"
  26. #include "wifi.h"
  27. #include "receiver.h"
  28. #include "ir_transmit.h"
  29. #include "toshiba_ir.h"
  30. static const char *TAG = "MQTT";
  31. #ifdef MQTT_ENABLED
  32. static const char *TAG_RX = "MQTT_RX";
  33. static esp_mqtt_client_handle_t client;
  34. static bool connected = false;
  35. int getJsonProp(const char *str,const char *prop) {
  36. int propValue = -1;
  37. const size_t propLen = strlen(prop);
  38. char sStr[propLen+4];
  39. strcpy(&sStr[1],prop);
  40. sStr[0] = sStr[propLen+1] = '\"';
  41. sStr[propLen+2] = ':';
  42. sStr[propLen+3] = '\0';
  43. char *ptr = strstr(str,sStr);
  44. if( ptr != NULL ) {
  45. ptr += strlen(sStr);
  46. int retVal = sscanf(ptr,"%d",&propValue);
  47. if( retVal == 1 ) {
  48. return propValue;
  49. }
  50. }
  51. return -1;
  52. }
  53. void remove_spaces(char* s) {
  54. char* d = s;
  55. do {
  56. while (*d == ' ') {
  57. ++d;
  58. }
  59. } while ( (*s++ = *d++) );
  60. }
  61. static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
  62. {
  63. esp_mqtt_client_handle_t client = event->client;
  64. int msg_id;
  65. // your_context_t *context = event->context;
  66. switch (event->event_id) {
  67. case MQTT_EVENT_CONNECTED:
  68. connected = true;
  69. ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
  70. msg_id = esp_mqtt_client_subscribe(client, "hall/ac/ir_cmd_tx", 0);
  71. ESP_LOGI(TAG, "Sent subscribe successful, msg_id=%d", msg_id);
  72. break;
  73. case MQTT_EVENT_DISCONNECTED:
  74. connected = false;
  75. ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
  76. break;
  77. case MQTT_EVENT_SUBSCRIBED:
  78. ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
  79. break;
  80. case MQTT_EVENT_UNSUBSCRIBED:
  81. ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
  82. break;
  83. case MQTT_EVENT_PUBLISHED:
  84. ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
  85. break;
  86. case MQTT_EVENT_DATA:
  87. ESP_LOGI(TAG, "MQTT_EVENT_DATA Topic: <%.*s>", event->topic_len, event->topic);
  88. #ifdef IR_TRANSMIT
  89. //printf("DATA=%.*s\r\n", event->data_len, event->data);
  90. // {"fan":0,"power":1,"temp":245}
  91. if( strncmp(event->topic,"hall/ac/ir_cmd_tx",event->topic_len) == 0 ) {
  92. int v1,v2,v3;
  93. v1=v2=v3=0;
  94. remove_spaces(event->data);
  95. ESP_LOGI(TAG_RX, "Temp: %d", v1 = getJsonProp(event->data, "temp"));
  96. ESP_LOGI(TAG_RX, "Fan: %d", v2 = getJsonProp(event->data, "fan"));
  97. ESP_LOGI(TAG_RX, "Power: %d", v3 = getJsonProp(event->data, "power"));
  98. if( v1 < 160 || v1 > 300 ) { ESP_LOGE(TAG_RX,"Error in temp data %d",v1); return 0; }
  99. //const uint8_t addExtraTemp = (v1%10 == 5) ? 1 : 0;
  100. //const uint8_t tempData = ((v1/10) - 16) * 2 + addExtraTemp;
  101. //ESP_LOGI(TAG_RX,"TempData: %d", tempData);
  102. // Fan: 0-5 -> 10, 3-7
  103. if( v2 < 0 || v2 > 5 ) { ESP_LOGE(TAG_RX,"Error in fan data %d",v2); return 0; }
  104. //const uint8_t fanData = (v2==0)? 10 : v2+2;
  105. if( v3 != 0 && v3 != 1 ) { ESP_LOGE(TAG_RX,"Error in power data %d",v3); return 0; }
  106. //const uint8_t powerData = v3;
  107. IR_TX_DATA txData;
  108. txData.temp = v1;
  109. txData.fan = v2;
  110. txData.power = v3;
  111. xQueueSend( toshibaTxQueue, &txData, 0 );
  112. #endif
  113. }
  114. break;
  115. case MQTT_EVENT_ERROR:
  116. ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
  117. break;
  118. default:
  119. ESP_LOGI(TAG, "Other event id:%d", event->event_id);
  120. break;
  121. }
  122. return ESP_OK;
  123. }
  124. static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
  125. ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
  126. mqtt_event_handler_cb(event_data);
  127. }
  128. void mqttTask(void *pvParameters) {
  129. ESP_LOGI("MQTT", "mqttTask starting. Core:%d\n",xPortGetCoreID());
  130. #ifdef WIFI_ENABLED
  131. // Wait for tcpip-comm
  132. while( commIsUpAndRunning == false ) vTaskDelay(10000 / portTICK_PERIOD_MS);
  133. #endif
  134. esp_mqtt_client_config_t mqtt_cfg = {
  135. .uri = "mqtt://192.168.1.110:1883",
  136. .password = CONFIG_ESP_MQTT_PASSWORD,
  137. .username = CONFIG_ESP_MQTT_UNAME
  138. };
  139. client = esp_mqtt_client_init(&mqtt_cfg);
  140. esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
  141. esp_mqtt_client_start(client);
  142. ESP_LOGI("MQTT", "mqttTask() started.");
  143. vTaskDelete(NULL);
  144. }
  145. void mqtt_init(void)
  146. {
  147. xTaskCreatePinnedToCore(mqttTask, "MQTT-Task", 1024*10, NULL, 2, NULL,0);
  148. }
  149. void sendMQTTMessage(const char * topic, const char * data) {
  150. if( connected ) {
  151. int msg_id;
  152. #ifdef MQTT_DEBUG
  153. char topic_debug_str[100];
  154. sprintf(topic_debug_str,"debug_ir_env/%s",topic);
  155. msg_id = esp_mqtt_client_publish(client, topic_debug_str, data, 0, 1, 0);
  156. #else
  157. msg_id = esp_mqtt_client_publish(client, topic, data, 0, 1, 0);
  158. #endif
  159. ESP_LOGI(TAG, "sent publish successful, msg_id=%d %s %s", msg_id, topic, data);
  160. }
  161. else {
  162. ESP_LOGI(TAG, "Not connected to MQTT");
  163. }
  164. }
  165. #else
  166. void sendMQTTMessage(const char * topic, const char * data) {
  167. ESP_LOGI(TAG, "sent publish FAKE, msg_id=- %s %s", topic, data);
  168. }
  169. #endif