How to use Task Scheduler in ESP8266?

1 5 48
calendar_todayschedule2 min read
— Originally published at dev.to

Hello, I'm Ganesh. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

In this article, We will be exploring inbult task function in ESP8266.

Core Logic of Task Scheduler

I want to perform two operations in ESP8266.

  1. Blink LED every 2.5 seconds 1 time.
  2. Blink LED every 5 seconds 2 times.

Let's see how to do this without using inbuilt task scheduler.

  1. Setup Serial communication and LED pin.
  2. Start Loop.
  3. Check if 2.5 seconds have passed since the last task activation.
  4. If yes, perform the blink.
  5. Check if 5 seconds have passed since the last task activation.
  6. If yes, perform the blink
#include <Arduino.h>

unsigned long t25, t50;

/**
 * Performs a single blink on the built-in LED (Active Low).
 * @param message Debug message to output to Serial.
 */
void performBlink(const char *message) {
  Serial.println(message);
  digitalWrite(LED_BUILTIN, 0); // LED ON
  delay(80);
  digitalWrite(LED_BUILTIN, 1); // LED OFF
  delay(80);
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 1); // Start with LED OFF
}

void loop() {
  // Task 1: Every 2.5s (triggers at 2.5s, 5.0s, 7.5s...)
  if (millis() - t25 >= 2500) {
    t25 = millis();
    performBlink("[2.5s] ");
  }

  // Task 2: Every 5.0s (triggers at 5.0s, 10.0s...)
  // At 5s, both tasks trigger, resulting in a double blink.
  if (millis() - t50 >= 5000) {
    t50 = millis();
    performBlink("[5.0s] ");
  }
}

Output:

[2.5s] 
[5.0s] 
[2.5s] 
[2.5s] 
[5.0s] 

See as we defined to use 2.5 sec interval for first task and 5 sec interval for second task.

This also mean 2nd time 2.5 sec blink will happen after 5 sec from 1st blink.

This will create 2 times blink in 5 sec.

Using Task Scheduler

We are going to use task scheduler library to perform multiple operations in specific intervals.

Let's see how to do this using task scheduler library.

  1. Include task scheduler library.
  2. Create a scheduler object.
  3. Define tasks.
  4. Add tasks to the scheduler.
  5. Enable tasks.
  6. Run the scheduler.
#include <Arduino.h>
#include <TaskScheduler.h>

Scheduler runner;

/**
 * Performs a single blink pulse.
 * Note: NodeMCU V2 built-in LED is active LOW.
 */
void pulse() {
  digitalWrite(LED_BUILTIN, 0); // ON
  delay(80);
  digitalWrite(LED_BUILTIN, 1); // OFF
  delay(80);
}

void Callback(const char *msg) {
  Serial.println(msg);
  pulse();
}

// Task Definitions
Task t25(2500, TASK_FOREVER, []() { Callback("[2.5s] "); });
Task t50(5000, TASK_FOREVER, []() { Callback("[5.0s] "); });

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 1); // Start OFF

  runner.init();

  // Add tasks to the scheduler
  runner.addTask(t25);
  runner.addTask(t50);

  // Enable tasks
  t25.enable();
  t50.enable();
}

void loop() { runner.execute(); }


By this way we can perform multiple operations in specific intervals without blocking the loop function.

Output:

[5.0s] 
[2.5s] 
[5.0s] 
[2.5s] 
[2.5s] 

You can also see 5.0s blink is happening first and sometimes 2.5s blink is happening first.

This is because of the task scheduler.

Conclusion

By this we understood how task scheduler works in ESP8266.

And we actualy used task scheduler to perform multiple operations in specific intervals without blocking the loop function.

git-lrc

Any feedback or contributors are welcome! It’s online, source-available, and ready for anyone to use.
⭐ Star it on GitHub: https://github.com/HexmosTech/git-lrc

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

How to perform multiple operations in the ESP8266?

Ganesh Kumar - Apr 10

Automation periodically tasks with CRON job scheduler

xinitd - Jan 13, 2025

How Do Smart Devices Perform Multiple Tasks?

Ganesh Kumar - May 4
chevron_left
1.3k Points54 Badges
60Posts
7Comments
4Connections
Right now I'm focused on LiveReview, AI code review that gives teams control over AI-generated code without slowing them down.

Related Jobs

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!