Clarification on printLocalTime & initTime functions [SOLVED]

I just need some verification. I run initTime inside void setup() and printLocal Time
inside void loop(). It is only initTime that actually requests an update from a NTP server?
printLocalTime is just accessing the data previously retreived by initTime? I ask because I do not want to unwittingly have the processor doing constant NTP requests as that is considered an abuse of time servers.

void initTime(const char* timezone) {
  struct tm timeinfo;

  Serial.println("Getting time from NTP server");
  configTime(0, 0, "pool.ntp.org");  // First connect to NTP server, use 0 TZ offset
  if (!getLocalTime(&timeinfo)) {
    Serial.println("  Failed to obtain time");
    return;
  }
  Serial.println("OK, Got the time from NTP");
  setTimezone(timezone);  // then set your timezone
}

void printLocalTime() {  // this function monitors current time of day to initiate
                         // pump counter reset at midnight
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
#include <WiFi.h>
#include "time.h"
#include "sntp.h"

const char* ssid       = "YOUR_SSID";
const char* password   = "YOUR_PASS";

const char* ntpServer1 = "pool.ntp.org";
const char* ntpServer2 = "time.nist.gov";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

const char* time_zone = "CET-1CEST,M3.5.0,M10.5.0/3";  // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("No time available (yet)");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

// Callback function (get's called when time adjusts via NTP)
void timeavailable(struct timeval *t)
{
  Serial.println("Got time adjustment from NTP!");
  printLocalTime();
}

void setup()
{
  Serial.begin(115200);

  // set notification call-back function
  sntp_set_time_sync_notification_cb( timeavailable );

  /**
   * NTP server address could be aquired via DHCP,
   *
   * NOTE: This call should be made BEFORE esp32 aquires IP address via DHCP,
   * otherwise SNTP option 42 would be rejected by default.
   * NOTE: configTime() function call if made AFTER DHCP-client run
   * will OVERRIDE aquired NTP server address
   */
  sntp_servermode_dhcp(1);    // (optional)

  /**
   * This will set configured ntp servers and constant TimeZone/daylightOffset
   * should be OK if your time zone does not need to adjust daylightOffset twice a year,
   * in such a case time adjustment won't be handled automagicaly.
   */
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

  /**
   * A more convenient approach to handle TimeZones with daylightOffset 
   * would be to specify a environmnet variable with TimeZone definition including daylight adjustmnet rules.
   * A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
   */
  //configTzTime(time_zone, ntpServer1, ntpServer2);

  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");

}

void loop()
{
  delay(5000);
  printLocalTime();     // it will take some time to sync time :)
}


Seems like the time libraries that come installed with the ESP8266/32 Arduino cores.

No, the updates from the NTP server happen on a regular schedule in the background. I think by default that is once every hour, but it can be changed. In between those updates, the ESP's internal clock is used. The internal clock isn't very accurate and will drift, hence the need to re-sync with the NTP server regularly.

With the Arduino Uno Wifi Rev2 I am using TimeLib.h

With the ESP its time.h

My question was hopefully generic in that there are probably differences
in how the same goal is actually accomplished between the two processors.

I just want to avoid contacting the time servers more than a couple times a day but
was unclear if printLocalTime was doing the same as initTime in scope.

Do you know where to access this area to modify the update interval?

I answered your question in POST EIGHT !!!!!

Or maybe I didn't... ???

For ESP32, nearly all the prototypes for the NTP APIs are in esp_sntp.h. Also see the System Time Reference:
https://docs.espressif.com/projects/esp-idf/en/release-v4.4/esp32/api-reference/system/system_time.html

It seem obvious that the implementation may be platform-dependent. That's why you need to be specific.

Understood.

your Espressif link was a good share.

Is there a good place on the web where a neophyte like myself can learn the distinctions between things like initTime, printLocalTime, actualTime, getLocalTime, and so on?
I understand the basic process of requesting timestamps from time servers but then it gets
into a plethora of different approaches and ways to get time updates and I realize they are not indentical across platforms.
My goal is it get an initial timestamp when the processor first cranks up and then periodic updates every couple hours or so. I want to make sure I am not continually requesting NTP updates inside void loop() as this is considered a violation of internet rules, for good reason. And now that I starting to use structures in my code timestamping takes on a whole new importance.

Please see post 14! My responses are not always being tied to the member being addressed.

Not trying to. Chalk it up to dealing with someone who is genetically obtuse.

As to the library question I am going to say for the ESP I am using the Time Library by Margolis 1.6.1 from the IDE Manager.

And I think TimeLib.h with the UNO also comes from the Time Library by Margolis.

Is it normally easy to backtrace what library files you are referencing by what is used in the #include definition? The only search result that comes up with the TimeLib search in Library Manager is the Time library by Margolis.
The Github link:
https://github.com/PaulStoffregen/Time/blob/master/TimeLib.h
Has it listed under Time so I am thinking they are the same or related. I have forgot the distinction of TimeLib versus Time as they were installed way back when.

Another important point I need to realize.....

see post 17....

I am working with forum staff to figure out why the forum member attachment is not being applied when I select the Reply button in the message box as opposed to the Reply button at the bottom of the thread. Sometines it attaches correctly but usually it does not. ???

Here's a useful link

https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm

@noiasca knows more than me about this subject.

2 Likes

... I just don't know what question wasn't answered up to now for the ESPs.

This is a very similar page but specific for the ESP32:
https://werner.rothschopf.net/microcontroller/202103_arduino_esp32_ntp_en.htm

1 Like

@edthewino what questions remain unanswered, after you read the various links shared so far?

No, you don't need that!!! The System Time API Documentation that I linked in Post #12 is all you need to handle time and NTP on an ESP32. I comes as part of the ESP32 package. All you have to to do is include the sntp.h header. Don't add any other time libraries as you may end up with conflicting function definitions.

Also, be very careful about you're terminology. In a previous post in this thread, you claimed to be using an Uno Wifi Rev2. That's a very much different board than what most people think of when you say just "Uno". Again, as everyone is saying, you must be clear and specific.

1 Like

I am playing catchup right now with the various suggested links to read. I will wade through this and then decide if i am still needing some more assistance. You guys have supplied me with lots of good general knowledge and links. :+1:

Rut row! I need to regroup after you hit me with that little tidbit of information.

1 step forward, 2 steps backwards... lol

thanx for trying to keep me in line....

OK, between gfvalvo's : "The System Time API Documentation" link over at docs.Espressif
and

PaulRB and nolasca's link to:

NTP for the ESP8266 including day light saving time (DST) without 3rd party library

I have plenty of good info to redo my ESP code.

Thanks to everyone for all your suggestions! :+1: :+1: