<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged read</title>
<link>https://coderlegion.com/tag/read</link>
<description>Powered by Question2Answer</description>
<item>
<title>How Do Smart Devices Perform Multiple Tasks?</title>
<link>https://coderlegion.com/16118/how-do-smart-devices-perform-multiple-tasks</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star Us&lt;/a&gt; to help devs discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;Ever wonder how your Alexa, robot vacuum cleaner, and smartwatch seamlessly perform multiple tasks without getting stuck in the middle? How exactly are these devices programmed to handle it all?&lt;/p&gt;

&lt;p&gt;You came to the right place to understand how it happens under the hood.&lt;/p&gt;

&lt;p&gt;In this article, we will understand how these tasks are performed and how they work.&lt;/p&gt;

&lt;p&gt;As all these devices use a microcontroller. I will be explaining using the simple microcontroller &lt;code&gt;ESP8266&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How will the task be performed?
&lt;/h2&gt;

&lt;p&gt;Every smart device consists of a microcontroller. These microcontrollers are programmed to perform specific tasks.&lt;/p&gt;

&lt;p&gt;These tasks are performed using a single loop function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Perform Task&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This loop function will run continuously.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pfzvp6pl2vc3456w6s7.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9pfzvp6pl2vc3456w6s7.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;210&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this loop, we can do multiple things.&lt;/p&gt;

&lt;p&gt;Let's take the example of a weather monitoring device.&lt;br&gt;
These operations are done using a single loop function. As there is nothing else to do.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start Device&lt;/li&gt;
&lt;li&gt;Start Loop&lt;/li&gt;
&lt;li&gt;Read sensor data&lt;/li&gt;
&lt;li&gt;Display data / Store data&lt;/li&gt;
&lt;li&gt;Repeat the process
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;But this comes with a problem. &lt;/p&gt;

&lt;p&gt;This loop will be running with a microsecond delay.&lt;br&gt;
If we run this loop function, it will run continuously. Means there will be no delay between the operations.&lt;/p&gt;
&lt;h2&gt;
  
  
  How will a normal loop cause a problem?
&lt;/h2&gt;

&lt;p&gt;Let's assume the above operation takes 1 ms (which is not true in real-world use, as it depends on which microcontroller you use).&lt;/p&gt;

&lt;p&gt;The DHT11 sensor should have a 1-second delay between readings.&lt;/p&gt;

&lt;p&gt;That means the above loop will run 1000 times in 1 second to just see if the sensor data has changed.&lt;/p&gt;

&lt;p&gt;Can you see the problem?&lt;/p&gt;

&lt;p&gt;1000 times operation means 1000 times power consumption.&lt;/p&gt;

&lt;p&gt;1000 times the power consumption means the battery will drain fast, or the embedded system will heat up.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjhcsxp65gge0pu0ipw.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjhcsxp65gge0pu0ipw.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;419&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid this, we can use a delay function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Wait for 1 second&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding a delay, we are making sure that the loop function is not running continuously.&lt;/p&gt;

&lt;p&gt;This is a good implementation if we have only one task to perform.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4590vh7dezcbp8ee5s9c.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4590vh7dezcbp8ee5s9c.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;400&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if we want to do multiple operations to be performed in specific intervals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Other Operations in a loop
&lt;/h2&gt;

&lt;p&gt;Like this, if we want to read the DHT11 sensor every 1 second and read the MQ2 sensor every 5 seconds. We can't use the delay function.&lt;/p&gt;

&lt;p&gt;It will be like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Wait for 5 seconds&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will cause a 5-second no-data reading from the DHT11 sensor.&lt;/p&gt;

&lt;p&gt;Hence, there will be data loss for 5 seconds.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foz2eu4u5zcgthgqn8494.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foz2eu4u5zcgthgqn8494.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;392&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is where the task scheduler comes into the picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Task Scheduler?
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;A Task Scheduler solves this problem using a &quot;to-do list&quot; approach. &lt;/p&gt;

&lt;p&gt;Instead of freezing, the program loops continuously at top speed. &lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;If a task's time is up, the scheduler runs it and then immediately goes back to checking the clock.&lt;/p&gt;

&lt;p&gt;To understand the internal clock, imagine the microcontroller holding a stopwatch that starts counting milliseconds the moment it powers on. &lt;/p&gt;

