mqtt subscribe does not work.

violetforest
Posts: 2
Joined: Wed Jun 26, 2024 7:31 am

mqtt subscribe does not work.

Postby violetforest » Tue Sep 03, 2024 7:50 am

Hello
I am asking a question on the forum because I cannot solve the problem related to mqtt.
The entire architecture is connected to esp32 - lwip(ppp) - sim7600 - aws (greengrass - IoT).
The OS is mac os, the language used is c, and the development environment is vscode + esp-idf.

The current situation is that mqtt connection, aws authentication and connection, shadow publish, and other functions work well, but subscribe does not respond at all.
All other events that occur on the event handler side are activated well, but I think the cause is that the data event does not occur.
(Confirmed events: MQTT_EVENT_CONNECTED, MQTT_EVENT_DISCONNECTED, MQTT_EVENT_SUBSCRIBED, MQTT_EVENT_BEFORE_CONNECT, MQTT_EVENT_ERROR)

I have tried many things, but I have not found a solution so far.
Please help me by attaching the code below.

Thank you.



mqtt_handler.h

Code: Select all

ESP_EVENT_DECLARE_BASE(MQTT_EVENTS);

mqtt_handler.c

Code: Select all

static esp_mqtt_client_handle_t mqtt_client;
static esp_mqtt_event_id_t mqtt_status;

esp_err_t mqtt_initialize(){
    esp_err_t err = ESP_OK;
    ESP_LOGI(TAG, "mqtt start intialization.");
    mqtt_status = MQTT_EVENT_ANY;
    // Load Credentials 
    mqtt_credentials_t* mqtt_creds = mqtt_load_credentials();
    // mqtt Client Configuration
    esp_mqtt_client_config_t mqtt_config = {0,};
    // ESP mqtt module init 
    mqtt_client = esp_mqtt_client_init(&mqtt_config);

   // Register Mqtt Event Handler 
    err = esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_ANY, mqtt_eventhandler, mqtt_client);
    if(err != ESP_OK){
        ESP_LOGE(TAG, "esp_mqtt_client_register_event failed with %d %s", err, esp_err_to_name(err));
    }
    // mqtt connect
    err = esp_mqtt_client_start(mqtt_client);
    if(err != ESP_OK){
        ESP_LOGE(TAG, "esp_mqtt_client_start failed with %d %s", err, esp_err_to_name(err));
    }
    free(mqtt_creds);
    return err;
}


void mqtt_eventhandler(void *arg, esp_event_base_t base, int32_t event_id, void *event_data)
{
//    ESP_LOGI(TAG, "Event dispatched from event loop event_id=%d", event_id);
    esp_mqtt_event_handle_t event = event_data;
    switch ((esp_mqtt_event_id_t)event_id) {
        case MQTT_EVENT_CONNECTED:
            ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
            mqtt_status = MQTT_EVENT_CONNECTED;
            char sub_topic[100];

            // subscribe : odn custom command 
            sprintf(sub_topic, "odn/things/%s/#", device_id);
            mqtt_subscribe(sub_topic);
            
            // publish : report shadow connected true message
            sprintf(sub_topic, "$aws/things/%s/shadow/name/%s_shadow/update", device_id, device_id);
            mqtt_publish(sub_topic, REPORT_MSG_CONNECT);
            
            // subscribe : shadow topic
            sprintf(sub_topic, "$aws/things/%s/shadow/name/%s_shadow/update/delta", device_id, device_id);
            mqtt_subscribe(sub_topic);
            
            // subscribe : AWS IoT Jobs for OTA(On-The-Air) Stream
            sprintf(sub_topic, "$aws/things/%s/jobs/notify-next", device_id);
            mqtt_subscribe(sub_topic);
            sprintf(sub_topic, "$aws/things/%s/jobs/start-next", device_id);
            mqtt_subscribe(sub_topic);
            sprintf(sub_topic, "$aws/things/%s/jobs/start-next/accepted", device_id);
            mqtt_subscribe(sub_topic);
            sprintf(sub_topic, "$aws/things/%s/streams/+/data/json", device_id);
            mqtt_subscribe(sub_topic);
            
            break;
        case MQTT_EVENT_DISCONNECTED:
            mqtt_status = MQTT_EVENT_DISCONNECTED ; 
            ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED, try Reconnect");
            esp_err_t err = esp_mqtt_client_reconnect(mqtt_client);
            if(err != ESP_OK){  
                esp_restart();
            }
            break;
        case MQTT_EVENT_SUBSCRIBED:
            ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED. msg_id=%d", event->msg_id);
//            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, topic=%s", event->msg_id, event->topic);
            break;
        case MQTT_EVENT_DATA:
            ESP_LOGI(TAG, "MQTT_EVENT_DATA,  msg_id=%d, topic[%d]=%s", event->msg_id, event->topic_len, event->topic);
            ESP_LOGI(TAG, "DATA[%d]=%s", event->data_len, event->data);
            mqtt_recvprocessing(event->data);
            break;
        case MQTT_EVENT_BEFORE_CONNECT:
            if(mqtt_status == MQTT_EVENT_ANY){
                ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT, Disgard msg why Start mqtt client.");
            } else if(mqtt_status == MQTT_EVENT_DISCONNECTED){
                esp_mqtt_client_reconnect(mqtt_client);
                ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT, after Disconnect Event. Try Reconnect.");
            } else {
                ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT, msg_id=%d, topic=%s.",event->msg_id, event->topic);
            }
            break;
        case MQTT_EVENT_DELETED:
            ESP_LOGI(TAG, "MQTT_EVENT_DELETED, msg_id=%d", event->msg_id);
            break;
        case MQTT_EVENT_ERROR:
            ESP_LOGI(TAG, "MQTT_EVENT_ERROR, Reboot ESP");
            mqtt_status = MQTT_EVENT_ERROR;
            esp_restart();
            break;
        default:
            ESP_LOGI(TAG, "MQTT other event id: %d.", event->event_id);
            break;
        }
}

Who is online

Users browsing this forum: MicroController and 70 guests