| | |
|
|
ESP32 NTP Client Ref
#include <WiFI.h>
#include <NTPClient.h.>
#include <WiFiUdp.h>
|
/*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 );
/*F********************************************************************
* Set time server name @param poolServerName
**********************************************************************/
void
setPoolServerName( const char *poolServerName );
/*F********************************************************************
* Set random local port
**********************************************************************/
void
setRandomPort( unsigned int minValue = 49152, unsigned int maxValue = 65535);
/*F********************************************************************
* Starts underlying UDP client with default local port
**********************************************************************/
void
begin();
/*F********************************************************************
* Starts underlying UDP client with specified local port
**********************************************************************/
void
begin( unsigned int port );
/*F********************************************************************
* 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
**********************************************************************/
bool
update();
/*F********************************************************************
* will force update from NTP Server.
return: true on success, false on failure
**********************************************************************/
bool
forceUpdate();
/*F********************************************************************
* check if NTPClient successfully received a NTP packet and set time.
return: true if time has been set, else false
**********************************************************************/
bool
isTimeSet() const;
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
/*F********************************************************************
* Changes time offset (in seconds). Useful for changing timezones dynamically
**********************************************************************/
void
setTimeOffset( int timeOffset);
/*F********************************************************************
* Set update interval to another frequency. E.g. useful when timeOffset should
not be set in constructor
**********************************************************************/
void
setUpdateInterval( unsigned long updateInterval );
/*F********************************************************************
* return: time formatted like `hh:mm:ss`
**********************************************************************/
String
getFormattedTime() const;
/*F********************************************************************
*getEpochTime returns Unix epoch, which are seconds elapsed since 00:00:00
UTC on 1 January 1970 (leap seconds are ignored, every day is treated as having
86400 seconds). Attention: If you have set a time offset this time offset will
be added to your epoch timestamp.
@return: time in seconds since Jan. 1, 1970
**********************************************************************/
unsigned long
getEpochTime() const;
/*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 );
}