9 #if defined(ESP8266) || defined(ESP32)
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"));
96 IPAddress ntpServerIP;
97 WiFi.hostByName(mServer, ntpServerIP);
98 sendNtpPacket(ntpServerIP);
102 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
103 static uint8_t rateLimiter;
106 if (!mIsSetUp)
return false;
107 if (WiFi.status() != WL_CONNECTED) {
108 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
109 if (++rateLimiter == 0) {
110 SERIAL_PORT_MONITOR.print(
"F[256]");
115 #if ACE_TIME_NTP_CLOCK_DEBUG >= 3
116 if (++rateLimiter == 0) {
117 SERIAL_PORT_MONITOR.print(
".[256]");
121 return mUdp.parsePacket() >= kNtpPacketSize;
126 if (WiFi.status() != WL_CONNECTED) {
127 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
128 SERIAL_PORT_MONITOR.println(
"NtpClock::readResponse(): not connected");
134 mUdp.read(mPacketBuffer, kNtpPacketSize);
137 uint32_t secsSince1900 = (uint32_t) mPacketBuffer[40] << 24;
138 secsSince1900 |= (uint32_t) mPacketBuffer[41] << 16;
139 secsSince1900 |= (uint32_t) mPacketBuffer[42] << 8;
140 secsSince1900 |= (uint32_t) mPacketBuffer[43];
142 acetime_t epochSeconds = (secsSince1900 == 0)
144 : secsSince1900 - kSecondsSinceNtpEpoch;
145 #if ACE_TIME_NTP_CLOCK_DEBUG >= 1
146 SERIAL_PORT_MONITOR.print(F(
"NtpClock::readResponse(): epoch="));
147 SERIAL_PORT_MONITOR.println(epochSeconds);
152 void NtpClock::sendNtpPacket(
const IPAddress& address)
const {
153 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
154 uint16_t startTime = millis();
158 memset(mPacketBuffer, 0, kNtpPacketSize);
161 mPacketBuffer[0] = 0b11100011;
162 mPacketBuffer[1] = 0;
163 mPacketBuffer[2] = 6;
164 mPacketBuffer[3] = 0xEC;
166 mPacketBuffer[12] = 49;
167 mPacketBuffer[13] = 0x4E;
168 mPacketBuffer[14] = 49;
169 mPacketBuffer[15] = 52;
172 mUdp.beginPacket(address, 123);
173 mUdp.write(mPacketBuffer, kNtpPacketSize);
176 #if ACE_TIME_NTP_CLOCK_DEBUG >= 2
177 SERIAL_PORT_MONITOR.print(F(
"NtpClock::sendNtpPacket(): "));
178 SERIAL_PORT_MONITOR.print((
unsigned) ((uint16_t) millis() - startTime));
179 SERIAL_PORT_MONITOR.println(
" ms");