-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtimezonefunctions.ino
99 lines (89 loc) · 2.99 KB
/
timezonefunctions.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
#include <ESP8266HTTPClient.h>
#include "ntp_client_plus.h"
#include "udplogger.h"
int api_offset = 0;
String api_timezone = "";
float api_lat = 0.0;
float api_lon = 0.0;
/**
* @brief Request the timezone and other data from the IP-API
*
* @param logger UDPLogger object to log messages
* @return bool true if the api request was successful
*/
bool requestAPIData(UDPLogger &logger) {
WiFiClient client;
HTTPClient http;
bool res = false;
logger.logString("[HTTP] Requesting timezone from IP-API");
// see API documentation on https://ip-api.com/docs/api:json to see which fields are available
if (http.begin(client, "http://ip-api.com/json/?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,offset,query")) {
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
api_timezone = getJsonParameterValue(payload, "timezone", true);
logger.logString("[HTTP] Received timezone: " + api_timezone);
String offsetString = getJsonParameterValue(payload, "offset", false);
api_offset = offsetString.toInt() / 60;
logger.logString("[HTTP] Received offset (min): " + String(api_offset));
String latString = getJsonParameterValue(payload, "lat", false);
api_lat = latString.toFloat();
logger.logString("[HTTP] Received latitude: " + String(api_lat));
String lonString = getJsonParameterValue(payload, "lon", false);
api_lon = lonString.toFloat();
logger.logString("[HTTP] Received longitude: " + String(api_lon));
}
}
else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
res = false;
}
http.end(); // Close connection
}
else {
logger.logString("[HTTP] Unable to connect");
res = false;
}
return res;
}
/**
* @brief Get the Json Parameter Value object
*
* @param json
* @param parameter
* @return String
*/
String getJsonParameterValue(String json, String parameter, bool isString) {
String value = "";
if(isString) {
int index = json.indexOf("\"" + parameter + "\":\"");
if (index != -1) {
int start = index + parameter.length() + 4;
int end = json.indexOf("\"", start);
value = json.substring(start, end);
}
}
else {
int index = json.indexOf("\"" + parameter + "\":");
if (index != -1) {
int start = index + parameter.length() + 3;
int end = json.indexOf(",", start);
value = json.substring(start, end);
}
}
return value;
}
/**
* @brief Update the UTC offset from the timezone string obtained from the IP-API
*
* @param logger UDPLogger object to log messages
* @param ntp NTPClientPlus object to set the UTC offset
* @return int
*/
void updateUTCOffsetFromTimezoneAPI(UDPLogger &logger, NTPClientPlus &ntp) {
bool res = requestAPIData(logger);
if (res) {
ntp.setUTCOffset(api_offset);
}
}