Inaccurate time returned by NTP

Hi everyone, I am making a weather station, which has a feature of displaying time on the LCD.

#include <ESP32Servo.h>
#include <WiFi.h>
#include "time.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x20, 16, 2);

Servo servo_1;

const int button_1_pin = 25; 
int button_1_state = 0;

const int servo_1_pin = 26;

const char* ssid = "NOVA_0A50";
const char* password = "*****";

const char* ntp_server = "at.pool.ntp.org";
const long utc_offset = 28000;
const int daylight_offset = 0;

unsigned long previous_millis = 0;
const long interval = 13000;

void setup() 
{
  Serial.begin(115200);
  servo_1.attach(servo_1_pin);
  pinMode(button_1_pin, INPUT_PULLUP);

  lcd.begin();

  connectWiFi();

  configTime(utc_offset, daylight_offset, ntp_server);
}

void connectWiFi()
{
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  Serial.println("WiFi Connected");
}

void getlocaltime()
{
 struct tm timeinfo;
  if(!getLocalTime(&timeinfo))
  {
    Serial.println("Failed to obtain time");
    return;
  }

  const char* monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

  String formatted_time = String(monthNames[timeinfo.tm_mon]) + " " +
                         String(timeinfo.tm_mday) + " " +
                         String(timeinfo.tm_hour) + ":" +
                         String(timeinfo.tm_min) + ":" +
                         String(timeinfo.tm_sec);

  Serial.print(formatted_time);

  lcd.setCursor(0,0);
  lcd.print(formatted_time);
}

void rotateservo()
{
  button_1_state = digitalRead(button_1_pin);

  if (button_1_state == HIGH)
  {
    Serial.println("Pressed");
    servo_1.write(0);
    delay(1000);
    servo_1.write(90);
  }
}

void loop() 
{
  unsigned long current_millis = millis();
  if (current_millis - previous_millis >= interval) 
  {
    previous_millis = current_millis;
    getlocaltime();
  }

  rotateservo();
}

I am using an ESP32, and it works great, but for whatever reason, the time has around 13 minutes delay to the local time. Is there any problem with my code? Thanks in advance.

You have the offset from UTC set to 28000 seconds, which is 7 hours 46 minutes 40 seconds.

Are you sure that you have set the offset from UTC correctly ?

Maybe the utc_offset?
If the offset is 8 hours, then it should be 28800, not 28000.

Oh, silly me.... it should be 28800... Thanks for the reply

Yeah I made a typo, already fixed it, thanks for the reply

Yeah I am too blind to see it, thanks for the reply

Don't do it this way, there is a better, less error-prone way which also deals with daylight savings time automatically (if you have it in your country).

  configTime(0, 0, ntp_server);  // 0, 0 because we will use TZ in the next line
  setenv("TZ", "XXX", 1);            // Set environment variable with your time zone
  tzset();

You can get the value for XXX for your country/city from here:

For example, my timezone is Europe/London, so I use "GMT0BST,M3.5.0/1,M10.5.0"

Your code shows how it used to be done. You don't need the time.h library, and references to it any more. Because it's now all built into the latest ESP core.
See this page.

  configTime(0, 0, ntp_server);
  setenv("TZ", "XXX", 1);
  tzset();

can be replaced with a single line. This also takes care of daylight savings.

configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "at.pool.ntp.org"); // Austria

Basic sketch to play with.

#include <WiFi.h>  // ESP32
unsigned long prevTime;
time_t now;  // holds epoch
tm tm;       // time struct

void setup() {
  Serial.begin(115200);
  WiFi.begin("NOVA_0A50", "*****");  // WiFi credentials
  configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "at.pool.ntp.org");  // Austria
}

void loop() {
  time(&now);
  if (now != prevTime) {     // if time has changed
    prevTime = now;          // remember
    localtime_r(&now, &tm);  // convert epoch to local time
    printf("%02u:%02u:%02u\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
  }
}

NTP automatically updates every 3 hours.
Leo..

2 Likes