FreeRTOS task integration
Posted: Thu Jul 29, 2021 11:47 am
Hi guys,
I am programming a device, which should get some values from sensors and send them over the MQTT protocol back to the server. Right now, there are two ways how to connect to the internet - external LTE modem over pppos and WiFi integrated right on ESP32.
I created two tasks - MQTT and CONN. Basically, my main function spawns those two tasks.
The CONN task connects to the internet (either using WiFi or LTE modem - it depends on current settings and availability of each method) and then unlocks mutex shared with MQTT task. After this, CONN task just maintains connection - meaning it checks for disconnect events and tries to reconnect when possible, or switch between WiFi <-> LTE.
MQTT task, after unlocking from CONN connects to the broker and waits for other parts of the application to publish messages. Again, MQTT task is responsible for waiting for disconnect event to reconnect back.
Now comes my question. Is it okay to end the task with infinite while?
The problem is that mqtt_init function contains all the code necessary to connect to the broker, it starts event loops and registers event handlers. I suppose that without the loop at the end, all code would stop. But would not the infinite loop exhaust all resources while doing nothing? Should I insert small delay? Or is this completely wrong design?
Thank you.
I am programming a device, which should get some values from sensors and send them over the MQTT protocol back to the server. Right now, there are two ways how to connect to the internet - external LTE modem over pppos and WiFi integrated right on ESP32.
I created two tasks - MQTT and CONN. Basically, my main function spawns those two tasks.
The CONN task connects to the internet (either using WiFi or LTE modem - it depends on current settings and availability of each method) and then unlocks mutex shared with MQTT task. After this, CONN task just maintains connection - meaning it checks for disconnect events and tries to reconnect when possible, or switch between WiFi <-> LTE.
MQTT task, after unlocking from CONN connects to the broker and waits for other parts of the application to publish messages. Again, MQTT task is responsible for waiting for disconnect event to reconnect back.
Now comes my question. Is it okay to end the task with infinite while?
- void task_mqtt(void *param) {
- esp_err_t ret = ESP_FAIL;
- // Wait for muttex from CONN task
- xSemaphoreTake(mqtt_semaphore_start, portMAX_DELAY);
- // Connect to the broker, start event loop
- ret = mqtt_init();
- while (ret != ESP_OK) {
- // Wait before next try
- vTaskDelay(MS_TO_TICKS(10 * 1000));
- // Try it again
- ret = mqtt_init();
- }
- // Here, MQTT should be connected to the broker
- while (1) {
- // IS THIS REQUIRED?
- }
- }
Thank you.