AceTime  0.7
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
Public Member Functions | Static Public Attributes | List of all members
ace_time::clock::NtpClock Class Reference

A Clock that retrieves the time from an NTP server. More...

#include <NtpClock.h>

Inheritance diagram for ace_time::clock::NtpClock:
Inheritance graph
[legend]
Collaboration diagram for ace_time::clock::NtpClock:
Collaboration graph
[legend]

Public Member Functions

 NtpClock (const char *server=kNtpServerName, uint16_t localPort=kLocalPort, uint16_t requestTimeout=kRequestTimeout)
 Constructor. More...
 
void setup (const char *ssid, const char *password)
 Set up using the provided ssid and password. More...
 
const char * getServer () const
 
bool isSetup () const
 
acetime_t getNow () const override
 Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z). More...
 
void sendRequest () const override
 Send a time request asynchronously. More...
 
bool isResponseReady () const override
 Return true if a response is ready. More...
 
acetime_t readResponse () const override
 Returns number of seconds since AceTime epoch (2000-01-01). More...
 
- Public Member Functions inherited from ace_time::clock::Clock
virtual ~Clock ()
 Virtual destructor. More...
 
virtual void setNow (acetime_t)
 Set the time to the indicated seconds. More...
 

Static Public Attributes

static const char kNtpServerName [] = "us.pool.ntp.org"
 Default NTP Server.
 
static const uint16_t kLocalPort = 8888
 Default port used for UDP packets. More...
 
static const uint16_t kRequestTimeout = 1000
 Request time out milliseconds. More...
 
- Static Public Attributes inherited from ace_time::clock::Clock
static const acetime_t kInvalidSeconds = LocalTime::kInvalidSeconds
 

Detailed Description

A Clock that retrieves the time from an NTP server.

This class has the deficiency that the DNS name resolver WiFi.hostByName() is a blocking call. So every now and then, it can take 5-6 seconds for the call to return, blocking everything (e.g. display refresh, button clicks) until it times out.

TODO: Create a version that uses a non-blocking DNS look up.

Borrowed from https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/NTPClient/NTPClient.ino and https://github.com/PaulStoffregen/Time/blob/master/examples/TimeNTP/TimeNTP.ino

Definition at line 43 of file NtpClock.h.

Constructor & Destructor Documentation

◆ NtpClock()

ace_time::clock::NtpClock::NtpClock ( const char *  server = kNtpServerName,
uint16_t  localPort = kLocalPort,
uint16_t  requestTimeout = kRequestTimeout 
)
inlineexplicit

Constructor.

Parameters
ssidwireless SSID
passwordpassword of the SSID
servername of the NTP server (default us.pool.ntp.org)
localPortused by the UDP client (default 8888) requestTimeout milliseconds for a request timesout (default 1000)

Definition at line 62 of file NtpClock.h.

Member Function Documentation

◆ getNow()

acetime_t ace_time::clock::NtpClock::getNow ( ) const
inlineoverridevirtual

Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).

Returns kInvalidSeconds if an error has occured.

This is a blocking call. Some clocks (e.g. NTP client) this may take many seconds. On those clocks, use the asynchronous methods (sendRequest(), isResponseReady(), and readResponse()) instead.

Implements ace_time::clock::Clock.

Definition at line 100 of file NtpClock.h.

◆ isResponseReady()

bool ace_time::clock::NtpClock::isResponseReady ( ) const
inlineoverridevirtual

Return true if a response is ready.

Reimplemented from ace_time::clock::Clock.

Definition at line 132 of file NtpClock.h.

◆ readResponse()

acetime_t ace_time::clock::NtpClock::readResponse ( ) const
inlineoverridevirtual

Returns number of seconds since AceTime epoch (2000-01-01).

Return kInvalidSeconds if there is an error. Valid only if isResponseReady() returns true.

Reimplemented from ace_time::clock::Clock.

Definition at line 137 of file NtpClock.h.

◆ sendRequest()

void ace_time::clock::NtpClock::sendRequest ( ) const
inlineoverridevirtual

Send a time request asynchronously.

Reimplemented from ace_time::clock::Clock.

Definition at line 114 of file NtpClock.h.

◆ setup()

void ace_time::clock::NtpClock::setup ( const char *  ssid,
const char *  password 
)
inline

Set up using the provided ssid and password.

Definition at line 71 of file NtpClock.h.

Member Data Documentation

◆ kLocalPort

const uint16_t ace_time::clock::NtpClock::kLocalPort = 8888
static

Default port used for UDP packets.

Definition at line 49 of file NtpClock.h.

◆ kRequestTimeout

const uint16_t ace_time::clock::NtpClock::kRequestTimeout = 1000
static

Request time out milliseconds.

Definition at line 52 of file NtpClock.h.


The documentation for this class was generated from the following files: