ESP32 NTP Client Ref

#include <WiFI.h>
#include <NTPClient.h.>
#include <WiFiUdp.h>



setpoolservername() setrandomport() begin() update
forceUpdate() isTimeSet() setTimeOffset() getFormattedTime()
getEpochTime() end() example-1 example-2
/*F********************************************************************
*
**********************************************************************/
#include "Arduino.h"
#include <Udp.h>

#define SEVENZYYEARS 2208988800UL
#define NTP_PACKET_SIZE 48
#define NTP_DEFAULT_LOCAL_PORT 1337

class NTPClient {
  private:
    UDP*          _udp;
    bool          _udpSetup       = false;

    const char*   _poolServerName = "pool.ntp.org"; // Default time server
    IPAddress     _poolServerIP;
    unsigned int  _port           = NTP_DEFAULT_LOCAL_PORT;
    long          _timeOffset     = 0;

    unsigned long _updateInterval = 60000;  // In ms

    unsigned long _currentEpoc    = 0;      // In s
    unsigned long _lastUpdate     = 0;      // In ms

    byte          _packetBuffer[NTP_PACKET_SIZE];

    void          sendNTPPacket();

  public:
    NTPClient(UDP& udp);
    NTPClient(UDP& udp, long timeOffset);
    NTPClient(UDP& udp, const char* poolServerName);
    NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
    NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
    NTPClient(UDP& udp, IPAddress poolServerIP);
    NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset);
    NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval);



void setPoolServerName( const char *poolServerName ); Set time server name @param poolServerName
/*F******************************************************************** * **********************************************************************/ void setRandomPort( unsigned int minValue = 49152, unsigned int maxValue = 65535); Set random local port
/*F******************************************************************** * **********************************************************************/ void begin(); Starts underlying UDP client with default local port void begin( unsigned int port ); Starts underlying UDP client with specified local port
/*F******************************************************************** * **********************************************************************/ bool update(); This should be called in main loop of your application. By default an update from NTP Server is only made every 60 seconds. This can be configured in NTPClient constructor. @return true on success, false on failure
/*F******************************************************************** * **********************************************************************/ bool forceUpdate(); This will force update from NTP Server. @return true on success, false on failure
/*F******************************************************************** * **********************************************************************/ bool isTimeSet() const; This allows to check if NTPClient successfully received a NTP packet and set time. @return true if time has been set, else false int getDay() const; int getHours() const; int getMinutes() const; int getSeconds() const;
void setTimeOffset( int timeOffset); Changes time offset (in seconds). Useful for changing timezones dynamically
/*F******************************************************************** * **********************************************************************/ void setUpdateInterval( unsigned long updateInterval); Set update interval to another frequency. E.g. useful when the timeOffset should not be set in constructor
/*F******************************************************************** * **********************************************************************/ String getFormattedTime() const; @return time formatted like `hh:mm:ss`
/*F******************************************************************** * **********************************************************************/ unsigned long getEpochTime() const; @return time in seconds since Jan. 1, 1970
/*F******************************************************************** * **********************************************************************/ void end(); Stops underlying UDP client

Example 1
/*H*****************************************************
Rui Santos
Complete project details at https://randomnerdtutorials.com
Based on NTP Client library example
******************************************************/
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

//************************* DEFINES ************************************
#define  BAUD  9600
const char  *Ssid   = "SSID";
const char  *Pwd    = "PASSWORD";

//************************* PROTOTYPES ************************************

//************************* VARIABLES ************************************
WiFiUDP NtpUdp;
NTPClient TimCLt(  NtpUdp );
String FmttdDat;
String DayStmp;
String TimStmp;

/*F********************************************************************
*
**********************************************************************/
void 
setup( ) 
{
    Serial.begin(  BAUD );                      // INITIALIZE SERIAL MONITOR
    Serial.print(  "Connecting to " );
    Serial.println(  Ssid );
    WiFi.begin(  Ssid, Pwd );
    while(  WiFi.status( ) != WL_CONNECTED ) 
    {
        delay(  500 );
        Serial.print(  "." );
    }
    Serial.println( "" );
    Serial.println( "WiFi connected." );
    Serial.println( "IP address: " );              // PRINT LOCAL IP ADDRESS
    Serial.println( WiFi.localIP() );
    TimCLt.begin();                    // INITIALIZE A NTPClient TO GET TIME
      // SET OFFSET TIME IN SECONDS TO ADJUST FOR YOUR TIMEZONE, FOR EXAMPLE
    // GMT +1 = 3600
    // GMT +8 = 28800
    // GMT -1 = -3600
    // GMT 0 = 0
    TimCLt.setTimeOffset( -6*3600 );                  // US CENTRAL STD TIME 
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
    while(  !TimCLt.update() ) 
        TimCLt.forceUpdate();
     // FmttdDat FORMAT: 2018-05-28T16:00:13Z, NEED TO EXTRACT DATE AND TIME
    FmttdDat = TimCLt.getFormattedDate();
    Serial.println( FmttdDat );
    int splitT = FmttdDat.indexOf( "T" );
    DayStmp = FmttdDat.substring( 0, splitT );               // EXTRACT DATE
    Serial.print( "DATE: " );
    Serial.println( DayStmp );
    // Extract time
    TimStmp = FmttdDat.substring( splitT+1, FmttdDat.length() -1);
    Serial.print( "HOUR: " );
    Serial.println( TimStmp );
    delay( 1000 );
}

Example 2
/*F********************************************************************
*
**********************************************************************/
#include <NTPClient.h>
#include <WiFi.h>                                   // FOR WiFi SHIELD
#include <WiFiUdp.h>

//************************* DEFINES ************************************
#define  BAUD  9600
const char *Ssid     = "";
const char *Pwd      = "";

//************************* PROTOTYPES ************************************

//************************* VARIABLES ************************************
WiFiUDP NtpUDP;
// By default 'pool.ntp.org' is used with 60 seconds update interval and no offset
NTPClient TimClt( NtpUDP );

// You can specify time server pool and offset, (in seconds)
// additionally you can specify update interval (in milliseconds).
// NTPClient TimClt(NtpUdp, "europe.pool.ntp.org", 3600, 60000);

/*F********************************************************************
*
**********************************************************************/
void 
setup()
{
    Serial.begin( BAUD );
    WiFi.begin( Ssid, Pwd );
    while( WiFi.status() != WL_CONNECTED ) 
    {
        delay( 500 );
        Serial.print ( "." );
    }
    TimClt.begin();
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
    TimClt.update();
    Serial.println( TimClt.getFormattedTime() );
    delay( 1000 );
}