UNO Wifi Rev2: WiFi Module NTP update frequency

Does anyone know the update frequency rate for the UNO WiFi module periodically retreiving NTP timestamps in conjunction with WiFi.getTime()? And is this a parameter that can be changed?

You're using the Time library right? It has a setSyncInterval function that sets how often it syncs to the NTP server. But I also thought you had an RTC. So it depends on which one you have set in setSyncProvider.

I know the link says teensyduino but I swear I got there from arduino.cc
https://www.pjrc.com/teensy/td_libs_Time.html

yes.

Whatever RTC is resident on the UNO Rev2 WiFi.

This is probably what am after then.

Also, I noticed after uploading the UNO that printing out "tnow" from: time_t tnow = now()
is the seconds value since the procesor started after the upload/initialization. Which I can also
use to show uptime I am thinking....

Which one do you have set as the sync provider for the time library?

Post your code.

I don't currently. This is something I need to address.
I have not set 'sync provider' or 'sync interval.'
I thought invoking WiFi.getTime() just pulled off a value locally from the network.

Yes, but now you're getting time through the Time library. That library needs to know where to get the time from. If you don't give it a clock source then it will just use the millis counter which is ok for short duration but isn't terribly accurate over long time spans.

I am confused. I stay confused btw...lol.

Here is code from my ESP32 that I use successfully to reset timers at midnight:

void setTimezone(const char* timezone) {
  Serial.print("Setting Timezone to ");
  Serial.println(timezone);
  setenv("TZ", timezone, 1);
  tzset();
}

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;
  }
  int time_hour = timeinfo.tm_hour;  // tm structure element "hour" from time.h
  int time_min = timeinfo.tm_min;    //                     "minute"  "    "
  int time_sec = timeinfo.tm_sec;    //                      "second"

  if (time_hour == hour_trigger && time_min == min_trigger && time_sec == sec_trigger) {
    Serial.println("Reset counters...");
    counter_reset = true;
    delay(1000);  // used to avoid conflict as time rolls over to new day
  }

  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
}

On the UNO WiFi I thought I could do something different, something more straightforward, as in grab the UNIX epoch time and reset the timers based on seconds since midnight.
Here is that UNO WiFi code [warning it is messy as I have been trying different things before incorporating it into my main code block for the UNO:

#include <SPI.h>
#include <WiFiNINA.h>
// #include <time.h>
#include <TimeLib.h>
#include "arduino_secrets.h"


int reconnects = 0;  // how many times you've reconnected to the network
int lastSecond;      // last second value, for watching passing seconds
unsigned long startTime;
unsigned long epoch;
unsigned long UNIX_epoch;
unsigned long secs_Since_Midnight;

boolean time_set_Flag = false;
boolean read_cycle_time = false;

unsigned long interval = 1000;  // Run LED Blink rate
unsigned long current_Millis = 0;
unsigned long previous_Millis = 0;
unsigned long cycle_Millis = 10000;  // 1 minute unsigned longerval checking epoch time
unsigned long current_cycle_Millis;
unsigned long start_Millis = 0;
setSyncInterval(300);

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  while (!Serial)
    ;
  connectToNetwork();
}

