I can't post my entire code as some of it is used commercially.
This is the code to initialise the client:
Code: Select all
void Setup() {
const esp_mqtt_client_config_t mqtt_cfg = {
.broker =
...
},
};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
if (client) {
vTaskDelay(pdMS_TO_TICKS(5000));
xQueueSend(setup_queue, &client, 0);
}
}
This is the task that starts the client:
Code: Select all
void StartClient(void* pvParameters) {
ESP_LOGI(TAG, "MQTT Task started");
EventBits_t bits =
xEventGroupWaitBits(wifi::wifi_event_group, wifi::kWifiConnectedBit,
pdFALSE, pdTRUE, portMAX_DELAY);
if (bits & wifi::kWifiConnectedBit) {
ESP_LOGI(TAG, "WiFi ready - attempting to connect to MQTT");
esp_mqtt_client_handle_t received_client;
if (xQueueReceive(setup_queue, &received_client, portMAX_DELAY)) {
esp_mqtt_client_start(received_client);
}
}
}
If I start the code with a massive allocated stack depth, the memory writes over other locations.
Code: Select all
xTaskCreate(StartClient, "mqtt", 106542, NULL, 1, &mqtt_task_handle);
I only know this is the case as it overwrites an objects variable which is used as the iterator in a loop, and when MQTT is started in this way, it creates an infinite loop in that component. I can see from printing the memory address and value that it gets set to some extremely large integer only after the MQTT is started (visible because of the delays I put in).
I apologise for getting confused with stack/heap, it's a little confusing as FreeRTOS allocates a stack for a task on to the heap, or am I understanding this wrong?
Thank you.