ESP8266 (Web Server – Part 3): How to recover time on a time server (NTP)

In this third tutorial on creating a web interface for DIY projects based on ESP8266, we will learn how to retrieve the time from a time server (using the NtpClientLib library). The SPIFFS storage area that equips the majority of ESP8266 is very convenient for storing configuration files or measurements. The Arduino and the ESP8266 do not have a real-time clock. One can add a real time clock (RTC module, Real Time Clock), or take advantage of the Internet connection of the ESP8266 to recover the time. It will thus be possible to easily create a small data acquisition system with a (quite) precise time base.

Install the NTPClientLib and Time libraries.

With the ESP8266, we have a permanent internet connection. To be able to recover time from a time server (NTP server, Network Time Protocol), the ESP8266 must be connected to the Internet. It is therefore not possible to use it as an access point (AP mode). Except to handle an initialization step during which you will recover the time.

It is very easy to recover time on the Internet using the library NTPClientLib developed by gmag11. To operate, you will also need to install and declare the standard Arduino TimeLib library. These two libraries are available from the Arduino IDE library manager.

esp8266 ntp server ntpclientlib timelib arduino

Once the libraries are installed, it remains only to declare them in the sketch.

The NtpClient library provides all the methods necessary for time management, but it uses the TimeLib library to function. You will find many (old) tutorials on the internet that directly decode the UDP messages sent by the time server. With the library developed by gmag11, everything works becomes useless.

Before you can retrieve the time on the Internet, you must already start the NTP object. It is started with the begin() method which takes 3 parameters in the setup():

  • The time server URL. By default, you can query pool.ntp.org
  • Time difference that matches your time zone
  • Summer time. Set to True to manage daylight saving mode, winters

The setInterval method is used to set the polling frequency of the time server. Stay reasonable, unnecessary to re-synchronize the clock of the ESP8266 10 times per second! You will unnecessarily overload the NTP server. Here, the time will be re-synchronized every minute (which is already too much).

You can create a callback function that will be called whenever an event occurs (inaccessible server, bad URL, time request).

Now that everything is ready, one can ask for time at any time in turn the Arduino code. The NtpClientLib library offers many functions that will make our work easier, in addition to synchronizing time with an NTP server:

  • TimeZone(get or set): time offset -11 to + 13
  • Stop(): stops time synchronization
  • Interval(get or set): time in milliseconds before the next time synchronization with the NTP server
  • DayLight(get or set): daylight saving time management
  • GetTimeStr(unix time): directly converts unix time to string, convenient to create a display
  • GetDateStr()
  • GetDateStr(unix time): string containing the date
  • GetTimeDateString(): string containing the date and time
  • GetLastNTPSync(): last sync

As you can see, there are almost all combinations. Only problem, the chains are not (yet) in an ISO standard. You may encounter problems if you have to process these dates in browser code (Javascript). For now, the best is to keep the unix timestamp returned by the getTime () function. The timestamp also has several advantages:

Two dates can easily be compared by a simple subtraction. The time difference will be in seconds.
It is a fairly short number, therefore economical in memory. This is important in an Arduino / ESP8266 project.
It will be easy to process in Javascript code. The new date (timestamp) function allows to create a date very simply.
There are also some handy functions for managing the system

  • GetUptimeString(): running time of the ESP8266
  • GetUptime(): idem but in the form of a Unix timestamp
  • GetLastBootTime(): elapsed time since the last ESP8266 boot
  • GetFirstSync(): first synchronization with the server since startup

What is Unix time (Unix timestamp)?

The unix time started on 1 January 1970 at midnight. It is a counter in seconds. You can directly convert a unix time into the Arduino code to deduce a date or time (or all) using the getTimeStr (unix time) and getDateStr (unix time) functions. There are also many converters on the internet (http://www.unixtimestamp.com/).

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

  • Samizard

    Hi, can you tell me how do i install that NTP library in arduino, since it not showing in board managers and even after copy-pasting the library in libraries folder the NTP header file shows error.
    Can you please tell me solution for this??

    • Hello Samizard. Which version of the IDE Arduino do you use. I just checked on mine (version 1.8.2) and I find it well in the list. I compiled this little piece of code without error. It’s strange.
      #include
      #include

      void setup() {
      NTP.begin(“pool.ntp.org”, 1, true);
      NTP.setInterval(60);
      }

      void loop() {
      Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
      delay(60000);
      }

      • Samizard

        version 1.8.5

DIY Projects