Page 1 of 1

RTC in the esp32 off about 2 seconds/day or am I wrong

Posted: Thu Dec 29, 2022 3:22 am
by DOugL-
I wrote a sketch to get ntp time, set localtime and set the RTC then every 10 seconds print the time and compare it with NIST time( time.gov) and I'm seeing the RTC being about 2 seconds faster than NIST. What am I don't wrong or is this normal?

I'm making something for a few friends and I didn't want to require them to have this setup with WiFi and my intention is to set the ESP23 up as and WAP which they connect to and when they load the default page(w/JavaScript) it'll get their time from their phone and via websocket relay it back to the ESP32 which will then set the RTC.

My prototype of this project uses an Arduino Nano, a Bluetooth board and a DS3231 board. I'm hoping to be able to just use the ESP32.

Code: Select all

#include <WiFi.h>
#include "time.h"
//#include <RTClib.h> // From https://github.com/millerlp/RTClib
#include <ESP32Time.h>

const char* ssid       = "mySSID";
const char* password   = "mySSID_PWD";

int8_t iDSTon_off = 0;//1=DST(mar-nov), 0=PST//-8; nov6-mar13 (Nov 6 2a=off, Mar 13 2a=on)
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = -8 * 60 * 60; //3600;
const int   daylightOffset_sec = 0;//3600*iDSTon_off; //3600;

ESP32Time rtc(gmtOffset_sec);  // offset in seconds (GMT+1)*7


void printTime(DateTime now) {
  Serial.print(now.year(), DEC);
  Serial.print(F("-"));
  Serial.print(now.month(), DEC);
  Serial.print(F("-"));
  Serial.print(now.day(), DEC);
  Serial.print(F("  "));
  Serial.print(now.hour(), DEC);
  Serial.print(F(":"));
  if (now.minute() < 10) {
    Serial.print(F("0"));
    Serial.print(now.minute());
   }
  else if (now.minute() >= 10) {
    Serial.print(now.minute());
  }
  Serial.print(F(":"));
  if (now.second() < 10) {
    Serial.print(F("0"));
    Serial.print(now.second());
  }
  else if (now.second() >= 10) {
    Serial.print(now.second());
  }
  Serial.print(F(" "));
} // End of printTime function
void setup()
{
  Serial.begin(115200);
  
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while ((WiFi.status() != WL_CONNECTED)){  
    delay(1500);
    Serial.print(".");   // Print the IP address
  }
  if ((WiFi.status() == WL_CONNECTED)){
    Serial.println("WiFi CONNECTED");   // Print the IP address    
    Serial.println(WiFi.localIP());
  }else
  {
    Serial.println("");
    Serial.println("WiFi NOT CONNECTED, restart in 10 Seconds");
    delay(10000);
    ESP.restart();            // ESP restart
  }
  
  //init and get the NTP time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  // update the RTC with the time aquired from the NTP server
  time_t now;
  time(&now);
  rtc.setTime(now);

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  static bool bOnce = true;
  if( (rtc.getSecond() % 10) == 0 ){
    if( bOnce ) {
      Serial.print(F("cur RTC time="));printTime(rtc.getEpoch());Serial.println();
      bOnce = false;
   }
  }
  else
    bOnce = true;
  //  
  // disabled
  // update NTP time and RTC clock on the hour
  if( rtc.getMinute() == 60 ) { //==59 ) { // **** 60 will never happen  *****
    //init and get the NTP time
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    time_t now;
    time(&now);
    rtc.setTime(now);
    Serial.println("NTP updated and RTC updated");
  }
}