-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8266_ntp.h
128 lines (119 loc) · 4.42 KB
/
8266_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
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
//*************************************************************************
// NTP routines for Network Real Time use.
//
#include <NTPClient.h> // NTP Client
#include <WiFiUdp.h> // Has variable type WiFiUDP, needed by timeClient()
#include <time.h> // Time.h calls timelib.h
// NTP variables & setup
long int updateInterval = 1000*3600; // Interval between calls to ntp, msec
long int gmtOffset = 1*3600; // European Standard Time Zone Time offset, seconds
long int summerOffset = 1*3600; // Daylight Savings offset(summertime), seconds
bool enableSummer = true; // Enable / disable Daylight Savings
int startSummerMonth = 3; // Startmonth & day Daylight Savings
int startSummerDay = 31; // ""
int endSummerMonth = 10; // Endmonth & day Daylight Savings
int endSummerDay = 31; // ""
//
int yearNow = 0; // Gregorian year.
int monthNow = 0; // Month
int dayOfMonthNow = 0; // Day of month
int dayOfWeekNow = 0; // Day of week
int hoursNow = 0; // Hours
int minutesNow = 0; // Minutes
int secondsNow = 0; // Seconds
int secondsSave = 0; // Second save
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", gmtOffset, updateInterval); // set up time client
// Calculate last sunday of month
int lastSunday(int iYear, int iMonth) {
struct tm sTime = {0};
sTime.tm_year = iYear - 1900;
sTime.tm_mon = iMonth - 1;
sTime.tm_mday = 31;
mktime(&sTime);
while (sTime.tm_wday != 0) {
sTime.tm_mday--;
mktime(&sTime);
}
return sTime.tm_mday;
}
// Check if in winter full months
bool inWinter() {
return ((monthNow < startSummerMonth) || (monthNow > endSummerMonth));
}
// Check if in summer full months
bool inSummer() {
return ((monthNow > startSummerMonth) && (monthNow < endSummerMonth));
}
// Switching logic to summertime
bool goingSummer(int days, int months) {
if (months != monthNow) {
return(false);
}
if (days > dayOfMonthNow) {
return(false);
}
if (days < dayOfMonthNow) {
return(true);
}
if (days = dayOfMonthNow) {
if (hoursNow >= 2) {
return(true);
} else {
return(false);
}
}
}
// Switching logic to wintertime
bool goingWinter(int days, int months) {
if (months != monthNow) {
return(false);
}
if (days > dayOfMonthNow) {
return(true);
}
if (days < dayOfMonthNow) {
return(false);
}
if (days = dayOfMonthNow) {
if (hoursNow >= 2) {
return(false);
} else {
return(true);
}
}
}
// Correct the time according the the summertime rules
time_t daylightSavingsCorrection(time_t *eptm) {
if (enableSummer) {
if (!inWinter()) {
if (inSummer()) {
*eptm+=summerOffset;
}
}
if (goingSummer(lastSunday(yearNow + 1900, startSummerMonth), startSummerMonth)) {
*eptm+=summerOffset;
}
if (goingWinter(lastSunday(yearNow + 1900, endSummerMonth), endSummerMonth)) {
*eptm+=summerOffset;
}
}
}
// Update all time variables in sync with NTP server
void updateTime() {
timeClient.update(); // will call ntp only if needed, based on updateInterval
time_t eptm; // C++ variable type, seconds since 1900
eptm = timeClient.getEpochTime(); // supposed to return sec since 1970, but magic here
daylightSavingsCorrection(&eptm); // correct Daylight Savings ...
//
struct tm* timenow; // C++ structure to hold times
timenow = localtime(&eptm); // function in time.h to get local time from time_t struct
//
yearNow = timenow->tm_year+1900; // Year; adjust to correct range
monthNow = timenow->tm_mon+1; // Month; adjust to 1-12 range
dayOfMonthNow = timenow->tm_mday; // Day of month
dayOfWeekNow = timenow->tm_wday; // Day of week
hoursNow = timenow->tm_hour; // Hours
minutesNow = timenow->tm_min; // Minutes
secondsNow = timenow->tm_sec; // Seconds
}