I’m doing an MQTT library for a project using the Espressif ID, and when I try to subscribe to more than 2 topics with one ESP32 i get the following error:
[1B][0;31mE (2594) mqtt_client: Subscribe message cannot be created[1B][0m
Is there any configuration that I need to change/add on the client handler?
esp_mqtt_client_config_t cfg = {
.broker.address.hostname = ip,
.broker.address.port = port,
.broker.address.transport = MQTT_TRANSPORT_OVER_TCP,
.credentials.set_null_client_id = true,
.session.protocol_ver = MQTT_PROTOCOL_V_3_1_1,
.task.priority = 1,
.task.stack_size = 4096,
.buffer.size = 32,
.buffer.out_size = 32
};
I’ll leave here the MQTT Event Handler and the topics I’m trying to subscribe:
#define NUM_OF_TOPICS (uint8_t)(3)
static const char* topics[NUM_OF_TOPICS] = {"esp1/dev1", "esp1/dev2", "esp1/dev3"};
static void mqttEventHandler(void* args, esp_event_base_t base, int32_t id, void* data) {
static uint8_t retry = MQTT_MAX_RETRY;
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)data;
esp_mqtt_client_handle_t client = event->client;
switch ((esp_mqtt_event_id_t)id) {
case MQTT_EVENT_CONNECTED:
retry = MQTT_MAX_RETRY;
esp_mqtt_topic_t list[NUM_OF_TOPICS];
for (uint8_t t = 0; t < NUM_OF_TOPICS; t++) {
list[t].filter = topics[t];
list[t].qos = 2;
}
esp_mqtt_client_subscribe_multiple(client, list, NUM_OF_TOPICS);
break;
case MQTT_EVENT_DISCONNECTED:
if (--retry) {
esp_mqtt_client_start(client);
}
break;
case MQTT_EVENT_SUBSCRIBED:
printf("[mqtt] Subscribed to all topics.n");
break;
case MQTT_EVENT_PUBLISHED:
printf("[mqtt] Published a message.n");
break;
case MQTT_EVENT_DATA:
printf("[mqtt] Received data -> %.*sn", event->data_len, event->data);
printf("[mqtt] @ topic -> %.*sn", event->topic_len, event->topic);
// User code begin
// User code end
memset(event->topic, '', event->topic_len);
memset(event->data, '', event->data_len);
break;
case MQTT_EVENT_BEFORE_CONNECT:
printf("[mqtt] Starting connection...n");
break;
default:
printf("[mqtt] Undefined event: %dn", (uint8_t)id);
break;
}
}
I tried to change the list of topics to subscribe to dynamic memory, expecting the problem to be the memory allocation for the list of topics, but the same error occurred.