Visit Our UNIX and Linux User Community

A Slightly Better NTP Client for the ESP8266

Thread Tools Search this Thread
Top Forums Programming A Slightly Better NTP Client for the ESP8266
# 1  
Old 01-31-2020
A Slightly Better NTP Client for the ESP8266

Was not really happy with the NTP clients for the ESP8266 because, after a few years of game engine programming, I am not a fan of a lot of code and delays in the main loop, so here is a "slightly better NTP client" for the ESP8266.

In a nutshell, instead of having a delay in the main loop as a part of the main code, I move the main code out of the main loop and use a simply modulo function to set the interval in the main loop. This kind of programming has some advantages, in that it is easier to add more functionality to the main loop. I'm sure they are even better ways to to this with other kinds of timers and interrupts, but this works OK. If you have a better way, please modify and post your improvements.


 Slightly Better NTP Client for the ESP8266 v0.1
 Neo 31 January 2020
 Use anyway the spirit moves you.....
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define MY_TZ_OFFSET_HOURS 7               // manually set the hours offset from UTC as you wish
#define MY_TZ_STRING "Bangkok Time" . // manually set your TZ string, as you like it
const char *ssid = "MY_WIFI_SSID";
const char *password = "MY_COOL_PASSWORD";
bool timeset = false;
const long utcOffsetInSeconds = 3600 * MY_TZ_OFFSET_HOURS;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Define the NTP Client to get time network time
NTPClient timeClient(ntpUDP, "", utcOffsetInSeconds);

void setup()

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED)

unsigned long count = 0;
void loop()
    // My ESP8266 loops at close to 133kHz +/- , so 60 seconds is close to 7900000, give or take a few seconds.
    int my_modulo_delayer = 7900000;
    if (timeset == false or count % my_modulo_delayer == 0)

void doTime()

    Serial.print(", ");
    if (timeClient.getMinutes() < 10)


    if (timeClient.getSeconds() < 10)

    Serial.print(" ");
    timeset = true;

This User Gave Thanks to Neo For This Post:
# 2  
Old 04-10-2020
Hi....You can keep local time with the TimeLib.h library. This runs from the processor's clock. You use NTP time from the network to periodically "true up" your local time. See TimeNTP example in the Time library and NTPClient in the ESP8266WiFi libarary.

Previous Thread | Next Thread
Test Your Knowledge in Computers #80
Difficulty: Easy
Many operating systems today, including Android, Chrome OS, Steam OS, and myriad embedded systems for devices, are based on Linux.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

A Slightly Better NTP Client for the ESP32 (ESPWROOM32) on macOS Catalina

Currently have two ESP8266 modules testing some Blynk apps, whereI'm not so happy with the Blynk business model for developers, but that's another story. So, with two of my ESP8266s currently "busy", I decided to work on the ESP32, and in particular the ESPWROOM32. I installed the... (0 Replies)
Discussion started by: Neo
0 Replies

2. Red Hat

Ntp client sync with local over ntp server

Hi, I have two ntp servers in my cluster and I want all the nodes in my cluster to sync with either of the ntp servers or just one. Unfortunately it keep rotating the sync, between my ntp server 1, ntp server 2 and local. Is there anyway I can change the sync to avoid local? # ntpq -p ... (3 Replies)
Discussion started by: pjeedu2247
3 Replies

3. Solaris

ntp client configuration error

hi all ntp client side configuration file is done but in # ntpq -p remote refid st t when poll reach delay offset disp ============================================================================== ntpserver .INIT. 16 u - 64 0 0.00 0.000... (5 Replies)
Discussion started by: nikhil kasar
5 Replies

4. Solaris

NTP client offset

How to add offset to NTP client so that, for eg., clock is -20 seconds? (2 Replies)
Discussion started by: orange47
2 Replies

5. Solaris

Cannot configure NTP client

Hi, I am trying to configure NTP client on my Solaris server, but I am getting error like: Jan 13 10:53:33 SPOTS ntpdate: no server suitable for synchronization foundMy ntp.conf file: bash-3.00# pwd /etc/inet bash-3.00# bash-3.00# bash-3.00# cat ntp.conf # ident "@(#)ntp.client... (5 Replies)
Discussion started by: nypreH
5 Replies

6. AIX

NTP Client problem

Hello everybody! When I run command "ntpdate" the following error occures "ksh: ntpdate: 0403-006 Execute permission denied. " # oslevel -s 5300-08-06-0918 oslevel -r 5300-08 eny suggestion friends? :-) (3 Replies)
Discussion started by: wwwzviadi
3 Replies

7. Linux

ntp client/daemon sync

All, I need to write a script which can determine if my NTP client is X second off from the NTPD server time. I am looking for a crafty oneliner to calculate the time difference? (3 Replies)
Discussion started by: vada010
3 Replies

8. Fedora

Solaris NTP client

Hello All I have a problem about ntp, I am install a ntp server on a fedora 11 machine and I have 8 ntp client (solaris 10) everything working fine but just a server can not sync time . I have receive this error message -bash-3.00# ntptime ntp_gettime() returns code 5 (ERROR) time... (1 Reply)
Discussion started by: bogacd
1 Replies

9. Solaris

ntp server and ntp client

All, How do you set a Solaris 9 server which received ntp updates from a ntp server to broadcast them on a local subnet. I have created a /etc/inet/ntp.conf file to receive the updates from a server on network and need to make this server become like a ntp relay from the main server. Any... (1 Reply)
Discussion started by: bubba112557
1 Replies

10. Windows & DOS: Issues & Discussions

ntp client windows xp

All, I have a Windows XP client which I need to get time synchronized from a Linux ntp server. What are the commands to perform the setup and configuration? Thanks, Mike (1 Reply)
Discussion started by: bubba112557
1 Replies

Featured Tech Videos