ESP8266 (Web server – part 3): retrieve the time on a time server (NTP) [Exclusive Guide]

2:33 pm March 6, 20175954

in this third tutorial devoted to creating a Web interface for DIY projects using ESP8266, we will learn how to retrieve time from a time server (using the library NtpClientLib ). SPIFFS storage area which equips the majority of the ESP8266 is very convenient to store configuration files or measures. The Arduino and the ESP8266 have no real-time clock. You can add a real time clock ( module RTC Real Time Clock ), or take advantage of the internet connection of the ESP8266 to retrieve the time. Thus, we can easily create a little with a (fairly) accurate time base data acquisition system.

with the ESP8266, you have a permanent internet connection. To be able to get the time from a time server (NTP, Network Time Protocol Server), the ESP8266 must be connected to the internet. Therefore, it is not possible to use it in access point (AP mode). Except to manage an initialization step when you go and get the time.

it is very easy to retrieve the time on the internet using the NTPClientLib Library developed by gmag11 . To work, it will also install and declare the standard TimeLib library for the Arduino. These two libraries are available from the Manager of library of the Arduino IDE.

esp8266 ntp server ntpclientlib timelib arduino

a time libraries installed, there are only to declare in the skit.

the NtpClient library makes available all the methods needed to time management but it uses the TimeLib library to run. You will find many (former) tutorials on the internet that directly decode UDP messages sent by the time server. Everything will work with the Library developed by gmag11, became useless.

before you can recover the time on the internet, we should already start the NTP object. We start with the method begin () which takes 3 parameters in the setup () :

  • the URL of the time server. By default, we can interrogate pool.ntp.org
  • lag that matches your time zone
  • daylight savings time. Set to True to manage mode DST, hours of winter

the setInterval method to set the polling interval of the time server. Keep reasonable , no need to re – synchronize the clock of the ESP8266 10 times per second!  You unnecessarily overloading the NTP server. Here, the time will be re-synchronized every minute (which is already much too).

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

now that everything is ready, we can ask the time any time in turn the Arduino code. The library NtpClientLib offers many features that will help us work in addition to synchronize time with an NTP server:

  • TimeZone (get or set): lag-11 to 13
  • stop (): stop the synchronization of time
  • Interval (get or set): time in milli-seconds before the next time with the NTP server synchronization
  • DayLight (get or set) : summer time management
  • getTimeStr(temps unix): allows to convert directly time unix in chain, convenient for create view
  • getDateStr ()
  • getDateStr(temps unix): string containing the date
  • getTimeDateString (): string containing the date and time
  • getLastNTPSync (): last synchronization

as you can see, there are almost all combinations. Only problem, the chains are not (yet) in an ISO standard. You may experience problems if you treat these dates in the browser (Javascript) code. For the moment, best therefore to keep the returned by the function getTime() unix timestamp. The timestamp (time stamp) for also several advantages:

  • you can easily compare two dates by simple subtraction. The time difference will be in seconds.
  • is a short number, so memory efficient. This is important in an Arduino project / ESP8266.
  • it will be easy to deal with in Javascript code. The new function Date (timestamp) allows to create a date very simply.

there are also some functions to manage the system

  • getUptimeString (): operating time of the ESP8266
  • getUptime (): same as above but in the form of a Unix timestamp
  • [194590[33] getLastBootTime (): time elapsed since the last boot for the ESP8266

  • getFirstSync (): first synchronization with the server since startup

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

here is a small list of other servers in time (in France and in the world). There are hundreds (probably even more!)

http://www.pool.ntp.org/zone/fr , indicate fr.pool.ntp.org

https://services.renater.fr/ntp/serveurs_francais

in the next tutorial, we’ll use this time base to create a history of the measures that will be used in the creation of graphics.