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.

Thanks!

/*************************************************************
 Slightly Better NTP Client for the ESP8266 v0.1
 Neo www.unix.com 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
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

void setup()
{
    Serial.begin(115200);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }

    timeClient.begin();
}
unsigned long count = 0;
void loop()
{
    count++;
    // 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)
    {
        doTime();
    }
}

void doTime()
{
    timeClient.update();

    Serial.print(daysOfTheWeek[timeClient.getDay()]);
    Serial.print(", ");
    Serial.print(timeClient.getHours());
    if (timeClient.getMinutes() < 10)
    {
        Serial.print(":0");
    }
    else
    {
        Serial.print(":");
    }

    Serial.print(timeClient.getMinutes());

    if (timeClient.getSeconds() < 10)
    {
        Serial.print(":0");
    }
    else
    {
        Serial.print(":");
    }

    Serial.print(timeClient.getSeconds());
    Serial.print(" ");
    Serial.println(MY_TZ_STRING);
    timeset = true;
}
1 Like

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.