void loop() {
  /*
  if (WiFi.status() != WL_CONNECTED) {   // if you disconnected from the network, reconnect:
    digitalWrite(LED_BUILTIN, LOW);
    connectToNetwork();
  }
  */
  current_Millis = millis();
  current_cycle_Millis = (current_Millis - start_Millis);
  Serial.println();
  Serial.print("Current Cycle Millis =  ");
  Serial.print(current_cycle_Millis);
  Serial.println();

  if (current_cycle_Millis > cycle_Millis) {
       start_Millis = current_Millis;
  Serial.println();
  Serial.print("[IF] Current Cycle Millis =  ");
  Serial.print(current_cycle_Millis);
  Serial.println();
    // read_cycle_time = true;
    UNIX_epoch = WiFi.getTime();
   secs_Since_Midnight = (UNIX_epoch - 21600) % 86400;
    // read_cycle_time = false;
    Serial.println();
    Serial.print("IF: UNIX_epoch = ");
    Serial.println(UNIX_epoch);
    Serial.println();
    Serial.print("Seconds since midnight = ");
    Serial.print(secs_Since_Midnight);
    Serial.println();
    Serial.print("...end of IF loop....");
  }
  /*} 
   else (read_cycle_time = false);

  if (read_cycle_time == true) {          
    UNIX_epoch = WiFi.getTime();
    // set_system_time( (time_t) UNIX_epoch );
    set_system_time(UNIX_epoch);
    read_cycle_time = false;
    Serial.println();
    Serial.print("System Time has been set!");
    Serial.println();
  }
  */

  time_t tnow = now();
  //time_t tnow;         removed 9/10 for testing other time statement
  // struct tm lt;
  // time(&tnow);          removed 9/10 for testing other time statement

  Serial.print("tnow:  ");
  Serial.println(tnow);
  Serial.println();
  //  Serial.println(tnow);        removed 9/10 for testing other time statement

  /*

  secs_Since_Midnight = (UNIX_epoch - 21600) % 86400;
  Serial.println();
  Serial.print("void loop : UNIX_epoch time =  ");
  Serial.println(UNIX_epoch);
  delay(2000);
  Serial.println();
  Serial.print(" void loop: Seconds since midnight: ");
  Serial.println(secs_Since_Midnight);
  delay(2000);

  if (secs_Since_Midnight >= 55800) {
    Serial.println();
    Serial.println("............secs_Since_Midnight value triggered!!!");
    delay(2000);
  }
*/

  //  if (secs_Since_Midnight => 86398) {
  // reset eqipment runtime counter:
  //   blr_daily_run_Millis = 0;
  //   blr_on_stop_millis   = 0;
  //   blr_daily_run_hrs    = 0;
  //   latest_on_cycle      = 0;
  delay(3000);
}

void connectToNetwork() {
  // try to connect to the network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Attempting to connect to: " + String(SECRET_SSID));
    //Connect to WPA / WPA2 network:
    WiFi.begin(SECRET_SSID, SECRET_PASS);
    delay(2000);
  }
  Serial.println("connected to: " + String(SECRET_SSID));
  // You're connected, turn on the LED:
  digitalWrite(LED_BUILTIN, HIGH);
  // set the time from the network:
  //  unsigned long epoch;
  //  unsigned long secs_Since_Midnight;
  do {
    Serial.println("Attempting to get network time");
    epoch = WiFi.getTime();
    delay(10000);
  } while (epoch == 0);

  startTime = epoch;
   secs_Since_Midnight = (epoch % 86400) - 21600;
  // secs_Since_Midnight = (epoch - 21600) % 86400;  // 86400: seconds in each day; 21600: minus 6 hours [CST]
  Serial.println();
  Serial.print(" First retreival of Seconds since midnight: ");
  Serial.println(secs_Since_Midnight);

  // increment the reconnect count:
  reconnects++;
  Serial.println();
  Serial.print(" First retreival of network time is: ");
  Serial.println(startTime);
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
  Serial.print("  Signal Strength: ");
  Serial.println(WiFi.RSSI());
}

I've tried to follow you through several threads now. I still don't understand what you want to "reset". You don't have to reset a clock. It rolls past midnight on its own every night.

Can you explain in words what this project is doing and why it needs this nightly clock reset?

Not much use if your problem is with an UNO Rev2 WiFi. It's already been requested once. We'll try one time ... post your code (the one that's germane to your question).

I monitor accumulated runtime of equipment based on a 24 hour cycle beginning at midnight.
At any time during the cycle I can observe the number of on/off cycles and the total amount of time pump X has run since midnight. That's it.

#include <SPI.h>
#include <WiFiNINA.h>
// #include <time.h>
#include <TimeLib.h>
#include "arduino_secrets.h"


int reconnects = 0;  // how many times you've reconnected to the network
int lastSecond;      // last second value, for watching passing seconds
unsigned long startTime;
unsigned long epoch;
unsigned long UNIX_epoch;
unsigned long secs_Since_Midnight;

boolean time_set_Flag = false;
boolean read_cycle_time = false;

unsigned long interval = 1000;  // Run LED Blink rate
unsigned long current_Millis = 0;
unsigned long previous_Millis = 0;
unsigned long cycle_Millis = 10000;  // 1 minute unsigned longerval checking epoch time
unsigned long current_cycle_Millis;
unsigned long start_Millis = 0;

void setSyncInterval(time_t interval);
// void    setSyncInterval(time_t interval);

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  while (!Serial)
    ;
  connectToNetwork();
}

