-
Notifications
You must be signed in to change notification settings - Fork 16
/
NTP_Example.ino
180 lines (149 loc) · 5.09 KB
/
NTP_Example.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
This is an example file for using the time function in ESP8266 or ESP32 tu get NTP time
It offers two functions:
- getNTPtime(struct tm * info, uint32_t ms) where info is a structure which contains time
information and ms is the time the service waits till it gets a response from NTP.
Each time you cann this function it calls NTP over the net.
If you do not want to call an NTP service every second, you can use
- getTimeReducedTraffic(int ms) where ms is the the time between two physical NTP server calls. Betwwn these calls,
the time structure is updated with the (inaccurate) timer. If you call NTP every few minutes you should be ok
The time structure is called tm and has teh following values:
Definition of struct tm:
Member Type Meaning Range
tm_sec int seconds after the minute 0-61*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag
because the values are somhow akwardly defined, I introduce a function makeHumanreadable() where all values are adjusted according normal numbering.
e.g. January is month 1 and not 0 And Sunday or monday is weekday 1 not 0 (according definition of MONDAYFIRST)
Showtime is an example on how you can use the time in your sketch
The functions are inspired by work of G6EJD ( https://www.youtube.com/channel/UCgtlqH_lkMdIa4jZLItcsTg )
*/
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <time.h>
// #include <credentials.h>
/*
The credentials.h file at least has to contain:
char mySSID[]="your SSID";
char myPASSWORD[]="your Password";
It has to be placed in the libraries folder
If you do not want a credentials file. delete the line: #include <credentials.h>
*/
#ifdef CREDENTIALS
const char* ssid = mySSID;
const char* password = myPASSWORD;
#else
const char* ssid = "yourSSID";
const char* password = "YourPassword";
#endif
const char* NTP_SERVER = "ch.pool.ntp.org";
const char* TZ_INFO = "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"; // enter your time zone (https://remotemonitoringsystems.ca/time-zone-abbreviations.php)
tm timeinfo;
time_t now;
long unsigned lastNTPtime;
unsigned long lastEntryTime;
void setup() {
Serial.begin(115200);
Serial.println("\n\nNTP Time Test\n");
WiFi.begin(ssid, password);
int counter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(200);
if (++counter > 100) ESP.restart();
Serial.print ( "." );
}
Serial.println("\n\nWiFi connected\n\n");
configTime(0, 0, NTP_SERVER);
// See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
setenv("TZ", TZ_INFO, 1);
if (getNTPtime(10)) { // wait up to 10sec to sync
} else {
Serial.println("Time not set");
ESP.restart();
}
showTime(timeinfo);
lastNTPtime = time(&now);
lastEntryTime = millis();
}
void loop() {
// getTimeReducedTraffic(3600);
getNTPtime(10);
showTime(timeinfo);
delay(1000);
}
bool getNTPtime(int sec) {
{
uint32_t start = millis();
do {
time(&now);
localtime_r(&now, &timeinfo);
Serial.print(".");
delay(10);
} while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
if (timeinfo.tm_year <= (2016 - 1900)) return false; // the NTP call was not successful
Serial.print("now "); Serial.println(now);
char time_output[30];
strftime(time_output, 30, "%a %d-%m-%y %T", localtime(&now));
Serial.println(time_output);
Serial.println();
}
return true;
}
// This function is obsolete because the time() function only calls the NTP server every hour. So you can always use getNTPtime()
// It can be deleted and only stays here for the video
/*
void getTimeReducedTraffic(int sec) {
tm *ptm;
if ((millis() - lastEntryTime) < (1000 * sec)) {
now = lastNTPtime + (int)(millis() - lastEntryTime) / 1000;
} else {
lastEntryTime = millis();
lastNTPtime = time(&now);
now = lastNTPtime;
Serial.println("Get NTP time");
}
ptm = localtime(&now);
timeinfo = *ptm;
}
*/
void showTime(tm localTime) {
Serial.print(localTime.tm_mday);
Serial.print('/');
Serial.print(localTime.tm_mon + 1);
Serial.print('/');
Serial.print(localTime.tm_year - 100);
Serial.print('-');
Serial.print(localTime.tm_hour);
Serial.print(':');
Serial.print(localTime.tm_min);
Serial.print(':');
Serial.print(localTime.tm_sec);
Serial.print(" Day of Week ");
if (localTime.tm_wday == 0) Serial.println(7);
else Serial.println(localTime.tm_wday);
}
/*
// Shorter way of displaying the time
void showTime(tm localTime) {
Serial.printf(
"%04d-%02d-%02d %02d:%02d:%02d, day %d, %s time\n",
localTime.tm_year + 1900,
localTime.tm_mon + 1,
localTime.tm_mday,
localTime.tm_hour,
localTime.tm_min,
localTime.tm_sec,
(localTime.tm_wday > 0 ? localTime.tm_wday : 7 ),
(localTime.tm_isdst == 1 ? "summer" : "standard")
);
}
*/