ESP8266 timeClient Daily Update

Hi, I'm working on the following code to get NTP from server by using ESP8266, currently it gets time every second, however I need to do it once a day.

Should I enter updateInterval as 86.400.000 which is 1 day in miliseconds? I guess that would not be the best way to do it.

What would be your recommendation to run it once a day?

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>


const char *ssid     = "SSID";
const char *password = "Pass";
const long utcOffsetInSeconds = 14400;  //Dubai time zone is GMT+4 = 4*60*60 = 14400seconds difference
const long updateInterval =60000; 
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

String formattedDate;
String dayStamp;
String timeStamp;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds, updateInterval);

void setup(){
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}

void loop() {


  timeClient.update();
 
  
  formattedDate = timeClient.getFormattedDate();    //Get the date and time in following format 2019-09-01T23:56:36Z
  

  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);      //This code will take characters from first(zero) till splitT(Which is until find letter T)

  
  timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);  //This code will take characters from the one after T till the one before the last one(last one is Z)
  Serial.println(timeStamp);                          //Return will be like 23:56:35

  delay(1000);
}

An unsigned long can hold that number so I would be tempted to do it that way but using millis() and not delay(...).

You could use a Real Time Clock but it would be excessive for just this purpose.

By the way, an Arduino does not keep accurate time but it should be good enough for this purpose.

delay(...) is handy but it is VERY hard to do anything else at the same time. millis() is more challenging but makes it easy to do other things. There is a great millis() tutorial at the top of this forum.

Juraj:
Arduino/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino at master · esp8266/Arduino · GitHub

When you use ESP built in time library with automatic NTP update support, (running in the background) and automatic DST adjustments,
definitely do not use time offsets - which is what is used in this example. The timezone offset stuff is horribly broken.
You must set the timezone offsets to ZERO and use a TZ string instead.

The person who implemented the ESP library ntp functions had no clue how time_t values and timezones are supposed to work.

See this github issue: https://github.com/esp8266/Arduino/issues/4637

IIRC, the background NTP time updates occur every hour automatically in the background.

When using things like NTP updates, the tricky part is making sure the network comes up and they actually run.
There are many edge conditions that have to be accounted for.
For example, during a power failure, the ESP will likely reboot faster than the WiFi router or its interconnect connect (like a cable modem)
If the application code has the ESP fall into a accesspoint if there is no client connection (like if WiFi manager is used) it may fall into access point mode even though everything is already configured properly.

There are many scenarios like this that have to be accounted for and often require some code in the application to attempt to reconnect to the network every so often if there is no active WiFi connection.

It gets more complicated if you have a device that must operate even if there is no WiFi or internet connection.

--- bill