I am using the function getTime() to acquire the UNIX epoch time off
the local network and then use function setTime() to load the UNO WiFi Rev2 RTC. This is accomplished in the void setup() section. My concern regards reinitialization after a power outage. It takes several minutes for my LAN router to start back up after losing power. If there is a problem securing the local network timestamp by the UNO I do not want it sit in a while loop for an extended period if getTime() requests are going unanswered. I would rather it continue on to the void loop() without the RTC being set.
Any suggestions on how to handle this contingency?
#include <SPI.h>
#include <WiFiNINA.h>
#include <TimeLib.h>
#include "arduino_secrets.h"
int reconnects = 0; // how many times you've reconnected to the network
unsigned long epoch;
unsigned long actualTime;
unsigned long oldTime;
void setup() {
Serial.begin(115200);
connectToNetwork();
do {
Serial.println("Attempting to get network time");
epoch = WiFi.getTime();
delay(5000);
} while (epoch == 0);
int hr = ((epoch - 18000) % 86400L) / 3600;
int min = (epoch % 3600) / 60;
int sec = (epoch % 60);
setTime(hr, min, sec, 1, 1, 1); // set UNO RTC; day, month, year not needed
}
void loop() {
actualTime = now(); // get actual system timestamp
if (actualTime != oldTime) { // make actions if flag is fired:
oldTime = actualTime;
Serial.println();
Serial.print("RTC: Hour = ");
Serial.print(hour(actualTime));
Serial.print("/ Minute = ");
Serial.print(minute(actualTime));
Serial.print("/ Second = ");
Serial.println(second(actualTime));
int HOUR = hour(actualTime);
int MINUTE = minute(actualTime);
int SECOND = second(actualTime);
if (HOUR == 23 && MINUTE == 59 && SECOND >= 58) {
// set runtime counters to zero
Serial.println();
Serial.println("Midnight is upon us. Reset counters. ");
} else {
Serial.println();
Serial.println("Not time yet. ");
}
delay(2000); // wait on system time to rollover
}
}
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));
IPAddress ip = WiFi.localIP();
Serial.println(ip);
Serial.print(" Signal Strength: ");
Serial.println(WiFi.RSSI());
// increment the reconnect count:
reconnects++;
}
I do a similar thing but I use a RTC module with a battery so it keeps great time. Every day or so I check it against NTP and so far the worse has been about one second.
It was my understanding the WiFi Rev2 has an onboard crystal/ Real Time Clock. Not just a counter.
What does it mean when you say "setting the time library?" How is the time library utilized as a
clock in essence?
It matters not I suppose as long as the setTime() function successfully executes. I just do not want the "do - while" loop of the getTime function getting stuck and hanging the processor up.
You could use it a a clock source as an alternative to the millis() based on the system timer but the accuracy of the two methods would need to be tested.
I think you are just fine using setTime() with the TimeLibrary. If the accuracy is not good enough over long periods, you can set the TimeLibrary synchProvider to use the time you get from WiFi.getTime().
That is a good idea. I am displaying the RTC [Real Time Counter now instead of Real Time Clock] on my webpage so I can track it fairly easily. The sync function could become handy at
some point.
I do not really see the advantage of wandering off the main path of using the millis() timer with network updates unless you know what you are doing. I do not think the RTC registers are persistent through a power cessation.
If power failures and time keeping is the main issue, then the DS3231 is the answer.