9 #if defined(ESP8266) || defined(ESP32) || defined(EPOXY_CORE_ESP8266)
12 #ifndef SERIAL_PORT_MONITOR
13 #define SERIAL_PORT_MONITOR Serial
24 uint16_t connectTimeoutMillis
28 WiFi.begin(ssid, password);
29 uint16_t startMillis = millis();
30 while (WiFi.status() != WL_CONNECTED) {
31 uint16_t elapsedMillis = millis() - startMillis;
32 if (elapsedMillis >= connectTimeoutMillis) {
33 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
34 SERIAL_PORT_MONITOR.println(F(
"NtpClock::setup(): failed"));
44 mUdp.begin(mLocalPort);
46 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
47 SERIAL_PORT_MONITOR.print(F(
"NtpClock::setup(): connected to"));
48 SERIAL_PORT_MONITOR.println(WiFi.localIP());
50 SERIAL_PORT_MONITOR.print(F(
"Local port: "));
51 SERIAL_PORT_MONITOR.println(mUdp.localPort());
63 uint16_t startTime = millis();
64 while ((uint16_t) (millis() - startTime) < mRequestTimeout) {
73 if (!mIsSetUp)
return;
74 if (WiFi.status() != WL_CONNECTED) {
75 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
76 SERIAL_PORT_MONITOR.println(
77 F(
"NtpClock::sendRequest(): not connected"));
83 while (mUdp.parsePacket() > 0) {}
85 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
86 SERIAL_PORT_MONITOR.println(F(
"NtpClock::sendRequest(): sending request"));
95 IPAddress ntpServerIP;
96 WiFi.hostByName(mServer, ntpServerIP);
97 sendNtpPacket(ntpServerIP);
101 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
102 static uint8_t rateLimiter;
105 if (!mIsSetUp)
return false;
106 if (WiFi.status() != WL_CONNECTED) {
107 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
108 if (++rateLimiter == 0) {
109 SERIAL_PORT_MONITOR.print(
"F[256]");
114 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
115 if (++rateLimiter == 0) {
116 SERIAL_PORT_MONITOR.print(
".[256]");
120 return mUdp.parsePacket() >= kNtpPacketSize;
125 if (WiFi.status() != WL_CONNECTED) {
126 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
127 SERIAL_PORT_MONITOR.println(
"NtpClock::readResponse(): not connected");
133 mUdp.read(mPacketBuffer, kNtpPacketSize);
153 uint32_t ntpSeconds = (uint32_t) mPacketBuffer[40] << 24;
154 ntpSeconds |= (uint32_t) mPacketBuffer[41] << 16;
155 ntpSeconds |= (uint32_t) mPacketBuffer[42] << 8;
156 ntpSeconds |= (uint32_t) mPacketBuffer[43];
160 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
161 SERIAL_PORT_MONITOR.print(F(
"NtpClock::readResponse(): ntpSeconds="));
162 SERIAL_PORT_MONITOR.print(ntpSeconds);
163 SERIAL_PORT_MONITOR.print(F(
"; epochSeconds="));
164 SERIAL_PORT_MONITOR.println(epochSeconds);
181 int32_t daysToCurrentEpochFromNtpEpoch =
182 Epoch::daysToCurrentEpochFromInternalEpoch()
183 + kDaysToInternalEpochFromNtpEpoch;
184 uint32_t secondsToCurrentEpochFromNtpEpoch = (uint32_t) 86400
185 * (uint32_t) daysToCurrentEpochFromNtpEpoch;
186 uint32_t epochSeconds = ntpSeconds - secondsToCurrentEpochFromNtpEpoch;
191 return (int32_t) epochSeconds;
194 void NtpClock::sendNtpPacket(
const IPAddress& address)
const {
195 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
196 uint16_t startTime = millis();
200 memset(mPacketBuffer, 0, kNtpPacketSize);
203 mPacketBuffer[0] = 0b11100011;
204 mPacketBuffer[1] = 0;
205 mPacketBuffer[2] = 6;
206 mPacketBuffer[3] = 0xEC;
208 mPacketBuffer[12] = 49;
209 mPacketBuffer[13] = 0x4E;
210 mPacketBuffer[14] = 49;
211 mPacketBuffer[15] = 52;
214 mUdp.beginPacket(address, 123);
215 mUdp.write(mPacketBuffer, kNtpPacketSize);
218 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
219 SERIAL_PORT_MONITOR.print(F(
"NtpClock::sendNtpPacket(): "));
220 SERIAL_PORT_MONITOR.print((
unsigned) ((uint16_t) millis() - startTime));
221 SERIAL_PORT_MONITOR.println(
" ms");
static const acetime_t kInvalidSeconds
Error value returned by getNow() and other methods when this object is not yet initialized.
static acetime_t convertNtpSecondsToAceTimeSeconds(uint32_t ntpSeconds)
Convert an NTP seconds to AceTime seconds relative to the current AceTime epoch defined by Epoch::cur...
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 char kNtpServerName[]
Default NTP Server.
bool isResponseReady() const override
Return true if a response is ready.
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.