-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntp.h
122 lines (100 loc) · 3.24 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
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
/**
* Does NTP, DST, and sunrise/sunset.
* @see https://github.com/neptune2/simpleDSTadjust/blob/master/examples/ntpTimedemo_DST/ntpTimedemo_DST.ino
* @see https://www.arduino.cc/reference/en/libraries/sunset/
*/
#include <ESP8266WiFi.h>
#include <sunset.h>
#include <time.h>
#include <simpleDSTadjust.h>
const char *ssid = "HelloWardrivers!";
const char *password = "aaaa1111";
#define NTP_SERVERS "us.pool.ntp.org", "pool.ntp.org", "time.nist.gov"
#define TIMEZONE -5
#define LATITUDE 39.10895
#define LONGITUDE -77.11047
// US Eastern Time Zone (New York, Boston).
struct dstRule StartRule = {"EDT", Second, Sun, Mar, 2, 3600}; // Daylight time = UTC/GMT -4 hours
struct dstRule EndRule = {"EST", First, Sun, Nov, 2, 0}; // Standard time = UTC/GMT -5 hour
simpleDSTadjust dstAdjusted(StartRule, EndRule);
SunSet sun;
void updateNTP() {
configTime(TIMEZONE * 3600, 0, NTP_SERVERS);
delay(500);
while (!time(nullptr)) {
Serial.print("#");
delay(1000);
}
}
void ntp_setup() {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
updateNTP(); // Init the NTP time
sun.setPosition(LATITUDE, LONGITUDE, TIMEZONE);
}
/**
* @return bool
* If we are observing DST.
*/
bool isDST() {
char *dstAbbrev;
dstAdjusted.time(&dstAbbrev);
return !strcmp(dstAbbrev, StartRule.abbrev);
}
/**
* @return int
* Number of minutes since midnight.
*/
int timeInMinutes() {
char *dstAbbrev;
time_t t = dstAdjusted.time(&dstAbbrev);
struct tm *timeinfo = localtime (&t);
return (timeinfo->tm_hour * 60) + timeinfo->tm_min;
}
int minutesToSleepUntilDark() {
updateNTP();
char *dstAbbrev;
time_t t = dstAdjusted.time(&dstAbbrev);
struct tm *timeinfo = localtime (&t);
char buf[30];
int hour12 = (timeinfo->tm_hour+11)%12+1; // take care of noon and midnight
sprintf(buf, "minutesToSleepUntilDark says it is %02d/%02d/%04d %02d:%02d:%02d%s %s\n",timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_year+1900, hour12, timeinfo->tm_min, timeinfo->tm_sec, timeinfo->tm_hour>=12?"pm":"am", dstAbbrev);
Serial.print(buf);
sun.setCurrentDate(timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday);
sun.setTZOffset(TIMEZONE + (isDST() ? 1 : 0));
int sunrise = sun.calcSunrise();
int sunset = sun.calcSunset();
int nowmins = timeInMinutes();
Serial.printf("Now %d:%02d pm\n", (nowmins / 60), nowmins % 60);
Serial.printf("Sunrise: %d:%02d am\n", sunrise / 60, sunrise % 60);
Serial.printf("Sunset %d:%02d pm\n", (sunset / 60) - 12, sunset % 60);
Serial.printf("isDST is %d\n", isDST() ? 1 : 0);
// Before sunrise, return 0.
if (nowmins < sunrise) {
return 0;
}
// After sunset, return 0;
if (nowmins > sunset) {
return 0;
}
return sunset - nowmins;
}
int minutesToSunset() {
char *dstAbbrev;
time_t t = dstAdjusted.time(&dstAbbrev);
struct tm *timeinfo = localtime (&t);
sun.setCurrentDate(2022, 04, 22);
int sunset = sun.calcSunset() + 60;
int nowmins = timeInMinutes();
// Before sunset is simple subtraction.
if (sunset > nowmins) {
return sunset - nowmins;
}
// Minutes to midnight + sunrise minutes.
return (1440 - nowmins) + sunset;
}