123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /* MQTT (over TCP) Example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <stdio.h>
- #include <stdint.h>
- #include <stddef.h>
- #include <string.h>
- #include "esp_wifi.h"
- #include "esp_system.h"
- #include "nvs_flash.h"
- #include "esp_event.h"
- #include "esp_netif.h"
- //#include "protocol_examples_common.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/semphr.h"
- #include "freertos/queue.h"
- #include "lwip/sockets.h"
- #include "lwip/dns.h"
- #include "lwip/netdb.h"
- #include "esp_log.h"
- #include "mqtt_client.h"
- #include "wifi.h"
- #include "receiver.h"
- #include "ir_transmit.h"
- #include "toshiba_ir.h"
- static const char *TAG = "MQTT";
- #ifdef MQTT_ENABLED
- static const char *TAG_RX = "MQTT_RX";
- static esp_mqtt_client_handle_t client;
- static bool connected = false;
- int getJsonProp(const char *str,const char *prop) {
- int propValue = -1;
- const size_t propLen = strlen(prop);
- char sStr[propLen+4];
- strcpy(&sStr[1],prop);
- sStr[0] = sStr[propLen+1] = '\"';
- sStr[propLen+2] = ':';
- sStr[propLen+3] = '\0';
- char *ptr = strstr(str,sStr);
- if( ptr != NULL ) {
- ptr += strlen(sStr);
- int retVal = sscanf(ptr,"%d",&propValue);
- if( retVal == 1 ) {
- return propValue;
- }
- }
- return -1;
- }
- void remove_spaces(char* s) {
- char* d = s;
- do {
- while (*d == ' ') {
- ++d;
- }
- } while ( (*s++ = *d++) );
- }
- static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
- {
-
- esp_mqtt_client_handle_t client = event->client;
- int msg_id;
- // your_context_t *context = event->context;
- switch (event->event_id) {
- case MQTT_EVENT_CONNECTED:
- connected = true;
- ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
- msg_id = esp_mqtt_client_subscribe(client, "hall/ac/ir_cmd_tx", 0);
- ESP_LOGI(TAG, "Sent subscribe successful, msg_id=%d", msg_id);
- break;
- case MQTT_EVENT_DISCONNECTED:
- connected = false;
- ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
- break;
-
- case MQTT_EVENT_SUBSCRIBED:
- ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
- break;
- case MQTT_EVENT_UNSUBSCRIBED:
- ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
- break;
- case MQTT_EVENT_PUBLISHED:
- ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
- break;
- case MQTT_EVENT_DATA:
- ESP_LOGI(TAG, "MQTT_EVENT_DATA Topic: <%.*s>", event->topic_len, event->topic);
- #ifdef IR_TRANSMIT
- //printf("DATA=%.*s\r\n", event->data_len, event->data);
- // {"fan":0,"power":1,"temp":245}
- if( strncmp(event->topic,"hall/ac/ir_cmd_tx",event->topic_len) == 0 ) {
- int v1,v2,v3;
- v1=v2=v3=0;
- remove_spaces(event->data);
- ESP_LOGI(TAG_RX, "Temp: %d", v1 = getJsonProp(event->data, "temp"));
- ESP_LOGI(TAG_RX, "Fan: %d", v2 = getJsonProp(event->data, "fan"));
- ESP_LOGI(TAG_RX, "Power: %d", v3 = getJsonProp(event->data, "power"));
- if( v1 < 160 || v1 > 300 ) { ESP_LOGE(TAG_RX,"Error in temp data %d",v1); return 0; }
- //const uint8_t addExtraTemp = (v1%10 == 5) ? 1 : 0;
- //const uint8_t tempData = ((v1/10) - 16) * 2 + addExtraTemp;
- //ESP_LOGI(TAG_RX,"TempData: %d", tempData);
- // Fan: 0-5 -> 10, 3-7
- if( v2 < 0 || v2 > 5 ) { ESP_LOGE(TAG_RX,"Error in fan data %d",v2); return 0; }
- //const uint8_t fanData = (v2==0)? 10 : v2+2;
- if( v3 != 0 && v3 != 1 ) { ESP_LOGE(TAG_RX,"Error in power data %d",v3); return 0; }
- //const uint8_t powerData = v3;
- IR_TX_DATA txData;
- txData.temp = v1;
- txData.fan = v2;
- txData.power = v3;
- xQueueSend( toshibaTxQueue, &txData, 0 );
- #endif
- }
- break;
- case MQTT_EVENT_ERROR:
- ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
- break;
- default:
- ESP_LOGI(TAG, "Other event id:%d", event->event_id);
- break;
- }
- return ESP_OK;
- }
- static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
- ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
- mqtt_event_handler_cb(event_data);
- }
- void mqttTask(void *pvParameters) {
- ESP_LOGI("MQTT", "mqttTask starting. Core:%d\n",xPortGetCoreID());
- #ifdef WIFI_ENABLED
- // Wait for tcpip-comm
- while( commIsUpAndRunning == false ) vTaskDelay(10000 / portTICK_PERIOD_MS);
- #endif
- esp_mqtt_client_config_t mqtt_cfg = {
- .uri = "mqtt://192.168.1.110:1883",
- .password = CONFIG_ESP_MQTT_PASSWORD,
- .username = CONFIG_ESP_MQTT_UNAME
- };
- client = esp_mqtt_client_init(&mqtt_cfg);
- esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
- esp_mqtt_client_start(client);
- ESP_LOGI("MQTT", "mqttTask() started.");
- vTaskDelete(NULL);
- }
- void mqtt_init(void)
- {
- xTaskCreatePinnedToCore(mqttTask, "MQTT-Task", 1024*10, NULL, 2, NULL,0);
- }
- void sendMQTTMessage(const char * topic, const char * data) {
- if( connected ) {
- int msg_id;
-
- #ifdef MQTT_DEBUG
- char topic_debug_str[100];
- sprintf(topic_debug_str,"debug_ir_env/%s",topic);
- msg_id = esp_mqtt_client_publish(client, topic_debug_str, data, 0, 1, 0);
- #else
- msg_id = esp_mqtt_client_publish(client, topic, data, 0, 1, 0);
- #endif
- ESP_LOGI(TAG, "sent publish successful, msg_id=%d %s %s", msg_id, topic, data);
- }
- else {
- ESP_LOGI(TAG, "Not connected to MQTT");
- }
- }
- #else
- void sendMQTTMessage(const char * topic, const char * data) {
- ESP_LOGI(TAG, "sent publish FAKE, msg_id=- %s %s", topic, data);
- }
- #endif
|