&lt;p&gt;In the Arduino framework, this stopwatch is read using a function called &lt;code&gt;millis()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of pausing the whole system to wait, the scheduler constantly checks this stopwatch against a simple formula for each task:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Current Time - Last Time Executed &amp;gt;= Interval&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple example of how the task scheduler works in ESP8266.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Remembers the last time DHT ran&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Remembers the last time MQ2 ran&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 1000 milliseconds = 1 second&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 5000 milliseconds = 5 seconds&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Initialization code goes here&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// 1. Read the internal stopwatch&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; 

 &lt;span class=&quot;c1&quot;&gt;// 2. Check the DHT Task&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// 3. Check the MQ2 Task&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqvsh8kgn96wljcpdkeg.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqvsh8kgn96wljcpdkeg.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;535&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way, we can perform multiple operations in specific intervals without blocking the loop function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Logic of Task Scheduler
&lt;/h2&gt;

&lt;p&gt;I want to perform two operations in ESP8266.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Blink the LED every 2.5 seconds 1 time.&lt;/li&gt;
&lt;li&gt;Blink the LED every 5 seconds 2 times.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see how to do this without using the built-in task scheduler.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set up serial communication and the LED pin.&lt;/li&gt;
&lt;li&gt;Start Loop.&lt;/li&gt;
&lt;li&gt;Check if 2.5 seconds have passed since the last task activation.&lt;/li&gt;
&lt;li&gt;If yes, perform the blink.&lt;/li&gt;
&lt;li&gt;Check if 5 seconds have passed since the last task activation.&lt;/li&gt;
&lt;li&gt;If yes, perform the blink
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;Arduino.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
 * Performs a single blink on the built-in LED (Active Low).
 * @param message Debug message to output to Serial.
 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;performBlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// LED ON&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// LED OFF&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Serial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;115200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;pinMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OUTPUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;digitalWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LED_BUILTIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Start with LED OFF&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// Task 1: Every 2.5s (triggers at 2.5s, 5.0s, 7.5s...)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t25&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;t25&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;performBlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[2.5s] &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;// Task 2: Every 5.0s (triggers at 5.0s, 10.0s...)&lt;/span&gt;
 &lt;span class=&quot;c1&quot;&gt;// At 5s, both tasks trigger, resulting in a double blink.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;t50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;performBlink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[5.0s] &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight shell&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2.5s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;5.0s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2.5s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2.5s] 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;5.0s] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See, as we defined, to use a 2.5 sec interval for the first task and a 5 sec interval for the second task. &lt;/p&gt;

&lt;p&gt;This also means the 2nd time, a 2.5 sec blink will happen after 5 sec from the 1st blink. &lt;/p&gt;

&lt;p&gt;This will create a 2-second blink in 5 sec. &lt;/p&gt;

&amp;lt;h2
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/16118/how-do-smart-devices-perform-multiple-tasks</guid>
<pubDate>Mon, 04 May 2026 10:58:23 +0000</pubDate>
</item>
<item>
<title>Five Caching Strategies Every Backend Dev Should Know</title>
<link>https://coderlegion.com/14654/five-caching-strategies-every-backend-dev-should-know</link>
<description>
&lt;p&gt;&lt;img src=&quot;https://blog.ratnesh-maurya.com/images/blog/Distributed-Caching-Strategies-Architecture.png&quot; alt=&quot;Five Caching Strategies Every Backend Dev Should Know&quot;&gt;&lt;/p&gt;&lt;h1&gt;Five Caching Strategies Every Backend Dev Should Know&lt;/h1&gt;
&lt;p&gt;If you build backend systems, you eventually run into &lt;strong&gt;caching&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Someone says &amp;ldquo;just put Redis in front of it&amp;rdquo; and suddenly your app is faster&amp;hellip; until you start seeing stale data, cache misses, and weird bugs when you deploy.&lt;/p&gt;
&lt;p&gt;This post explains &lt;strong&gt;five core caching strategies&lt;/strong&gt; in plain language:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cache-Aside&lt;/li&gt;
&lt;li&gt;Read-Through&lt;/li&gt;
&lt;li&gt;Write-Through&lt;/li&gt;
&lt;li&gt;Write-Back&lt;/li&gt;
&lt;li&gt;Write-Around&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You&amp;rsquo;ll see &lt;strong&gt;how each one works&lt;/strong&gt;, &lt;strong&gt;what can go wrong&lt;/strong&gt;, and &lt;strong&gt;when to use it&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;A cache is just a faster, smaller copy of some data.
The hard part is &lt;strong&gt;keeping that copy useful&lt;/strong&gt;: when to fill it, when to read from it, and when to update or skip it.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;1. Cache-Aside (Lazy Loading)&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Idea:&lt;/strong&gt;
The application talks to the cache and the database.
It &amp;ldquo;checks the cache first, then falls back to the DB&amp;rdquo; and fills the cache on a miss.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;If the key is in the cache (a hit), just return it. Super fast.


