7 #include "../common/compat.h"
8 #include "../common/logging.h"
11 #if defined(ESP8266) || defined(ESP32)
21 uint16_t connectTimeoutMillis
25 WiFi.begin(ssid, password);
26 uint16_t startMillis = millis();
27 while (WiFi.status() != WL_CONNECTED) {
28 uint16_t elapsedMillis = millis() - startMillis;
29 if (elapsedMillis >= connectTimeoutMillis) {
30 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
31 SERIAL_PORT_MONITOR.println(F(
"NtpClock::setup(): failed"));
41 mUdp.begin(mLocalPort);
43 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
44 SERIAL_PORT_MONITOR.print(F(
"NtpClock::setup(): connected to"));
45 SERIAL_PORT_MONITOR.println(WiFi.localIP());
47 SERIAL_PORT_MONITOR.print(F(
"Local port: "));
48 SERIAL_PORT_MONITOR.println(mUdp.localPort());
60 uint16_t startTime = millis();
61 while ((uint16_t) (millis() - startTime) < mRequestTimeout) {
70 if (!mIsSetUp)
return;
71 if (WiFi.status() != WL_CONNECTED) {
72 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
73 SERIAL_PORT_MONITOR.println(
74 F(
"NtpClock::sendRequest(): not connected"));
80 while (mUdp.parsePacket() > 0) {}
82 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
83 SERIAL_PORT_MONITOR.println(F(
"NtpClock::sendRequest(): sending request"));
93 IPAddress ntpServerIP;
94 WiFi.hostByName(mServer, ntpServerIP);
95 sendNtpPacket(ntpServerIP);
99 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
100 static uint8_t rateLimiter;
103 if (!mIsSetUp)
return false;
104 if (WiFi.status() != WL_CONNECTED) {
105 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
106 if (++rateLimiter == 0) {
107 SERIAL_PORT_MONITOR.print(
"F[256]");
112 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
113 if (++rateLimiter == 0) {
114 SERIAL_PORT_MONITOR.print(
".[256]");
118 return mUdp.parsePacket() >= kNtpPacketSize;
123 if (WiFi.status() != WL_CONNECTED) {
124 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
125 SERIAL_PORT_MONITOR.println(
"NtpClock::readResponse(): not connected");
131 mUdp.read(mPacketBuffer, kNtpPacketSize);
134 uint32_t secsSince1900 = (uint32_t) mPacketBuffer[40] << 24;
135 secsSince1900 |= (uint32_t) mPacketBuffer[41] << 16;
136 secsSince1900 |= (uint32_t) mPacketBuffer[42] << 8;
137 secsSince1900 |= (uint32_t) mPacketBuffer[43];
139 acetime_t epochSeconds = (secsSince1900 == 0)
141 : secsSince1900 - kSecondsSinceNtpEpoch;
142 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
143 SERIAL_PORT_MONITOR.print(F(
"NtpClock::readResponse(): epoch="));
144 SERIAL_PORT_MONITOR.println(epochSeconds);
149 void NtpClock::sendNtpPacket(
const IPAddress& address)
const {
150 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
151 uint16_t startTime = millis();
155 memset(mPacketBuffer, 0, kNtpPacketSize);
158 mPacketBuffer[0] = 0b11100011;
159 mPacketBuffer[1] = 0;
160 mPacketBuffer[2] = 6;
161 mPacketBuffer[3] = 0xEC;
163 mPacketBuffer[12] = 49;
164 mPacketBuffer[13] = 0x4E;
165 mPacketBuffer[14] = 49;
166 mPacketBuffer[15] = 52;
169 mUdp.beginPacket(address, 123);
170 mUdp.write(mPacketBuffer, kNtpPacketSize);
173 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
174 SERIAL_PORT_MONITOR.print(F(
"NtpClock::sendNtpPacket(): "));
175 SERIAL_PORT_MONITOR.print((
unsigned) ((uint16_t) millis() - startTime));
176 SERIAL_PORT_MONITOR.println(
" ms");