6 #ifndef ACE_TIME_NTP_CLOCK_H
7 #define ACE_TIME_NTP_CLOCK_H
9 #if defined(ESP8266) || defined(ESP32)
13 #include <ESP8266WiFi.h>
20 #ifndef ACE_TIME_NTP_CLOCK_DEBUG
21 #define ACE_TIME_NTP_CLOCK_DEBUG 0
76 mLocalPort(localPort),
77 mRequestTimeout(requestTimeout) {}
90 const char* ssid =
nullptr,
91 const char* password =
nullptr,
100 acetime_t
getNow()
const override;
110 static const uint8_t kNtpPacketSize = 48;
116 static const uint32_t kSecondsSinceNtpEpoch = 3155673600;
119 void sendNtpPacket(
const IPAddress& address)
const;
122 const char*
const mServer;
123 uint16_t
const mLocalPort;
124 uint16_t
const mRequestTimeout;
126 mutable WiFiUDP mUdp;
128 mutable uint8_t mPacketBuffer[kNtpPacketSize];
129 bool mIsSetUp =
false;
Abstract base class for objects that provide and store time.
A Clock that retrieves the time from an NTP server.
const char * getServer() const
Return the name of the NTP server.
acetime_t readResponse() const override
Returns number of seconds since AceTime epoch (2000-01-01).
void setup(const char *ssid=nullptr, const char *password=nullptr, uint16_t connectTimeoutMillis=kConnectTimeoutMillis)
Set up the WiFi connection using the given ssid and password, and prepare the UDP connection.
static const uint16_t kLocalPort
Default port used for UDP packets.
static const char kNtpServerName[]
Default NTP Server.
static const uint16_t kConnectTimeoutMillis
Number of millis to wait during connect before timing out.
static const uint16_t kRequestTimeoutMillis
Request time out milliseconds.
NtpClock(const char *server=kNtpServerName, uint16_t localPort=kLocalPort, uint16_t requestTimeout=kRequestTimeoutMillis)
Constructor.
bool isResponseReady() const override
Return true if a response is ready.
bool isSetup() const
Return true if setup() suceeded.
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
void sendRequest() const override
Send a time request asynchronously.
static const uint16_t kRequestTimeout
Deprecated.