Master The ESP32 WiFi: Practical Tasks
Written by Harry Fairhead and Mike James   
Wednesday, 07 January 2026
Article Index
Master The ESP32 WiFi: Practical Tasks
First To Finish

First To Finish

Another requirement is to wait for one of a group of tasks to finish. This is very easy using a semaphore, again assuming two cores, folder First:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_random.h"
SemaphoreHandle_t xSemaphore;
void task0(void *arg)
{
    uint32_t delay = (esp_random() % 500) + 1;
    vTaskDelay(delay);
    printf("task0\n");
    xSemaphoreGive(xSemaphore);
    vTaskDelete(NULL);
}
void task1(void *arg)
{
    uint32_t delay = (esp_random() % 500) + 1;
    vTaskDelay(delay);
    printf("task1\n");
    xSemaphoreGive(xSemaphore);
    vTaskDelete(NULL);
}
void app_main()
{
    xSemaphore=xSemaphoreCreateBinary();
    xTaskCreatePinnedToCore(task0,"task0", 4000,
NULL, 0, NULL, 1); xTaskCreatePinnedToCore(task1,"task1", 4000,
NULL, 0, NULL, 0); xSemaphoreTake(xSemaphore, portMAX_DELAY); printf("task complete\n"); }

In this case each task takes a random amount of time to complete and when it does it signals the app_main task that it has finished. The final task eventually completes, but it could be terminated by the app_main task.

ESPMaster180

All Finished

To detect when a set of tasks has finished is a little more complicated, but the semaphore still does the job very well. The idea is that if you need to wait for N tasks to complete you need to Take the semaphore N times. To allow for all of the tasks to complete at exactly the same time the semaphore has to be able to store a count of N. Each task simply gives the semaphore when it has completed and the tasks waiting count the tasks off using a for loop, assuming two cores, folder All:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_random.h"
SemaphoreHandle_t xCountingSemaphore;
void task0(void *arg)
{
    uint32_t delay = (esp_random() % 500) + 1;
    vTaskDelay(delay);
    printf("task0\n");
    xSemaphoreGive(xCountingSemaphore);
    vTaskDelete(NULL);
}
void task1(void *arg)
{
    uint32_t delay = (esp_random() % 500) + 1;
    vTaskDelay(delay);
    printf("task1\n");
    xSemaphoreGive(xCountingSemaphore);
    vTaskDelete(NULL);
}
void app_main()
{
   xCountingSemaphore = xSemaphoreCreateCounting(2, 0);
   xTaskCreatePinnedToCore(task0, "task0", 4000,
NULL, 0, NULL, 1); xTaskCreatePinnedToCore(task1, "task1", 4000,
NULL, 0, NULL, 0); for (int i = 0; i < 2; i++) { xSemaphoreTake(xCountingSemaphore, portMAX_DELAY); } printf("task complete\n"); }

The semaphore is set to 0 with a maximum count of 2 as there are only two tasks to monitor.

In Chapter but not in this extract

  • Creating an Async Function
  • Interrupt-Based Async

Summary

  • Semaphore can be used to wait for another task to complete or to reach a particular point.

  • When multiple tasks are all trying to achieve a result, a semaphore can be used to wait for the first of the group to complete or to wait for all of the tasks in a group to complete.

  • Any function can be converted into an asynchronous function by yielding control back to FreeRTOS at regular intervals.

  • A better method of converting a function into an asynchronous function is to set up an interrupt handler that is called when the function can do some work. If no such interrupt exists a periodic yield can be used to achieve the same result.

MasterThe ESP32In C:
WiFi with the ESP-IDF, FreeRTOS, LwIP & MbedTLS

By Harry Fairhead & Mike James

ESPMaster360

Buy from Amazon.

Contents

  • Chapter 1 FreeRTOS Basics 
  • Chapter 2 Locks and Synchronization
  • Chapter 3 Practical Tasks 
     Extract:
     Practical Tasks  NEW!!!
  • Chapter 4 Data Structures
  • Chapter 5 Basic WiFi
  • Chapter 6 Using LwIP
  • Chapter 7 Sockets and HTTP Clients 
  • Chapter 8 Socket Server
  • Chapter 9 Details of Cryptography
  • Chapter 10 SSL/TLS and HTTPS
  • Chapter 11 SSL Server
  • Chapter 12 UDP For Speed
  • Chapter 13 SNTP For Timekeeping
  • Chapter 14 SMTP For Email
  • Chapter 15 MQTT For The IoT
  • Chapter 16 Advanced WiFi and AP Mode
      Extract: 
     ESPNow 
  • Appendix I Advanced WiFi Configuration

<ASIN:B0G5M76KLB> 

pico book

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Facebook or Linkedin.



Last Updated ( Wednesday, 07 January 2026 )