-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntp.h
55 lines (49 loc) · 1.59 KB
/
ntp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
unsigned int localPort = 4097;
const int NTP_PACKET_SIZE = 48;
byte packetBuffer[NTP_PACKET_SIZE];
byte sendBuffer[] = {
0b11100011, // LI, Version, Mode.
0x0, // Stratum unspecified.
0x6, // Polling interval
0xEC, // Clock precision.
0x0, 0x0, 0x0, 0x0
}; // Reference ...
void sendNTPpacket(WiFiUDP *u) {
// Zeroise the buffer.
memset(packetBuffer, 0, NTP_PACKET_SIZE);
memcpy(packetBuffer, sendBuffer, 16);
if (u->beginPacket(settings.timeserver, 123)) {
u->write(packetBuffer, NTP_PACKET_SIZE);
u->endPacket();
}
}
time_t getNtpTime()
{
WiFiUDP udp;
udp.begin(localPort);
while (udp.parsePacket() > 0) ; // discard any previously received packets
for (int i = 0 ; i < 5 ; i++) { // 5 retries.
sendNTPpacket(&udp);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
if (udp.parsePacket()) {
udp.read(packetBuffer, NTP_PACKET_SIZE);
// Extract seconds portion.
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secSince1900 = highWord << 16 | lowWord;
udp.flush();
Serial.println("Got NTP Time");
return secSince1900 - 2208988800UL + settings.timezone * SECS_PER_HOUR;
}
delay(10);
}
}
Serial.println("Failed getting NTP time...");
return 0; // return 0 if unable to get the time
}
void setupTime() {
Serial.println("Initiating NTP");
setSyncProvider(getNtpTime);
setSyncInterval(settings.interval);
}