Skip to main content

Multi-Tasking on the ESP8266: A Beginner's Guide with FreeRTOS



Article By: Repon Sheikh, LinkedIn Account

The ESP8266 is a powerful and popular Wi-Fi enabled microcontroller, but its true potential is unlocked when you can run multiple processes simultaneously. This is where a Real-Time Operating System (RTOS) becomes invaluable. An RTOS allows a single processor to handle multiple tasks concurrently, which is crucial for applications that need to perform various functions like communication, sensor reading, and user interface updates without interruption.

This article introduces a straightforward project that serves as a perfect entry point into the world of RTOS development on the ESP8266. Using the FreeRTOS SDK, a widely adopted open-source RTOS, this example demonstrates how to create and manage two independent tasks. The full source code for this project can be found on GitHub: ESP8266_Multi_Task_RTOS_Example.

The Project Explained



At its core, this project defines two distinct tasks:

  1. blink_task: This task is responsible for the classic "Hello World" of embedded systems—blinking an LED. It runs in a continuous loop, turning an LED on and off at a 500-millisecond interval. Importantly, it uses a FreeRTOS function, vTaskDelay, to pause its execution, allowing the scheduler to run other tasks.

  2. print_task: This task is a simple logger. It periodically prints a message to the serial console, confirming that a separate process is running alongside the blinking LED. Like the blink task, it uses vTaskDelay to yield control back to the RTOS scheduler.





The main entry point of the application, user_init, is where these two tasks are created and launched. The xTaskCreate function is called for each task, specifying its function, name, stack size, priority, and handle. The RTOS scheduler then takes over, ensuring that both tasks receive CPU time and execute as intended.

Why an RTOS is Essential

For beginners, it's common to write code that uses a single while(1) loop, with all logic nested inside. This "bare-metal" approach works for simple projects but quickly becomes unmanageable. If one part of the code gets stuck in a loop (for example, waiting for a network response), the entire system freezes.

An RTOS solves this by providing a scheduler. The scheduler is a core component that allocates processing time to each task based on its priority. This pre-emptive scheduling model means that even if a lower-priority task is running, a higher-priority task can interrupt it to execute. This guarantees that critical functions, such as communication protocols or safety checks, are always executed promptly.

In our demo, the two tasks are given the same priority, so the scheduler will share the CPU time between them, creating the illusion that they are running at the same time.

Conclusion

This project is a fundamental first step toward building complex embedded systems with the ESP8266. By understanding how to create and manage independent tasks, you can design more robust and reliable applications. An RTOS is a powerful tool that transforms the way you think about embedded programming, moving from a linear execution model to a parallel, event-driven architecture.

Feel free to clone the repository and experiment with the code. Try changing the task priorities, adding a third task, or modifying the timing to see how the RTOS scheduler behaves!

Comments

Popular posts from this blog

BJT vs. MOSFET: Understanding the Key Differences

  When it comes to semiconductor devices, BJT (Bipolar Junction Transistor) and MOSFET (Metal-Oxide-Semiconductor Field-Effect Transistor) are two of the most commonly used transistors. They both play a critical role in amplifying or switching electronic signals, but their underlying principles and applications are quite different. In this post, we’ll explore the fundamental differences between BJT and MOSFET to help you understand their unique features and decide which one to use in your project. What is a BJT? A Bipolar Junction Transistor (BJT) is a current-controlled device that has three terminals: Collector (C) , Base (B) , and Emitter (E) . BJTs can be either NPN or PNP types, depending on the arrangement of their semiconductor layers. BJTs work by using a small current at the base to control a larger current flowing between the collector and emitter. How BJT Works: Current-Controlled : In a BJT, the amount of current that flows from the collector to the emitter is cont...

Arduino Programming Tutorial

In this tutorial, we'll cover the basics of Arduino programming, focusing on variable types , functions , function return types , and constant declarations . We will use simple examples to demonstrate how each concept works. 1. Variables in Arduino Variables are used to store data that your Arduino program can use. There are different types of variables based on the data they store. Common variable types: int : Stores integer values (whole numbers). float : Stores decimal numbers. char : Stores a single character. boolean : Stores true or false . String : Stores a sequence of characters (text). Example: int ledPin = 13 ; // Integer variable to store LED pin number float sensorValue = 0.0 ; // Float variable to store sensor value char myChar = 'A' ; // Char variable to store a character boolean isOn = true ; // Boolean variable to store true/false String myText = "Hello" ; // String variable to store a string 2. Constants Constants a...

Embedded system programming & development interest is becoming more popular

  Over the years there has been a growing interest in embedded systems development and programming as part of science technology and computer research. Due to the obligation to present a project or research in science in many schools, colleges, or universities, this subject is becoming necessary and acceptable day by day. In most cases, science projects are gaining importance as electronics and embedded system programming. This is because of their easy availability and the availability of lots of information on the Internet. photo: Arduino board, an example of an embedded board  Arduino Board is a top choice for beginners of electro-embedded designers because of this less complexity and availability of lots of resources online. easy and attractive beginning to an embedded programming platform is Arduino.  to be continued...  writer: Repon Sheikh