AceTimeClock  1.3.0
Clock classes for Arduino that can synchronize from an NTP server or an RTC chip
Public Member Functions | Static Public Attributes | List of all members
ace_time::clock::EspSntpClock Class Reference

An implementation of Clock that configures the built-in SNTP client on the ESP8266 and ESP32 using the configTime() function using UTC timezone (no STD or DST offset), and uses the C-library time() function as the reference clock. More...

#include <EspSntpClock.h>

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

Public Member Functions

bool setup (const char *ntpServer=kDefaultNtpServer, uint32_t timeoutMillis=kDefaultTimeoutMillis)
 Setup the SNTP client. More...
 
acetime_t getNow () const override
 Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z). More...
 
- Public Member Functions inherited from ace_time::clock::Clock
 Clock ()=default
 Default constructor.
 
 ~Clock ()=default
 We deliberately avoid using a virtual destructor. More...
 
virtual void sendRequest () const
 Send a time request asynchronously.
 
virtual bool isResponseReady () const
 Return true if a response is ready.
 
virtual acetime_t readResponse () const
 Returns number of seconds since AceTime epoch (2000-01-01). More...
 
virtual void setNow (acetime_t)
 Set the time to the indicated seconds. More...
 

Static Public Attributes

static const char kDefaultNtpServer [] = "pool.ntp.org"
 Default NTP server, "pool.ntp.org".
 
static const uint32_t kDefaultTimeoutMillis = 15000
 Default time out for setup().
 
- Static Public Attributes inherited from ace_time::clock::Clock
static const acetime_t kInvalidSeconds = LocalTime::kInvalidSeconds
 Error value returned by getNow() and other methods when this object is not yet initialized.
 

Detailed Description

An implementation of Clock that configures the built-in SNTP client on the ESP8266 and ESP32 using the configTime() function using UTC timezone (no STD or DST offset), and uses the C-library time() function as the reference clock.

Apparently the SNTP client synchronizes the time() every hour. This class depends on the WiFi client being configured somewhere else.

Definition at line 25 of file EspSntpClock.h.

Member Function Documentation

◆ getNow()

acetime_t ace_time::clock::EspSntpClock::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.

Since acetime_t is a 32-bit integer, this method is valid if the current SNTP time() is within about +/- 68 years of the current epoch being used by the AceTime library, as defined by Epoch::currentEpochYear().

Implements ace_time::clock::Clock.

Definition at line 58 of file EspSntpClock.h.

◆ setup()

bool ace_time::clock::EspSntpClock::setup ( const char *  ntpServer = kDefaultNtpServer,
uint32_t  timeoutMillis = kDefaultTimeoutMillis 
)

Setup the SNTP client.

Assumes WiFi is already configured. This step can be skipped if the SNTP client is configured somewhere else.

Parameters
ntpServername of the NTP server, default "pool.ntp.org"
timeoutMillisnumber of millis to wait before returning if the SNTP does not stabilize, default 15000 (15 seconds)
Returns
true if the setup() was successful, false if the timeout occurred

Definition at line 15 of file EspSntpClock.cpp.


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