void loop() {
  /*
  if (WiFi.status() != WL_CONNECTED) {   // if you disconnected from the network, reconnect:
    digitalWrite(LED_BUILTIN, LOW);
    connectToNetwork();
  }
  */
  current_Millis = millis();
  current_cycle_Millis = (current_Millis - start_Millis);
  Serial.println();
  Serial.print("Current Cycle Millis =  ");
  Serial.print(current_cycle_Millis);
  Serial.println();

  if (current_cycle_Millis > cycle_Millis) {
       start_Millis = current_Millis;
  Serial.println();
  Serial.print("[IF] Current Cycle Millis =  ");
  Serial.print(current_cycle_Millis);
  Serial.println();
    // read_cycle_time = true;
    UNIX_epoch = WiFi.getTime();
   secs_Since_Midnight = (UNIX_epoch - 21600) % 86400;
    // read_cycle_time = false;
    Serial.println();
    Serial.print("IF: UNIX_epoch = ");
    Serial.println(UNIX_epoch);
    Serial.println();
    Serial.print("Seconds since midnight = ");
    Serial.print(secs_Since_Midnight);
    Serial.println();
    Serial.print("...end of IF loop....");
  }
  /*} 
   else (read_cycle_time = false);

  if (read_cycle_time == true) {          
    UNIX_epoch = WiFi.getTime();
    // set_system_time( (time_t) UNIX_epoch );
    set_system_time(UNIX_epoch);
    read_cycle_time = false;
    Serial.println();
    Serial.print("System Time has been set!");
    Serial.println();
  }
  */

  time_t tnow = now();
  //time_t tnow;         removed 9/10 for testing other time statement
  // struct tm lt;
  // time(&tnow);          removed 9/10 for testing other time statement

  Serial.print("tnow:  ");
  Serial.println(tnow);
  Serial.println();
  //  Serial.println(tnow);        removed 9/10 for testing other time statement

  /*

  secs_Since_Midnight = (UNIX_epoch - 21600) % 86400;
  Serial.println();
  Serial.print("void loop : UNIX_epoch time =  ");
  Serial.println(UNIX_epoch);
  delay(2000);
  Serial.println();
  Serial.print(" void loop: Seconds since midnight: ");
  Serial.println(secs_Since_Midnight);
  delay(2000);

  if (secs_Since_Midnight >= 55800) {
    Serial.println();
    Serial.println("............secs_Since_Midnight value triggered!!!");
    delay(2000);
  }
*/

  //  if (secs_Since_Midnight => 86398) {
  // reset eqipment runtime counter:
  //   blr_daily_run_Millis = 0;
  //   blr_on_stop_millis   = 0;
  //   blr_daily_run_hrs    = 0;
  //   latest_on_cycle      = 0;
  delay(3000);
}

void connectToNetwork() {
  // try to connect to the network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Attempting to connect to: " + String(SECRET_SSID));
    //Connect to WPA / WPA2 network:
    WiFi.begin(SECRET_SSID, SECRET_PASS);
    delay(2000);
  }
  Serial.println("connected to: " + String(SECRET_SSID));
  // You're connected, turn on the LED:
  digitalWrite(LED_BUILTIN, HIGH);
  // set the time from the network:
  //  unsigned long epoch;
  //  unsigned long secs_Since_Midnight;
  do {
    Serial.println("Attempting to get network time");
    epoch = WiFi.getTime();
    delay(10000);
  } while (epoch == 0);

  startTime = epoch;
   secs_Since_Midnight = (epoch % 86400) - 21600;
  // secs_Since_Midnight = (epoch - 21600) % 86400;  // 86400: seconds in each day; 21600: minus 6 hours [CST]
  Serial.println();
  Serial.print(" First retreival of Seconds since midnight: ");
  Serial.println(secs_Since_Midnight);

  // increment the reconnect count:
  reconnects++;
  Serial.println();
  Serial.print(" First retreival of network time is: ");
  Serial.println(startTime);
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
  Serial.print("  Signal Strength: ");
  Serial.println(WiFi.RSSI());
}

And what is the correct syntax for:

void setSyncInterval(time_t interval);

Is void necessary? The only way it compiles w/o an error is exactly like the code line above. I am assuming you then define 'interval' for your sync time period.

Ok, counters. You're resetting counters that count runtime. That was my confusion, I thought you wanted to reset the time.

So all you need to know is when it's midnight right?

The board you are using has an RTC. I would investigate that library. I bet it has an alarm function.

Right now you've got code using two different clocks.

Does that get the time? When that rolls over to 0 it's midnight. Time to reset the counters.

That's also getting unix time assuming that you've told Time where to get time from and set the time in the library. Time library is really great for handling time, so even with an RTC to keep time it's often beneficial to include TimeLib.h to handle printing and breaking down into hours minutes seconds and such.

But you need to look at the docs and examples for the Time library. There are some things you have to set up.

The thing with 'tnow' currently is that it shows the number of secs since the latest upload/initialization of the processor. That may be because I am not telling it to use the requisite 'sync provider.'

I can work with epoch time and get what I need. My only concern was how often the UNO was going out to get a NTP timestamp, if it even is.
The concern being I have limited data on my internet access and I want to limit the NTP requests to once a day.
I appreciate your explanation about using two different clock sources as I was not grasping the subject correctly.

And you haven't set the time either. There's a method for that in one of the examples.

Only when you tell it to.

Use the RTC and do them one a week.

I'll tell you what I'd do, but I don't know all the code. It's all example code though that should be easy to find.

I would get NTP time once in setup. I would use that time to set the RTC. If the RTC has an alarm function, set it for midnight and be done.

If it doesn't have an alarm function, then add the Time library. Set the sync provider to a function that just gets time from the RTC. Trigger a sync so it will get the initial time from the RTC. Then periodically check unix time mod 86400 to see if it has been midnight. When the new value is less than the old value then you rolled past midnight.

As for NTP time, since you have an RTC you're don't really have to be that worried about it. Just add code that whenever you have an internet connection it checks NTP time and compares to the RTC time. If the RTC is off then set it to the proper time.

Your suggestion of "what I'd do" makes sense. My question is concerning the EXAMPLES you reference. Are they found in the IDE Examples or on the web at someplace like GitHub?

Documentation for whatever. For the Time library: Time - Arduino Reference

There are links to the documentation and the repository on github.

If you have the library installed, then they will also be in the examples in the IDE.

For the RTC I would expect there to be some examples with the IDE.

Looking in the IDE Time.h library examples there is a TimeRTCSet.ino
example. The thing is it #includes a <DS1307RTC.h> reference.
I think this is a separate module you can attach to the UNO.
The code compiles though so I may see if this is the path to set the
Wifi Rev2 RTC. I have found nothing specific for the Rev2 yet.

ok so the thing about examples is you have to focus on the part that it's exampling.

That example is written for someone with a DS1307 external RTC. You'll have to find the commands for the onboard RTC that do the same thing. That will be in a different example. The part you want to pay attention to in the Time library examples are how they use the Time library functions.

When you look at the examples for the RTC. They will probably just print out the time. But you need to give it to the Time library. So you'll have to look at the RTC functions in that example and sort of ignore the rest.

That's the thing with examples. They just show you how some functions work, but they're pretty useless on their own. You have to just focus on seeing how they use their functions.

Whatever happened to to the Examples Listing that included
the one titled "Hey Ed! This is the one you definitely need to look at." :upside_down_face:
That TimeRTCSet did not work, no surprise.
I will continue to look at the examples and try to find that bit of code
that works with the Rev2.
I know enough about UNIX epoch I can limp along using it but ultimately retreivng the once a day timestamp and updating the onboard RTC is the goal. Hope springs eternal.

If you had an UNOR4 I'd probably be more help in doing the digging. That's the board I've been focused on learning lately.

You probably won't find the piece of code that does exactly what you want. You need to learn a few basic things in pieces.

How to obtain a NTP timestamp. Doesn't need to be automatic. Just how to get the time.

How to set the RTC. Whatever example is going to be getting the time somewhere else. Focus on the part that puts the time into the RTC. It's probably one line that takes a unix time as an argument.

Does the RTC have an alarm function? I bet it does. If it does then you can stop here.

How to read the RTC. Same story, just focus on the line that gets a unix time out. That's what syncprovider needs. Again it's probably one line that returns a time stamp.

Once you know how to do those basic steps on your own, then the rest will fall into place.