Can anyone offer any guidance or point to an example on how I might go about setting a DS3231 RTC using NTP? I'm using an ESP32 Wroom 32D
I thought it would be a straightforward process but it doesn't seem to be turning out like that. I currently don't have any example code of my own at the moment as I'm really struggling to figure out the best method.
The 'getTime' example I'm looking at is the following off the shelf example from the IDE, however I can't figure out how to assign the time variables to the RTC.
#include <WiFi.h>
#include "time.h"
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
void setup()
{
Serial.begin(115200);
//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");
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void loop()
{
delay(1000);
printLocalTime();
}
in your printLocalTime() function you go grab the local time by doing getLocalTime(&timeinfo)
timeinfois a tm structure, and assuming you use Adafruit's library (#include <RTClib.h>) and the RTC is correctly initialized in the setup then you could do
Thanks for the reply, sorry, yes, I'm using the library you mentioned above,
I'll give it a go when I'm back home.
You're right, I won't be doing it often at all, maybe once a day. I know the drift on the DS3231 is very small, but I thought it would be a nice exercise to get it sync'd with the NTP server.
but if you have Wi-Fi you can use the ESP32 time function directly. It will sync up every hour with your NTP server and the external RTC might not be needed
I quite like the redundancy of the RTC, just in case there is a power failure and can't reconnect for whatever reason, then at least the rest of the system will still function and have 'more or less' the correct time - until we can reconnect.
Maybe I could remove it, I'll have to see how things go
no nothing at all. The manufacturer "AVM" who produces the "Fritz!box" is very innovative.
They include more and more useful features in their routers.
phonebook for their DECT-telefones, blocking list, call-handling like forwarding or different ringtones
DECT/and Powerline smarthome-functions and a lot of more things
best regards Stefan
NTP is a network protocol, you can't guarantee the latency over the network, so it would be hard to guarantee you have time accuracy at the ms level or even more at µs level.
I need to add timeinfo h:m:s:ms(with 5 digit) to calculate delay between sender and reciever. Timestamp in serial monitor only can show 3 digit of ms..
Do you have any suggestion how to get 5 digit ms ? Thankyou
Assuming the GPS has received the leap seconds update, the reported seconds will be inline with UTC. Then the rising edge of the 1PPS signal will be within a handful of nS.