diff --git a/components/webguiapp b/components/webguiapp index 724c7a4..267a7de 160000 --- a/components/webguiapp +++ b/components/webguiapp @@ -1 +1 @@ -Subproject commit 724c7a437606d27fc41694de95da5523dd581ab5 +Subproject commit 267a7de82ac60a1d0a4e8caed0324e8c5600bfd9 diff --git a/main/MQTTCustom.c b/main/MQTTCustom.c index 6b0b412..97d3627 100644 --- a/main/MQTTCustom.c +++ b/main/MQTTCustom.c @@ -1,4 +1,4 @@ - /*! Copyright 2022 Bogdan Pilyugin +/*! Copyright 2022 Bogdan Pilyugin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,42 +24,69 @@ #include "webguiapp.h" #define TAG "MQTTCustom" +esp_err_t UserMQTTSendExample(int idx) +{ + const char resp[] = { "Got MQTT data in topic USER" }; + char *buf = (char*) malloc(strlen(resp) + 1); + if (buf) + { + memcpy(buf, resp, strlen(resp)); + DATA_SEND_STRUCT DSS; + ComposeTopic(DSS.topic, + GetSysConf()->mqttStation[idx].RootTopic, + "UPLINK", + GetSysConf()->mqttStation[idx].ClientID, + "USER"); + DSS.raw_data_ptr = buf; + DSS.data_length = strlen(resp); + if (xQueueSend(GetMQTTHandlesPool(idx)->mqtt_queue, &DSS, pdMS_TO_TICKS(1000)) == pdPASS) + return ESP_OK; + else + { + free(buf); + return ESP_ERR_TIMEOUT; + } + + } + return ESP_ERR_NO_MEM; +} void UserMQTTEventHndlr(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - esp_mqtt_event_handle_t event = event_data; - esp_mqtt_client_handle_t client = event->client; - mqtt_client_t *ctx = (mqtt_client_t*) event->user_context; - int msg_id; - char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH]; - switch ((esp_mqtt_event_id_t) event_id) - { - case MQTT_EVENT_CONNECTED: - ComposeTopic(topic, - GetSysConf()->mqttStation[ctx->mqtt_index].RootTopic, - "DOWNLINK", - GetSysConf()->mqttStation[ctx->mqtt_index].ClientID, - "USER"); - //Subscribe to the service called "USER" - msg_id = esp_mqtt_client_subscribe(client, (const char*) topic, 0); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + esp_mqtt_event_handle_t event = event_data; + esp_mqtt_client_handle_t client = event->client; + mqtt_client_t *ctx = (mqtt_client_t*) event->user_context; + int msg_id; + char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH]; + switch ((esp_mqtt_event_id_t) event_id) + { + case MQTT_EVENT_CONNECTED: + ComposeTopic(topic, + GetSysConf()->mqttStation[ctx->mqtt_index].RootTopic, + "DOWNLINK", + GetSysConf()->mqttStation[ctx->mqtt_index].ClientID, + "USER"); + //Subscribe to the service called "USER" + msg_id = esp_mqtt_client_subscribe(client, (const char*) topic, 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_DATA: - ComposeTopic(topic, - GetSysConf()->mqttStation[ctx->mqtt_index].RootTopic, - "DOWNLINK", - GetSysConf()->mqttStation[ctx->mqtt_index].ClientID, - "USER"); - if (!memcmp(topic, event->topic, event->topic_len)) - { - //Here data for service called "USER" - ESP_LOGI(TAG, "USER data handler on client %d", ctx->mqtt_index); - } - break; + break; + case MQTT_EVENT_DATA: + ComposeTopic(topic, + GetSysConf()->mqttStation[ctx->mqtt_index].RootTopic, + "DOWNLINK", + GetSysConf()->mqttStation[ctx->mqtt_index].ClientID, + "USER"); + if (!memcmp(topic, event->topic, event->topic_len)) + { + //Here data for service called "USER" + UserMQTTSendExample(ctx->mqtt_index); + ESP_LOGI(TAG, "USER data handler on client %d", ctx->mqtt_index); + } + break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; - } + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } }