{"id":166859,"date":"2025-03-18T16:47:27","date_gmt":"2025-03-18T16:47:27","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=166859"},"modified":"2025-03-20T15:30:36","modified_gmt":"2025-03-20T15:30:36","slug":"esp32-esp8266-run-daily-task","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-run-daily-task\/","title":{"rendered":"ESP32\/ESP8266: Run Daily Task at Specific Time (Arduino IDE)"},"content":{"rendered":"\n<p>In this guide, we\u2019ll show you how to get date and time using the ESP32 or ESP8266 to run one or multiple tasks every day at an exact time. For this application, your ESP boards will get the time using Network Time Protocol (NTP), so they must be connected to the Internet. The ESP boards will be programmed using Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-Daily-Tasks.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Run Daily Task at Specific Time Arduino IDE\" class=\"wp-image-167014\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-Daily-Tasks.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-Daily-Tasks.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-Daily-Tasks.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-Daily-Tasks.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-Daily-Tasks.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding make sure you have the ESP32 or ESP8266 boards add-on installed in Arduino IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\">Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\">Installing ESP8266 NodeMCU Board in Arduino IDE 2 (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<p>For this tutorial, you only need an ESP32 or an ESP8266 board:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 Board<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP8266 NodeMCU Board<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">NTP (Network Time Protocol)<\/h2>\n\n\n\n<p>To get track of time, we&#8217;ll be using NTP. NTP stands for Network Time Protocol and it is a networking protocol for clock synchronization between computer systems. In other words, it is used to synchronize computer clock times in a network.<\/p>\n\n\n\n<p>There are NTP servers like <em>pool.ntp.org<\/em> that anyone can use to request time as a client. In this case, the ESP32\/ESP8266 is an NTP Client that requests time from an NTP Server (<em>pool.ntp.org<\/em>).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"412\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/ESP32-NTP-Client-Server-Arduino-IDE-How-NTP-works.png?resize=750%2C412&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"NTP Network Time Protocol ESP32 Request time and date\" class=\"wp-image-95124\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/ESP32-NTP-Client-Server-Arduino-IDE-How-NTP-works.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/03\/ESP32-NTP-Client-Server-Arduino-IDE-How-NTP-works.png?resize=300%2C165&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>If you want to learn more about NTP Client-Server interaction with the ESP boards, you can read the following guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-date-time-ntp-client-server-arduino\/\">ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-date-time-ntp-client-server-arduino\/\">ESP8266 NodeMCU NTP Client-Server: Get Date and Time (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>For this guide you&#8217;ll be using the default <span class=\"rnthl rntliteral\">time.h<\/span> library that comes with the Arduino framework so you don&#8217;t need to install additional libraries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32\/ESP8266 Run Daily Task at Specific Time &#8211; Code<\/h2>\n\n\n\n<p>To get date and time with the ESP boards, you don&#8217;t need to install any libraries. You simply include the <span class=\"rnthl rntliteral\">time.h<\/span> library in your code. <\/p>\n\n\n\n<p>The following code gets date and time from the NTP Server, prints the results on the Serial Monitor, and checks if the daily task has run or not.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*  \n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  https:\/\/RandomNerdTutorials.com\/esp32-esp8266-run-daily-task\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n#include &lt;Arduino.h&gt;\n#if defined(ESP32)\n  #include &lt;WiFi.h&gt;\n#elif defined(ESP8266)\n  #include &lt;ESP8266WiFi.h&gt;\n#endif\n#include &lt;time.h&gt;\n\n\/\/ Replace with your network credentials\nconst char* ssid     = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Timezone string for your region, example: https:\/\/github.com\/nayarsystems\/posix_tz_db\/blob\/master\/zones.csv\nconst char* timezone = &quot;WET0WEST,M3.5.0\/1,M10.5.0&quot;; \/\/ WET0WEST,M3.5.0\/1,M10.5.0 = Europe\/Lisbon\n\n\/\/ Time that the daily task runs in 24 hour format\nconst int taskHour = 16;   \/\/ Hour example in 24 hour format: 16 = 4 PM\nconst int taskMinute = 5;  \/\/ 5 minutes\n\n\/\/ Store the day when the task last ran to ensure it only runs once per day\nint lastRunDay = -1;\n\nunsigned long lastNTPUpdate = 0; \/\/ Timestamp for the last NTP sync\nconst unsigned long ntpSyncInterval = 30 * 60 * 1000; \/\/ Sync every 30 minutes (in ms)\n\nvoid syncTime() {\n  Serial.print(&quot;Synchronizing time with NTP server...&quot;);\n  configTime(0, 0, &quot;pool.ntp.org&quot;, &quot;time.nist.gov&quot;); \/\/ UTC offset set to 0\n  time_t now = time(nullptr);\n  while (now &lt; 24 * 3600) { \/\/ Wait until time is valid\n    delay(100);\n    now = time(nullptr);\n  }\n  Serial.println(&quot; Time synchronized!&quot;);\n  \n  \/\/ Set timezone\n  setenv(&quot;TZ&quot;, timezone, 1);\n  tzset();\n\n  lastNTPUpdate = millis(); \/\/ Record the time of the last sync\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  \n  \/\/ Connect to Wi-Fi\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected.&quot;);\n\n  syncTime();\n}\n\nvoid loop() {\n  time_t now = time(nullptr);\n  struct tm timeinfo;\n  localtime_r(&amp;now, &amp;timeinfo);\n\n  \/\/ Current time and date\n  Serial.printf(&quot;Current time: %02d:%02d:%02d, Date: %04d-%02d-%02d\\n&quot;,\n              timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,\n              timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);\n\n  \/\/ Check if it's time to run the daily task\n  if (timeinfo.tm_hour == taskHour &amp;&amp; timeinfo.tm_min == taskMinute &amp;&amp; lastRunDay != timeinfo.tm_mday) {\n    dailyTask();\n    \/\/ Set the day to ensure it only runs once per day\n    lastRunDay = timeinfo.tm_mday;\n  }\n\n  \/\/ Resynchronize with NTP every 30 minutes\n  if (millis() - lastNTPUpdate &gt; ntpSyncInterval) {\n    syncTime();\n  }\n\n  delay(1000); \/\/ Run loop every second\n}\n\nvoid dailyTask() {\n  Serial.println(&quot;#########\\nDoing daily task...\\n#########&quot;);\n  \/\/ ENTER YOUR TASK HERE\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Run_Daily_Task.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Let\u2019s take a quick look at the code to see how it works. First, include the libraries to connect to Wi-Fi and get time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Arduino.h&gt;\n#if defined(ESP32)\n  #include &lt;WiFi.h&gt;\n#elif defined(ESP8266)\n  #include &lt;ESP8266WiFi.h&gt;\n#endif\n#include &lt;time.h&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Setting SSID and Password<\/h3>\n\n\n\n<p>Type your network credentials in the following variables, so that the ESP32\/ESP8266 is able to establish an Internet connection and get date and time from the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Replace with your network credentials\nconst char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Set timezone Variable<\/h3>\n\n\n\n<p>Then, you need to define the <span class=\"rnthl rntliteral\">timezone<\/span> variable to retrieve the correct time for your location.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* timezone = \"WET0WEST,M3.5.0\/1,M10.5.0\";<\/code><\/pre>\n\n\n\n<p>For example, I live in Porto. The time zone is Europe\/Lisbon. From the list of time zone string variables, I see that the time zone string variable for my location is WET0WEST,M3.5.0\/1,M10.5.0. You can check a&nbsp;<a href=\"https:\/\/github.com\/nayarsystems\/posix_tz_db\/blob\/master\/zones.csv\" target=\"_blank\" rel=\"noreferrer noopener\">list of time zone string variables here<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Set the Hour and Minute the Task Runs<\/h3>\n\n\n\n<p>Set the hour and minute you want your daily task to run. The time for the daily task is set in 24-hour format, so the <span class=\"rnthl rntliteral\">taskHour<\/span> set to 16 refers to 4 PM.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int taskHour = 16;\nconst int taskMinute = 5;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other Variables<\/h3>\n\n\n\n<p>Store the day when the task last ran to ensure it only runs once a day:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int lastRunDay = -1;<\/code><\/pre>\n\n\n\n<p>Timestamp for the last NTP sync and how often you want to sync your time with the NTP server:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned long lastNTPUpdate = 0;\nconst unsigned long ntpSyncInterval = 30 * 60 * 1000; \/\/ Sync every 30 minutes (in ms)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span> you initialize the Serial communication at baud rate 115200 to print the results:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>These next lines connect the ESP board to your router to establish an Internet connection.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Connect to Wi-Fi\nSerial.print(\"Connecting to \");\nSerial.println(ssid);\nWiFi.begin(ssid, password);\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(500);\n  Serial.print(\".\");\n}\nSerial.println(\"\");\nSerial.println(\"WiFi connected.\");<\/code><\/pre>\n\n\n\n<p>Finally, call the <span class=\"rnthl rntliteral\">syncTime()<\/span> function to synchronize your board with the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>syncTime();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">syncTime()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">syncTime()<\/span> function connects to the NTP server, retrieves the date and time, and updates the time with the correct time zone defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void syncTime() {\n  Serial.print(\"Synchronizing time with NTP server...\");\n  configTime(0, 0, \"pool.ntp.org\", \"time.nist.gov\"); \/\/ UTC offset set to 0\n  time_t now = time(nullptr);\n  while (now &lt; 24 * 3600) { \/\/ Wait until time is valid\n    delay(100);\n    now = time(nullptr);\n  }\n  Serial.println(\" Time synchronized!\");\n  \n  \/\/ Set timezone\n  setenv(\"TZ\", timezone, 1);\n  tzset();\n\n  lastNTPUpdate = millis(); \/\/ Record the time of the last sync\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">loop()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span> we prepare the time and date variables. We also print the variables in the Serial Monitor for debugging purposes:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>time_t now = time(nullptr);\nstruct tm timeinfo;\nlocaltime_r(&amp;now, &amp;timeinfo);\n\n\/\/ Current time and date\nSerial.printf(\"Current time: %02d:%02d:%02d, Date: %04d-%02d-%02d\\n\",\n                     timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,\n                     timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);<\/code><\/pre>\n\n\n\n<p>This part of the code checks if it&#8217;s the exact time to run the daily task:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (timeinfo.tm_hour == taskHour &amp;&amp; timeinfo.tm_min == taskMinute &amp;&amp; lastRunDay != timeinfo.tm_mday) {\n  dailyTask();\n  \/\/ Set the day to ensure it only runs once per day\n  lastRunDay = timeinfo.tm_mday;\n}<\/code><\/pre>\n\n\n\n<p>Finally, check if 30 minutes have passed, if that&#8217;s true it&#8217;s required to sync the time. We also add a delay to run the loop every second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Resynchronize with NTP every 30 minutes\nif (millis() - lastNTPUpdate &gt; ntpSyncInterval) {\n  syncTime();\n}\n\ndelay(1000);  \/\/ Run loop every second<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">dailyTask()<\/h3>\n\n\n\n<p>You can modify the <span class=\"rnthl rntliteral\">dailyTask()<\/span> function to add the desired code for your project that executes at the specified time:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void dailyTask() {\n  Serial.println(\"#########\\nDoing daily task...\\n#########\");\n  \/\/ ENTER YOUR TASK HERE\n}<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Note:<\/strong> You can apply the same logic using an external RTC module if you don&#8217;t have access to the internet or if you don&#8217;t want to rely on the ESP32\/ESP8266 internal clock. You can use a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-ds3231-real-time-clock-arduino\/\" title=\"\">DS3231 <\/a>or <a href=\"https:\/\/randomnerdtutorials.com\/esp32-ds1307-real-time-clock-rtc-arduino\/\" title=\"\">DS1307<\/a> RTC module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>At the moment the code is only printing a message in the Arduino IDE Serial Monitor when the task runs. You should modify the code to perform any other useful task. Here&#8217;s an example of how it should look like:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"824\" height=\"390\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-NodeMCU-Run-Daily-Task-Arduino-IDE-Demonstration.png?resize=824%2C390&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 NodeMCU Run Daily Task Arduino IDE Demonstration\" class=\"wp-image-166921\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-NodeMCU-Run-Daily-Task-Arduino-IDE-Demonstration.png?w=824&amp;quality=100&amp;strip=all&amp;ssl=1 824w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-NodeMCU-Run-Daily-Task-Arduino-IDE-Demonstration.png?resize=300%2C142&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-NodeMCU-Run-Daily-Task-Arduino-IDE-Demonstration.png?resize=768%2C363&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 824px) 100vw, 824px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">ESP32\/ESP8266 Run Multiple Daily Tasks &#8211; Code<\/h2>\n\n\n\n<p>Based on the previous example, you can duplicate some sections of the code to run multiple daily tasks at a specific time of day. Basically, you add two target times and check whether each task has already run for the current day using two variables. Here&#8217;s the full example:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*  \n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  https:\/\/RandomNerdTutorials.com\/esp32-esp8266-run-daily-task\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n#include &lt;Arduino.h&gt;\n#if defined(ESP32)\n  #include &lt;WiFi.h&gt;\n#elif defined(ESP8266)\n  #include &lt;ESP8266WiFi.h&gt;\n#endif\n#include &lt;time.h&gt;\n\n\/\/ Replace with your network credentials\nconst char* ssid     = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Timezone string for your region, example: https:\/\/github.com\/nayarsystems\/posix_tz_db\/blob\/master\/zones.csv\nconst char* timezone = &quot;WET0WEST,M3.5.0\/1,M10.5.0&quot;; \/\/ WET0WEST,M3.5.0\/1,M10.5.0 = Europe\/Lisbon\n\n\/\/ Time that the daily task runs in 24 hour format\nconst int task1Hour = 8;    \/\/ Task 1 at 8:15 AM\nconst int task1Minute = 15;\n\nconst int task2Hour = 18;   \/\/ Task 2 at 6:45 PM\nconst int task2Minute = 45;\n  \n\/\/ Store the day when the task last ran to ensure it only runs once per day\nint lastRunDayTask1 = -1;\nint lastRunDayTask2 = -1;\n\nunsigned long lastNTPUpdate = 0; \/\/ Timestamp for the last NTP sync\nconst unsigned long ntpSyncInterval = 30 * 60 * 1000; \/\/ Sync every 30 minutes (in ms)\n\nvoid syncTime() {\n  Serial.print(&quot;Synchronizing time with NTP server...&quot;);\n  configTime(0, 0, &quot;pool.ntp.org&quot;, &quot;time.nist.gov&quot;); \/\/ UTC offset set to 0\n  time_t now = time(nullptr);\n  while (now &lt; 24 * 3600) { \/\/ Wait until time is valid\n    delay(100);\n    now = time(nullptr);\n  }\n  Serial.println(&quot; Time synchronized!&quot;);\n  \n  \/\/ Set timezone\n  setenv(&quot;TZ&quot;, timezone, 1);\n  tzset();\n\n  lastNTPUpdate = millis(); \/\/ Record the time of the last sync\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  \n  \/\/ Connect to Wi-Fi\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected.&quot;);\n\n  syncTime();\n}\n\nvoid loop() {\n  time_t now = time(nullptr);\n  struct tm timeinfo;\n  localtime_r(&amp;now, &amp;timeinfo);\n\n  \/\/ Current time and date\n  Serial.printf(&quot;Current time: %02d:%02d:%02d, Date: %04d-%02d-%02d\\n&quot;,\n              timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,\n              timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);\n\n  \/\/ Check if it's time to run the daily task #1\n  if (timeinfo.tm_hour == task1Hour &amp;&amp; timeinfo.tm_min == task1Minute &amp;&amp; lastRunDayTask1 != timeinfo.tm_mday) {\n    dailyTask1();\n    \/\/ Set the day to ensure it only runs once per day\n    lastRunDayTask1 = timeinfo.tm_mday;\n  }\n\n  \/\/ Check if it's time to run the daily task #2\n  if (timeinfo.tm_hour == task2Hour &amp;&amp; timeinfo.tm_min == task2Minute &amp;&amp; lastRunDayTask2 != timeinfo.tm_mday) {\n    dailyTask2();\n    \/\/ Set the day to ensure it only runs once per day\n    lastRunDayTask2 = timeinfo.tm_mday;\n  }\n\n  \/\/ Resynchronize with NTP every 30 minutes\n  if (millis() - lastNTPUpdate &gt; ntpSyncInterval) {\n    syncTime();\n  }\n\n  delay(1000); \/\/ Run loop every second\n}\n\nvoid dailyTask1() {\n  Serial.println(&quot;#########\\nDoing daily task #1...\\n#########&quot;);\n  \/\/ ENTER YOUR TASK HERE\n}\n\nvoid dailyTask2() {\n  Serial.println(&quot;#########\\nDoing daily task #2...\\n#########&quot;);\n  \/\/ ENTER YOUR TASK HERE\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Run_Multiple_Daily_Tasks.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Don&#8217;t forget to insert your network credentials, time zone, desired tasks&#8217; time and modify the dailyTask functions to run your daily task. By default, with this new code, you&#8217;ll have task #1 running at 8:15 AM and task #2 at 6:45 PM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This quick guide taught you how to create a simple code that can run one or multiple tasks with your ESP boards at a desired time.<\/p>\n\n\n\n<p>We hope you found this tutorial useful. We have other tutorials related to time that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-date-time-ntp-client-server-arduino\/\">ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-date-time-ntp-client-server-arduino\/\">ESP8266 NodeMCU NTP Client-Server: Get Date and Time (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ntp-timezones-daylight-saving\/\">ESP32 NTP Time \u2013 Setting Up Timezones and Daylight Saving Time<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-data-logging-temperature-to-microsd-card\/\">ESP32 Data Logging Temperature to MicroSD Card (with NTP time)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ds1307-real-time-clock-rtc-arduino\/\">ESP32: Guide for DS1307 Real Time Clock (RTC) Module (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\"><strong>Build Web Servers with ESP32 and ESP8266 eBook<\/strong><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 tutorials and projects\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thank you for reading.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we\u2019ll show you how to get date and time using the ESP32 or ESP8266 to run one or multiple tasks every day at an exact time. For &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266: Run Daily Task at Specific Time (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-run-daily-task\/#more-166859\" aria-label=\"Read more about ESP32\/ESP8266: Run Daily Task at Specific Time (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":167014,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-166859","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/ESP32-ESP8266-Daily-Tasks.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/166859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=166859"}],"version-history":[{"count":9,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/166859\/revisions"}],"predecessor-version":[{"id":168164,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/166859\/revisions\/168164"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/167014"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=166859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=166859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=166859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}