If the key is missing, the app queries the database instead.


The app stores the result in the cache and returns it to the user.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why people love it&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Simple and flexible&lt;/strong&gt;: logic lives in your code; you control what goes in the cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Resilient&lt;/strong&gt;: if the cache dies, the app can still read from the DB (just slower).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Memory-efficient&lt;/strong&gt;: only data that is actually read gets cached.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;What can go wrong&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first&lt;/strong&gt; request for a key is slow (cold cache).&lt;/li&gt;
&lt;li&gt;If the DB is updated &lt;strong&gt;outside&lt;/strong&gt; your code path (e.g. admin tool, another service), the cache can serve &lt;strong&gt;stale data&lt;/strong&gt; until TTL expires or you manually invalidate.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;When to use&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Great default for &lt;strong&gt;read-heavy&lt;/strong&gt; apps (blogs, product catalogs, many dashboards).&lt;/li&gt;
&lt;li&gt;When you&amp;rsquo;re just adding caching to an existing codebase and want full control.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;2. Read-Through&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Idea:&lt;/strong&gt;
The application pretends the &lt;strong&gt;cache is the main database&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;It calls the cache for every read. On a miss, the &lt;strong&gt;cache layer itself&lt;/strong&gt; fetches from the DB, stores the result, and returns it.&lt;/p&gt;
&lt;p&gt;With &lt;strong&gt;Cache-Aside&lt;/strong&gt;, your app code calls both cache and DB.
With &lt;strong&gt;Read-Through&lt;/strong&gt;, your app only calls the cache; a loader behind the cache talks to the DB.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cleaner application code&lt;/strong&gt;: your code only knows &amp;ldquo;get from cache&amp;rdquo;.&lt;/li&gt;
&lt;li&gt;Cache libraries or infrastructure can handle loading, retries, and metrics.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If some writes go &lt;strong&gt;directly to the DB&lt;/strong&gt; (bypassing the cache API), the cache can easily become &lt;strong&gt;stale&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You need a cache provider or library that supports read-through loading.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;When to use&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Larger teams where you want &lt;strong&gt;centralized cache logic&lt;/strong&gt;, not ad-hoc caching scattered everywhere.&lt;/li&gt;
&lt;li&gt;Situations where infra/platform team owns the cache layer.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;3. Write-Through&lt;/h2&gt;
&lt;p&gt;So far we talked about reads.
Now let&amp;rsquo;s talk about &lt;strong&gt;writes&lt;/strong&gt;: how we keep cache and DB in sync.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Write-Through&lt;/strong&gt; means:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;On a write, update the cache &lt;strong&gt;and&lt;/strong&gt; the database synchronously.
Only when both succeed is the write considered &amp;ldquo;done&amp;rdquo;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Flow&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User updates something (e.g. changes quantity in a cart).&lt;/li&gt;
&lt;li&gt;App writes the new value into the &lt;strong&gt;cache&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;App also writes the same value into the &lt;strong&gt;database&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Only if both succeed do we return &amp;ldquo;OK&amp;rdquo; to the user.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strong consistency&lt;/strong&gt;: cache and DB always match for that key.&lt;/li&gt;
&lt;li&gt;Reads from the cache always see the latest value.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Slower writes&lt;/strong&gt;: every write has to hit both cache and DB before returning.&lt;/li&gt;
&lt;li&gt;If DB is slow, your API write latency is also slow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;When to use&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When correctness matters more than raw speed:
&lt;ul&gt;
&lt;li&gt;balances in a &lt;strong&gt;wallet / bank&lt;/strong&gt;,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;inventory&lt;/strong&gt; in an e&amp;#8209;commerce site,&lt;/li&gt;
&lt;li&gt;data where a stale read would be really bad.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;4. Write-Back (Write-Behind)&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Write-Back&lt;/strong&gt; trades strict consistency for &lt;strong&gt;speed&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;On a write, update &lt;strong&gt;only the cache&lt;/strong&gt; and return success quickly.
Later, an async process flushes those changes to the database.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Flow&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User sends a write.&lt;/li&gt;
&lt;li&gt;App writes to cache and returns &lt;code&gt;200 OK&lt;/code&gt; immediately.&lt;/li&gt;
&lt;li&gt;A background worker or the cache itself periodically writes batched updates into the DB.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Very fast writes&lt;/strong&gt;: app isn&amp;rsquo;t blocked waiting for DB.&lt;/li&gt;
&lt;li&gt;Great for high-throughput workloads:
&lt;ul&gt;
&lt;li&gt;counters (views, likes),&lt;/li&gt;
&lt;li&gt;logs / telemetry,&lt;/li&gt;
&lt;li&gt;social feed events.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Risk of data loss&lt;/strong&gt;: if the cache crashes before flushing to DB, those changes are gone.&lt;/li&gt;
&lt;li&gt;DB is &lt;strong&gt;eventually&lt;/strong&gt; updated, not immediately.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;When to use&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Write-heavy workloads where losing a tiny fraction of data is acceptable:
&lt;ul&gt;
&lt;li&gt;metrics, dashboards, click-streams,&lt;/li&gt;
&lt;li&gt;non-critical logs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;5. Write-Around&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Write-Around&lt;/strong&gt; takes a different approach:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Writes &lt;strong&gt;skip the cache&lt;/strong&gt; and go straight to the DB.
The cache is only filled later when data is read.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Flow&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A write goes &lt;strong&gt;directly&lt;/strong&gt; into the database.&lt;/li&gt;
&lt;li&gt;The cache is &lt;strong&gt;not&lt;/strong&gt; updated.&lt;/li&gt;
&lt;li&gt;On the next read, the app (or cache) sees a &lt;strong&gt;miss&lt;/strong&gt;:
&lt;ul&gt;
&lt;li&gt;loads from DB,&lt;/li&gt;
&lt;li&gt;populates the cache,&lt;/li&gt;
&lt;li&gt;and returns the value.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Prevents &amp;ldquo;polluting&amp;rdquo; the cache with data that is &lt;strong&gt;written but rarely read&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Good when you have big write batches (imports, ETL jobs) that users won&amp;rsquo;t read immediately.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first read&lt;/strong&gt; after a write is always a &lt;strong&gt;cache miss&lt;/strong&gt; (slower).&lt;/li&gt;
&lt;li&gt;More frequent DB reads if many keys are only read once.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;When to use&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Large imports, archival data, logs that might be read only occasionally.&lt;/li&gt;
&lt;li&gt;Systems where memory is tight and you want the cache to focus on &lt;strong&gt;truly hot&lt;/strong&gt; data.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;Side-by-side comparison&lt;/h2&gt;
&lt;p&gt;Here&amp;rsquo;s a quick comparison of these five strategies using the labels you provided.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;How teams typically choose&lt;/h2&gt;
&lt;p&gt;To connect this with the interactive content you built:&lt;/p&gt;
&lt;p&gt;These charts show two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cache-Aside&lt;/strong&gt; dominates in real systems because it&amp;rsquo;s &lt;strong&gt;simple and resilient&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write-Back&lt;/strong&gt; gives the fastest writes but carries the most risk;
&lt;strong&gt;Write-Through&lt;/strong&gt; is safest but slower on each write.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;Which caching strategy should you pick? (a practical rule-set)&lt;/h2&gt;
&lt;p&gt;If you only remember one thing from this post, remember this:
&lt;strong&gt;you pick a caching strategy based on the cost of a stale read and the cost of a slow write.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a fast, real-world decision guide:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Most read-heavy apps&lt;/strong&gt; (catalogs, blogs, dashboards):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Start with &lt;strong&gt;Cache-Aside&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;TTLs&lt;/strong&gt; and a plan for &lt;strong&gt;invalidation&lt;/strong&gt; if data changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;You want a clean abstraction and centralized caching&lt;/strong&gt; (platform/infra owned):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Read-Through&lt;/strong&gt; so product code only depends on the cache API.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A stale read would be painful&lt;/strong&gt; (wallet balances, inventory, &amp;ldquo;did my payment go through?&amp;rdquo;):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Prefer &lt;strong&gt;Write-Through&lt;/strong&gt; (consistency &amp;gt; speed).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Writes are hot and you can tolerate some eventual consistency&lt;/strong&gt; (counters, analytics, clickstream):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Consider &lt;strong&gt;Write-Back&lt;/strong&gt;, but only with durability guardrails (below).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;You write lots of data that is rarely read&lt;/strong&gt; (imports, ETL, logs):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Write-Around&lt;/strong&gt; to avoid polluting the cache.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;Two failure modes teams hit in production (and what to do)&lt;/h2&gt;
&lt;p&gt;Caching rarely fails in obvious ways. It fails with &amp;ldquo;the DB is melting&amp;rdquo; or &amp;ldquo;why is data stale?&amp;rdquo;.
Two patterns show up everywhere.&lt;/p&gt;
&lt;h3&gt;1) Cache stampede (thundering herd)&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Symptom:&lt;/strong&gt; a hot key expires (or cache restarts), and suddenly thousands of requests miss at once and slam the DB.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mitigations:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Request coalescing / single-flight&lt;/strong&gt;: only one request recomputes the value; others wait and reuse it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stale-while-revalidate&lt;/strong&gt;: keep serving a slightly stale value while one background refresh updates it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jittered TTLs&lt;/strong&gt;: add randomization to expiration times so many keys don&amp;rsquo;t expire at the same second.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;2) Hot key / hotspotting&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Symptom:&lt;/strong&gt; one key becomes so popular (home feed, trending item, auth config) that a single cache shard or a single DB row gets hammered.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mitigations:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Shard the key&lt;/strong&gt; (e.g. &lt;code&gt;trending:v1:0..N&lt;/code&gt;) and merge results.&lt;/li&gt;
&lt;li&gt;Use a &lt;strong&gt;two-layer cache&lt;/strong&gt;: per-instance in-memory (tiny TTL) + shared Redis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Precompute&lt;/strong&gt; or cache at a higher level (edge/CDN) if the data is public.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2&gt;What to monitor so caching doesn&amp;rsquo;t become &amp;ldquo;magic&amp;rdquo;&lt;/h2&gt;
&lt;p&gt;Whatever strategy you choose, watch these:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cache hit rate&lt;/strong&gt; (overall and by endpoint)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DB QPS&lt;/strong&gt; (does caching actually reduce it?)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;p95/p99 latency&lt;/strong&gt; (cache helps only if tail latency improves)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eviction rate / memory pressure&lt;/strong&gt; (are you constantly evicting hot data?)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stale reads / correctness signals&lt;/strong&gt; (app-specific: inventory mismatches, stale dashboards, etc.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you can&amp;rsquo;t measure these, caching will eventually surprise you.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Putting it all together (for a newbie)&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re just starting with caching:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Start with Cache-Aside&lt;/strong&gt; for reads.
It&amp;rsquo;s simple, safe, and easy to reason about.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add Read-Through&lt;/strong&gt; if you want a cleaner abstraction and your cache provider supports it.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For writes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Write-Through&lt;/strong&gt; when you absolutely need the cache and DB to agree on every write.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;Write-Back&lt;/strong&gt; when you care a lot about write speed and can tolerate a little risk.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;Write-Around&lt;/strong&gt; when you write lots of data that users won&amp;rsquo;t read immediately.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Whatever you choose, always:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Set reasonable &lt;strong&gt;TTLs&lt;/strong&gt;,&lt;/li&gt;
&lt;li&gt;Have a plan for &lt;strong&gt;invalidating&lt;/strong&gt; keys,&lt;/li&gt;
&lt;li&gt;And monitor &lt;strong&gt;hit rate&lt;/strong&gt; and &lt;strong&gt;latency&lt;/strong&gt; so you can adjust over time.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With just these five strategies, you can handle most caching problems you&amp;rsquo;ll run into as a backend engineer.&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/14654/five-caching-strategies-every-backend-dev-should-know</guid>
<pubDate>Sun, 19 Apr 2026 19:33:53 +0000</pubDate>
</item>
<item>
<title>How to perform multiple operations in the ESP8266?</title>
<link>https://coderlegion.com/14077/how-to-perform-multiple-operations-in-the-esp8266</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star Us&lt;/a&gt; to help devs discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;p&gt;In this article, We will be exploring how to perform multiple operations in ESP8266.&lt;/p&gt;

&lt;p&gt;As we are using ESP8266, we will be using Arduino framework.&lt;br&gt;
Let see how operations are performed in ESP8266 or any other embedded system.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Operations are performed in ESP8266?
&lt;/h2&gt;

&lt;p&gt;Before going to understand how to perform multiple operations in ESP8266. Let's understand what is how regular programming works in embedded systems.&lt;/p&gt;

&lt;p&gt;Any activity in embedded system is done using loop function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Do something&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This loop function will run continuously.&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z7vutuawksya5mgg9pu.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z7vutuawksya5mgg9pu.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;210&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this loop we can do multiple things.&lt;/p&gt;

&lt;p&gt;For example we can read dht11 sensor data&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this comes with a problem. &lt;/p&gt;

&lt;p&gt;If we run this loop function, it will run continuously. Means there will be no delay between the operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Normal Loop will cause problem?
&lt;/h2&gt;

&lt;p&gt;Let's assume above operation takes 1 ms (which is not true in real world).&lt;br&gt;
And DHT11 sensor should have 1 second delay between readings.&lt;/p&gt;

&lt;p&gt;That means above loop will run 1000 times in 1 second to just see if the sensor data is changed.&lt;/p&gt;

&lt;p&gt;Can you see the problem?&lt;/p&gt;

&lt;p&gt;1000 times operation means 1000 times power consumption.&lt;/p&gt;

&lt;p&gt;1000 times power consumption means battery will drain fast or embedded system will heat up.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs01qpskpmds6chix0kqs.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs01qpskpmds6chix0kqs.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;418&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To Avoid this we can use delay function.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Wait for 1 second&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding delay we are making sure that the loop function is not running continuously.&lt;/p&gt;

&lt;p&gt;But this also has a problem. &lt;/p&gt;

&lt;p&gt;If we add delay of 1 second, it means the loop function will run only once in 1 second.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fte8v3x8mx0btwzmwa2uq.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fte8v3x8mx0btwzmwa2uq.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;400&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if we want to do multiple operations to be performed in specific intervals. It will be blocked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Other Operation in loop
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;It will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Read data from sensor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Wait for 5 seconds&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will cause 5 sec no data reading from dht11 sensor.&lt;/p&gt;

&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnllq9w0u7kaxva0j7kr.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnllq9w0u7kaxva0j7kr.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;392&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here where task scheduler comes into picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Task Scheduler?
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;A Task Scheduler solves this problem using a &quot;to-do list&quot; approach. &lt;/p&gt;

&lt;p&gt;Instead of freezing, the program loops continuously at top speed. &lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;If a task's time is up, the scheduler runs it, and then immediately goes back to checking the clock.&lt;/p&gt;

&lt;p&gt;To understand the internal clock, imagine the microcontroller holding a stopwatch that starts counting milliseconds the moment it powers on. &lt;/p&gt;

&lt;p&gt;In Arduino framework, this stopwatch is read using a function called &lt;code&gt;millis()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of pausing the whole system to wait, the scheduler constantly checks this stopwatch against a simple formula for each task:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Current Time - Last Time Executed &amp;gt;= Interval&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is simple example of how task scheduler works in ESP8266.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight cpp&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Remembers the last time DHT ran&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Remembers the last time MQ2 ran&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// 1000 milliseconds = 1 second&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// 5000 milliseconds = 5 seconds&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Initialization code goes here&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 1. Read the internal stopwatch&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;millis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; 

  &lt;span class=&quot;c1&quot;&gt;// 2. Check the DHT Task &amp;#9201;&amp;#65039;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhtInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;previousDHTTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readTemperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dht&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readHumidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temperature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;humidity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// 3. Check the MQ2 Task &amp;#9201;&amp;#65039;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mq2Interval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;previousMQ2Time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Reset this task's timer&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;analogRead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MQ2_PIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Send data to cloud&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sendDataToCloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2xv1snl09h9dmfk7i8r.png&quot; class=&quot;article-body-image-wrapper&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq2xv1snl09h9dmfk7i8r.png&quot; alt=&quot;&quot; width=&quot;800&quot; height=&quot;534&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way we can perform multiple operations in specific intervals without blocking the loop function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article we have seen how to perform multiple operations in specific intervals without blocking the loop function in ESP8266.&lt;/p&gt;

&lt;p&gt;In next article we will use inbuilt task scheduler and itegrate with painlessmesh.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabn6nl3iehrohehnxgje.png&quot; alt=&quot;git-lrc&quot; width=&quot;800&quot; height=&quot;373&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It&amp;rsquo;s online, source-available, and ready for anyone to use. &lt;br&gt;
&amp;#11088; Star it on GitHub: &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;https://github.com/HexmosTech/git-lrc&lt;/a&gt;&lt;/p&gt;


</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/14077/how-to-perform-multiple-operations-in-the-esp8266</guid>
<pubDate>Fri, 10 Apr 2026 21:18:16 +0000</pubDate>
</item>
</channel>
</rss>