In this example, I will show you how to apply FreeRTOS in Arduino ESP32. Arduino ESP32 is built over FreeRTOS and actually the main program is put in a loopTask .
In this example we will have two tasks, an ESP32 task (loopTask), we will add another task to our application. This means our application has 2 tasks: ESP32 task will print the text “this is ESP32 task” and the second task will print “this is another task” on Serial terminal.
I recommend visiting http://www.FreeRTOS.org and reading up on FreeRTOS. There are some interesting resources available from https://www.freertos.org/Documentation/RTOS_book.html
Code
[cpp]
void setup()
{
Serial.begin(112500);
/* we create a new task here */
xTaskCreate(
anotherTask, /* Task function. */
“another Task”, /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
NULL); /* Task handle to keep track of created task */
}
/* the forever loop() function is invoked by Arduino ESP32 loopTask */
void loop()
{
Serial.println(“this is ESP32 Task”);
delay(1000);
}
/* this function will be invoked when additionalTask was created */
void anotherTask( void * parameter )
{
/* loop forever */
for(;;)
{
Serial.println(“this is another Task”);
delay(1000);
}
/* delete a task when finish,
this will never happen because this is infinity loop */
vTaskDelete( NULL );
}
[/cpp]
Output
Unsurprisingly if you open the Serial Monitor you should see something like this
this is another Task
this is ESP32 Task
this is another Task
this is ESP32 Task
this is another Task
this is ESP32 Task
this is another Task
this is ESP32 Task
this is another Task
this is ESP32 Task
this is another Task
this is ESP32 Task
this is another Task
this is ESP32 Task