AceTime
1.7.2
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.
|
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
70 mLocalPort(localPort),
71 mRequestTimeout(requestTimeout) {}
84 const char* ssid =
nullptr,
85 const char* password =
nullptr,
86 uint16_t connectTimeoutMillis = kConnectTimeoutMillis);
94 acetime_t
getNow()
const override;
104 static const uint8_t kNtpPacketSize = 48;
110 static const uint32_t kSecondsSinceNtpEpoch = 3155673600;
113 static const uint16_t kConnectTimeoutMillis = 10000;
116 void sendNtpPacket(
const IPAddress& address)
const;
119 const char*
const mServer;
120 uint16_t
const mLocalPort;
121 uint16_t
const mRequestTimeout;
123 mutable WiFiUDP mUdp;
125 mutable uint8_t mPacketBuffer[kNtpPacketSize];
126 bool mIsSetUp =
false;
132 #endif // defined(ESP8266) || defined(ESP32)
void sendRequest() const override
Send a time request asynchronously.
static const uint16_t kRequestTimeout
Request time out milliseconds.
static const char kNtpServerName[]
Default NTP Server.
bool isResponseReady() const override
Return true if a response is ready.
const char * getServer() const
Return the name of the NTP server.
NtpClock(const char *server=kNtpServerName, uint16_t localPort=kLocalPort, uint16_t requestTimeout=kRequestTimeout)
Constructor.
A Clock that retrieves the time from an NTP server.
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
Abstract base class for objects that provide and store time.
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.
bool isSetup() const
Return true if setup() suceeded.
static const uint16_t kLocalPort
Default port used for UDP packets.