-
Notifications
You must be signed in to change notification settings - Fork 13
/
NTP.ino
122 lines (119 loc) · 3.64 KB
/
NTP.ino
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
// NTP and location functions
String getIPlocation() { // Using ip-api.com to discover public IP's location and timezone
WiFiClient wifi;
HTTPClient http;
static const char URL[] PROGMEM = "http://ip-api.com/json";
String payload;
http.setUserAgent(UserAgent);
if (!http.begin(wifi, URL)) {
#ifdef SYSLOG
syslog.log(F("getIPlocation HTTP failed"));
#endif
OUT.println(F("getIPlocation: HTTP failed"));
} else {
int stat = http.GET();
if (stat == HTTP_CODE_OK) {
payload = http.getString();
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(payload);
if (root.success()) {
String isp = root["isp"];
String region = root["regionName"];
String country = root["countryCode"];
String tz = root["timezone"];
String zip = root["zip"];
timezone = tz;
countryCode = country;
http.end();
#ifdef SYSLOG
syslog.logf("getIPlocation: %s, %s, %s, %s",
isp.c_str(), region.c_str(), country.c_str(), tz.c_str());
#endif
OUT.println(PSTR("getIPlocation: ") + isp + PSTR(", ")
+ region + PSTR(", ") + country + PSTR(", ") + tz);
return zip;
} else {
#ifdef SYSLOG
syslog.log(F("getIPlocation JSON parse failed"));
syslog.log(payload);
#endif
OUT.println(F("getIPlocation: JSON parse failed!"));
OUT.println(payload);
}
} else {
#ifdef SYSLOG
syslog.logf("getIPlocation failed, GET reply %d", stat);
#endif
OUT.printf_P(PSTR("getIPlocation: GET reply %d\r\n"), stat);
}
}
http.end();
delay(1000);
} // getIPlocation
int getOffset(const String tz) { // using timezonedb.com, return offset for zone name
WiFiClient wifi;
HTTPClient http;
String URL = PSTR("http://api.timezonedb.com/v2.1/get-time-zone?key=")
+ tzKey + PSTR("&format=json&by=zone&country=") + countryCode + PSTR("&zone=") + tz;
String payload;
int stat;
http.setUserAgent(UserAgent);
if (!http.begin(wifi, URL)) {
#ifdef SYSLOG
syslog.log(F("getOffset HTTP failed"));
#endif
OUT.println(F("getOffset: HTTP failed"));
} else {
stat = http.GET();
if (stat == HTTP_CODE_OK) {
payload = http.getString();
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(payload);
if (root.success()) {
offset = root["gmtOffset"];
OUT.println(PSTR("getOffset: ") + String(offset));
} else {
#ifdef SYSLOG
syslog.log(F("getOffset JSON parse failed"));
syslog.log(payload);
#endif
OUT.println(F("getOffset: JSON parse failed!"));
OUT.println(payload);
}
} else {
#ifdef SYSLOG
syslog.logf("getOffset failed, GET reply %d", stat);
#endif
OUT.printf_P(PSTR("getOffset: GET reply %d\r\n"), stat);
}
}
http.end();
return stat;
} // getOffset
void setNTP(const String tz) {
while (getOffset(tz) != HTTP_CODE_OK) { // wait for valid timezone offset
delay(1000);
}
OUT.print(F("setNTP: configure NTP ..."));
configTime(offset, 0, "pool.ntp.org");
while (time(nullptr) < (30 * 365 * 24 * 60 * 60)) { // wait for NTP sync
delay(1000);
OUT.print(F("."));
}
OUT.println(F(" OK"));
time_t now = time(nullptr);
struct tm * calendar;
calendar = localtime(&now);
calendar->tm_mday++; // calculate tomorrow
calendar->tm_hour = 2; // at 2am
calendar->tm_min = 0;
calendar->tm_sec = 0;
TWOAM = mktime(calendar);
String t = ctime(&TWOAM);
t.trim();
OUT.print(F("setNTP: next timezone check @ "));
OUT.println(t);
#ifdef SYSLOG
syslog.logf("setNTP: %s (%d)", timezone.c_str(), int(offset / 3600));
#endif
} // setNTP