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 how to perform multiple operations in ESP8266.
As we are using ESP8266, we will be using Arduino framework.
Let see how operations are performed in ESP8266 or any other embedded system.
How Operations are performed in ESP8266?
Before going to understand how to perform multiple operations in ESP8266. Let's understand what is how regular programming works in embedded systems.
Any activity in embedded system is done using loop function.
void loop() {
// Do something
}
This loop function will run continuously.

In this loop we can do multiple things.
For example we can read dht11 sensor data
void loop() {
// Read data from sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Send data to cloud
sendDataToCloud(temperature, humidity);
}
But this comes with a problem.
If we run this loop function, it will run continuously. Means there will be no delay between the operations.
How Normal Loop will cause problem?
Let's assume above operation takes 1 ms (which is not true in real world).
And DHT11 sensor should have 1 second delay between readings.
That means above loop will run 1000 times in 1 second to just see if the sensor data is changed.
Can you see the problem?
1000 times operation means 1000 times power consumption.
1000 times power consumption means battery will drain fast or embedded system will heat up.
To Avoid this we can use delay function.
void loop() {
// Read data from sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Send data to cloud
sendDataToCloud(temperature, humidity);
// Wait for 1 second
delay(1000);
}
By adding delay we are making sure that the loop function is not running continuously.
But this also has a problem.
If we add delay of 1 second, it means the loop function will run only once in 1 second.
So if we want to do multiple operations to be performed in specific intervals. It will be blocked.
Adding Other Operation in loop
Like this if we want to read dht11 sensor every 1 second and read mq2 sensor every 5 seconds. We can't use delay function.
It will be like this
void loop() {
// Read data from sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Send data to cloud
sendDataToCloud(temperature, humidity);
// Read data from sensor
float gas = analogRead(MQ2_PIN);
// Send data to cloud
sendDataToCloud(gas);
// Wait for 5 seconds
delay(5000);
}
This will cause 5 sec no data reading from dht11 sensor.
Here where task scheduler comes into picture.
What is Task Scheduler?
A task scheduler is a lightweight system that allows a processor to juggle multiple activities seemingly at the same time, a concept known as multitasking.
A Task Scheduler solves this problem using a "to-do list" approach.
Instead of freezing, the program loops continuously at top speed.
Every time it loops, the scheduler checks the clock and looks at its list of tasks to see if any are due to run right now.
If a task's time is up, the scheduler runs it, and then immediately goes back to checking the clock.
To understand the internal clock, imagine the microcontroller holding a stopwatch that starts counting milliseconds the moment it powers on.
In Arduino framework, this stopwatch is read using a function called millis().
Instead of pausing the whole system to wait, the scheduler constantly checks this stopwatch against a simple formula for each task:
Current Time - Last Time Executed >= Interval
Here is simple example of how task scheduler works in ESP8266.
Ex:
unsigned long previousDHTTime = 0; // Remembers the last time DHT ran
unsigned long previousMQ2Time = 0; // Remembers the last time MQ2 ran
const long dhtInterval = 1000; // 1000 milliseconds = 1 second
const long mq2Interval = 5000; // 5000 milliseconds = 5 seconds
void setup() {
// Initialization code goes here
}
void loop() {
// 1. Read the internal stopwatch
unsigned long currentMillis = millis();
// 2. Check the DHT Task ⏱️
if (currentMillis - previousDHTTime >= dhtInterval) {
previousDHTTime = currentMillis; // Reset this task's timer
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Send data to cloud
sendDataToCloud(temperature, humidity);
}
// 3. Check the MQ2 Task ⏱️
if (currentMillis - previousMQ2Time >= mq2Interval) {
previousMQ2Time = currentMillis; // Reset this task's timer
float gas = analogRead(MQ2_PIN);
// Send data to cloud
sendDataToCloud(gas);
}
}
This way we can perform multiple operations in specific intervals without blocking the loop function.
Conclusion
In this article we have seen how to perform multiple operations in specific intervals without blocking the loop function in ESP8266.
In next article we will use inbuilt task scheduler and itegrate with painlessmesh.
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





Top comments (0)