-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNtp.cpp
executable file
·129 lines (101 loc) · 3.14 KB
/
Ntp.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Ntp.h"
#include <ESPAsyncUDP.h>
char * NtpClient::TimeServerName;
int8_t NtpClient::timezone;
time_t NtpClient::syncInterval;
IPAddress NtpClient::timeServer;
AsyncUDP NtpClient::udpListener;
byte NtpClient::NTPpacket[NTP_PACKET_SIZE];
void ICACHE_FLASH_ATTR NtpClient::Ntp(const char * server, int8_t tz, time_t syncSecs) {
TimeServerName = strdup(server);
timezone = tz;
syncInterval = syncSecs;
WiFi.hostByName(TimeServerName, timeServer);
setSyncProvider(getNtpTime);
setSyncInterval(syncInterval);
}
ICACHE_FLASH_ATTR NtpClient::~NtpClient() {
udpListener.close();
}
// send an NTP request to the time server at the given address
time_t ICACHE_FLASH_ATTR NtpClient::getNtpTime() {
memset(NTPpacket, 0, sizeof(NTPpacket));
NTPpacket[0] = 0b11100011;
NTPpacket[1] = 0;
NTPpacket[2] = 6;
NTPpacket[3] = 0xEC;
NTPpacket[12] = 49;
NTPpacket[13] = 0x4E;
NTPpacket[14] = 49;
NTPpacket[15] = 52;
if (udpListener.connect(timeServer, 123)) {
udpListener.onPacket([](AsyncUDPPacket packet) {
unsigned long highWord = word(packet.data()[40], packet.data()[41]);
unsigned long lowWord = word(packet.data()[42], packet.data()[43]);
time_t UnixUTCtime = (highWord << 16 | lowWord) - 2208988800UL;
setTime(UnixUTCtime);
});
}
else {
}
udpListener.write(NTPpacket, sizeof(NTPpacket));
// ugly
return 0;
}
bool ICACHE_FLASH_ATTR NtpClient::processTime() {
timeStatus_t ts = timeStatus();
switch (ts) {
case timeNeedsSync:
return false;
break;
case timeSet:
return true;
break;
default:
return false;
}
}
String ICACHE_FLASH_ATTR NtpClient::zeroPaddedIntVal(int val) {
if (val < 10)
return "0" + String(val);
else
return String(val);
}
//returns the current date/time as a string in iso8601 format
String ICACHE_FLASH_ATTR NtpClient::iso8601DateTime() {
String hyphen = "-";
String colon = ":";
return String(year()) + hyphen +
zeroPaddedIntVal(month()) + hyphen +
zeroPaddedIntVal(day()) + "T" +
zeroPaddedIntVal(hour()) + colon +
zeroPaddedIntVal(minute()) + colon +
zeroPaddedIntVal(second()) +
(timezone == 0 ? "Z" : String(timezone));
}
time_t NtpClient::getUptimeSec() {
_uptimesec = _uptimesec + (millis () - _uptimesec);
return _uptimesec / 1000;
}
deviceUptime ICACHE_FLASH_ATTR NtpClient::getDeviceUptime() {
unsigned long currentmillis = millis();
deviceUptime uptime;
uptime.secs = (long)((currentmillis / 1000) % 60);
uptime.mins = (long)((currentmillis / 60000) % 60);
uptime.hours = (long)((currentmillis / 3600000) % 24);
uptime.days = (long)((currentmillis / 86400000) % 10);
return uptime;
}
String ICACHE_FLASH_ATTR NtpClient::getDeviceUptimeString() {
deviceUptime uptime = getDeviceUptime();
return String(uptime.days) + " days, " +
String(uptime.hours) + " hours, " +
String(uptime.mins) + " mins, " +
String(uptime.secs) + " secs";
}
/*
* returns the current time as UTC (timezone offset removed)
*/
ICACHE_FLASH_ATTR time_t NtpClient::getUtcTimeNow() {
return now() - timezone;
}