ESP32-FreeRTOS: A Comprehensive Collection of FreeRTOS Examples for Reference and Learning

ESP32-FreeRTOS is a set of open-source example codes that awaken the fun features of the ESP32, such as multitasking, timers, events, and semaphores, making it easier and more reliable for you to write IoT applications.

What Can It Help You Do Don’t be fooled by the ESP32’s built-in Wi-Fi/Bluetooth; using bare-metal programming for complex projects can easily lead to chaos. FreeRTOS allows you to:

  • • Run multiple “apps” simultaneously, such as Wi-Fi connections, sensor data collection, and cloud communications, without interference.
  • • Easily handle inter-task data exchange using task notifications, queues, and semaphores.
  • • Manage software timers and event groups, making timed and asynchronous logic straightforward.
  • • Distribute loads across multiple cores, with one core handling networking and another processing business logic, significantly boosting performance.

What Pain Points Does It Address

  1. 1. Task interference: Bare-metal programming can lead to complex business logic that is difficult to maintain.
  2. 2. Asynchronous events: Timers, serial ports, and networks can lead to nested callbacks that feel like “callback hell”—with FreeRTOS’s event groups and queues, the situation becomes manageable.
  3. 3. Resource conflicts: Bare-metal resource contention can cause crashes; semaphores and mutexes help stabilize your application.
  4. 4. Portability issues: FreeRTOS provides a unified interface, allowing you to reuse parts of your code when transitioning from ESP32 to STM32.

How to Install and Use Prerequisites:

  • • A Windows/Linux/Mac computer
  • • Python3, Git
  • • An ESP32 development board (e.g., DEVKIT DOIT)

The installation steps are very friendly:

# 1. Get ESP-IDF
git clone --recursive https://github.com/espressif/esp-idf.git
cd esp-idf
./install.sh        # Run install.bat on Windows

# 2. Configure environment variables
../export.sh       # Run export.bat on Windows

# 3. Clone the ESP32-FreeRTOS example repository
cd ~
git clone https://github.com/DiegoPaezA/ESP32-freeRTOS.git
cd ESP32-freeRTOS

# 4. Compile and flash
cd examples/Task\ FreeRTOS
idf.py set-target esp32
idf.py menuconfig    # Configure Wi-Fi, serial port, etc. as needed
idf.py build flash monitor

It’s that simple! Open the serial monitor, and your board will be running the FreeRTOS example!

Example Code Here’s an example of an HTTP client that you can use right away:

#include <stdio.h>
#include "esp_wifi.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_http_client.h"
#include "protocol_examples_common.h"

esp_err_t clientEvent(esp_http_client_event_t *evt) {
  if (evt->event_id == HTTP_EVENT_ON_DATA) {
    printf("%.*s\n", evt->data_len, (char*)evt->data);
  }
  return ESP_OK;
}

void app_main(void) {
  nvs_flash_init();
  tcpip_adapter_init();
  esp_event_loop_create_default();
  example_connect(); // Connect to Wi-Fi

  esp_http_client_config_t clientConfig = {
   .url = "https://xxx.com",
   .event_handler = clientEvent
  };
  esp_http_client_handle_t client = esp_http_client_init(&clientConfig);
  esp_http_client_perform(client);
  esp_http_client_cleanup(client);
}

Common Examples Overview

Example Name Description
Task FreeRTOS Implement LED blinking using tasks
Task scheduler Dual-core test: tasks running on two CPUs
Semaphores_Mutex Mutex example, resource contention test
cxColas1 – Queue 1 Use queues to pass data between Task1 and Task2
timerSoftware Software timer, timed callback
EventsGroup – 2 Wake three tasks at once, event group synchronization
UART_DEMO Send “OK” via UART and capture with a logic analyzer
DHT Sensor Data collection from DHT temperature and humidity sensor
i2c_scanner Scan all devices on the I²C bus
WiFi_MQTT Connect to an MQTT server to implement data subscription/publishing

(More examples can be found in the project)

Pros and Cons Analysis Advantages:

  • • Quick to get started: ESP-IDF comes with FreeRTOS, making it easier than bare-metal programming.
  • • Comprehensive features: tasks, queues, semaphores, timers, event groups… everything you need.
  • • Extensible: Official updates are ongoing, and there are many community examples.Disadvantages:
  • • Learning curve: The concepts of RTOS can be initially unfriendly; understanding task scheduling, memory, and synchronization is necessary.
  • • Resource usage: RTOS consumes a few more KB than bare-metal, which needs to be evaluated in ultra-low-power scenarios.
  • • Complex debugging: Debugging multi-task crashes is more complicated than bare-metal.

Conclusion Overall, ESP32-FreeRTOS is truly a powerful tool for IoT development. If you’re working on DIY projects and want to implement more complex logic, cloud communications, or sensor data fusion, FreeRTOS can help you structure your code clearly. The downside is that there is a bit of a learning curve, but once you grasp the concepts, everything becomes simpler. Give it a try and dive into some real-world projects!

Project address: https://github.com/DiegoPaezA/ESP32-freeRTOS

Leave a Comment