21
21
22
22
#include " NTPClient.h"
23
23
24
- NTPClient::NTPClient (UDP& udp) {
25
- this ->_udp = &udp;
24
+ NTPClient::NTPClient (UDP &udp)
25
+ {
26
+ this ->_udp = &udp;
26
27
}
27
28
28
- NTPClient::NTPClient (UDP& udp, long timeOffset) {
29
- this ->_udp = &udp;
30
- this ->_timeOffset = timeOffset;
29
+ NTPClient::NTPClient (UDP &udp, long timeOffset)
30
+ {
31
+ this ->_udp = &udp;
32
+ this ->_timeOffset = timeOffset;
31
33
}
32
34
33
- NTPClient::NTPClient (UDP& udp, const char * poolServerName) {
34
- this ->_udp = &udp;
35
+ NTPClient::NTPClient (UDP &udp, const char *poolServerName)
36
+ {
37
+ this ->_udp = &udp;
35
38
this ->_poolServerName = poolServerName;
36
39
}
37
40
38
- NTPClient::NTPClient (UDP& udp, IPAddress poolServerIP) {
39
- this ->_udp = &udp;
40
- this ->_poolServerIP = poolServerIP;
41
+ NTPClient::NTPClient (UDP &udp, IPAddress poolServerIP)
42
+ {
43
+ this ->_udp = &udp;
44
+ this ->_poolServerIP = poolServerIP;
41
45
this ->_poolServerName = NULL ;
42
46
}
43
47
44
- NTPClient::NTPClient (UDP& udp, const char * poolServerName, long timeOffset) {
45
- this ->_udp = &udp;
46
- this ->_timeOffset = timeOffset;
48
+ NTPClient::NTPClient (UDP &udp, const char *poolServerName, long timeOffset)
49
+ {
50
+ this ->_udp = &udp;
51
+ this ->_timeOffset = timeOffset;
47
52
this ->_poolServerName = poolServerName;
48
53
}
49
54
50
- NTPClient::NTPClient (UDP& udp, IPAddress poolServerIP, long timeOffset){
51
- this ->_udp = &udp;
52
- this ->_timeOffset = timeOffset;
53
- this ->_poolServerIP = poolServerIP;
55
+ NTPClient::NTPClient (UDP &udp, IPAddress poolServerIP, long timeOffset)
56
+ {
57
+ this ->_udp = &udp;
58
+ this ->_timeOffset = timeOffset;
59
+ this ->_poolServerIP = poolServerIP;
54
60
this ->_poolServerName = NULL ;
55
61
}
56
62
57
- NTPClient::NTPClient (UDP& udp, const char * poolServerName, long timeOffset, unsigned long updateInterval) {
58
- this ->_udp = &udp;
59
- this ->_timeOffset = timeOffset;
63
+ NTPClient::NTPClient (UDP &udp, const char *poolServerName, long timeOffset, unsigned long updateInterval)
64
+ {
65
+ this ->_udp = &udp;
66
+ this ->_timeOffset = timeOffset;
60
67
this ->_poolServerName = poolServerName;
61
68
this ->_updateInterval = updateInterval;
62
69
}
63
70
64
- NTPClient::NTPClient (UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval) {
65
- this ->_udp = &udp;
66
- this ->_timeOffset = timeOffset;
67
- this ->_poolServerIP = poolServerIP;
71
+ NTPClient::NTPClient (UDP &udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval)
72
+ {
73
+ this ->_udp = &udp;
74
+ this ->_timeOffset = timeOffset;
75
+ this ->_poolServerIP = poolServerIP;
68
76
this ->_poolServerName = NULL ;
69
77
this ->_updateInterval = updateInterval;
70
78
}
71
79
72
- void NTPClient::begin () {
80
+ void NTPClient::config_update (UDP &udp, const char *poolServerName, long timeOffset, unsigned long updateInterval)
81
+ {
82
+ this ->_udp = &udp;
83
+ this ->_timeOffset = timeOffset;
84
+ this ->_poolServerName = poolServerName;
85
+ this ->_updateInterval = updateInterval;
86
+ }
87
+
88
+ void NTPClient::begin ()
89
+ {
73
90
this ->begin (NTP_DEFAULT_LOCAL_PORT);
74
91
}
75
92
76
- void NTPClient::begin (unsigned int port) {
93
+ void NTPClient::begin (unsigned int port)
94
+ {
77
95
this ->_port = port;
78
96
79
97
this ->_udp ->begin (this ->_port );
80
98
81
99
this ->_udpSetup = true ;
82
100
}
83
101
84
- bool NTPClient::forceUpdate () {
85
- #ifdef DEBUG_NTPClient
86
- Serial.println (" Update from NTP Server" );
87
- #endif
88
-
102
+ bool NTPClient::forceUpdate ()
103
+ {
89
104
// flush any existing packets
90
- while (this ->_udp ->parsePacket () != 0 )
105
+ while (this ->_udp ->parsePacket () != 0 )
91
106
this ->_udp ->flush ();
92
107
93
108
this ->sendNTPPacket ();
94
109
95
110
// Wait till data is there or timeout...
96
111
byte timeout = 0 ;
97
112
int cb = 0 ;
98
- do {
99
- delay ( 10 );
113
+ do
114
+ {
115
+ delay (10 );
100
116
cb = this ->_udp ->parsePacket ();
101
- if (timeout > 100 ) return false ; // timeout after 1000 ms
117
+ if (timeout > 100 )
118
+ return false ; // timeout after 1000 ms
102
119
timeout++;
103
120
} while (cb == 0 );
104
121
@@ -114,99 +131,166 @@ bool NTPClient::forceUpdate() {
114
131
115
132
this ->_currentEpoc = secsSince1900 - SEVENZYYEARS;
116
133
117
- return true ; // return true after successful update
134
+ return true ; // return true after successful update
118
135
}
119
136
120
- bool NTPClient::update () {
121
- if ((millis () - this ->_lastUpdate >= this ->_updateInterval ) // Update after _updateInterval
122
- || this ->_lastUpdate == 0 ) { // Update if there was no update yet.
123
- if (!this ->_udpSetup || this ->_port != NTP_DEFAULT_LOCAL_PORT) this ->begin (this ->_port ); // setup the UDP client if needed
137
+ bool NTPClient::update ()
138
+ {
139
+ if ((millis () - this ->_lastUpdate >= this ->_updateInterval ) // Update after _updateInterval
140
+ || this ->_lastUpdate == 0 )
141
+ { // Update if there was no update yet.
142
+ if (!this ->_udpSetup || this ->_port != NTP_DEFAULT_LOCAL_PORT)
143
+ this ->begin (this ->_port ); // setup the UDP client if needed
124
144
return this ->forceUpdate ();
125
145
}
126
- return false ; // return false if update does not occur
146
+ return false ; // return false if update does not occur
127
147
}
128
148
129
- bool NTPClient::isTimeSet () const {
149
+ bool NTPClient::isTimeSet () const
150
+ {
130
151
return (this ->_lastUpdate != 0 ); // returns true if the time has been set, else false
131
152
}
132
153
133
- unsigned long NTPClient::getEpochTime () const {
134
- return this ->_timeOffset + // User offset
135
- this ->_currentEpoc + // Epoch returned by the NTP server
154
+ unsigned long NTPClient::getEpochTime () const
155
+ {
156
+ return this ->_timeOffset + // User offset
157
+ this ->_currentEpoc + // Epoch returned by the NTP server
136
158
((millis () - this ->_lastUpdate ) / 1000 ); // Time since last update
137
159
}
138
160
139
- int NTPClient::getDay () const {
140
- return (((this ->getEpochTime () / 86400L ) + 4 ) % 7 ); // 0 is Sunday
161
+ int NTPClient::getDay () const
162
+ {
163
+ return (((this ->getEpochTime () / 86400L ) + 4 ) % 7 ); // 0 is Sunday
141
164
}
142
- int NTPClient::getHours () const {
143
- return ((this ->getEpochTime () % 86400L ) / 3600 );
165
+ int NTPClient::getHours () const
166
+ {
167
+ return ((this ->getEpochTime () % 86400L ) / 3600 );
144
168
}
145
- int NTPClient::getMinutes () const {
169
+ int NTPClient::getMinutes () const
170
+ {
146
171
return ((this ->getEpochTime () % 3600 ) / 60 );
147
172
}
148
- int NTPClient::getSeconds () const {
173
+ int NTPClient::getSeconds () const
174
+ {
149
175
return (this ->getEpochTime () % 60 );
150
176
}
151
177
152
- String NTPClient::getFormattedTime () const {
178
+ int NTPClient::getYear () const
179
+ {
180
+ time_t rawtime = this ->getEpochTime ();
181
+ struct tm *ti;
182
+ ti = localtime (&rawtime);
183
+ return ti->tm_year + 1900 ;
184
+ }
185
+
186
+ int NTPClient::getMonth () const
187
+ {
188
+ time_t rawtime = this ->getEpochTime ();
189
+ struct tm *ti;
190
+ ti = localtime (&rawtime);
191
+ return ti->tm_mon + 1 ;
192
+ }
193
+
194
+ int NTPClient::getDate () const
195
+ {
196
+ time_t rawtime = this ->getEpochTime ();
197
+ struct tm *ti;
198
+ ti = localtime (&rawtime);
199
+ return ti->tm_mday ;
200
+ }
201
+
202
+ void NTPClient::getTM_t (tmElements_t &tm ) const
203
+ {
204
+ time_t rawtime = this ->getEpochTime ();
205
+ struct tm *ti;
206
+ ti = localtime (&rawtime);
207
+
208
+ tm .Year = ti->tm_year + 1900 ;
209
+ tm .Month = ti->tm_mon + 1 ;
210
+ tm .Day = ti->tm_mday ;
211
+ tm .Hour = ti->tm_hour ;
212
+ tm .Minute = ti->tm_min ;
213
+ tm .Second = ti->tm_sec ;
214
+ }
215
+
216
+ String NTPClient::getFormattedTime () const
217
+ {
153
218
unsigned long rawTime = this ->getEpochTime ();
154
- unsigned long hours = (rawTime % 86400L ) / 3600 ;
155
- String hoursStr = hours < 10 ? " 0" + String (hours) : String (hours);
219
+ uint8_t hours = (rawTime % 86400L ) / 3600 ;
220
+ uint8_t minutes = (rawTime % 3600 ) / 60 ;
221
+ uint8_t seconds = rawTime % 60 ;
156
222
157
- unsigned long minutes = (rawTime % 3600 ) / 60 ;
158
- String minuteStr = minutes < 10 ? " 0 " + String ( minutes) : String (minutes );
223
+ char *tt = (char *) malloc ( 10 ) ;
224
+ sprintf (tt, " %02u:%02u:%02u " , hours, minutes, seconds );
159
225
160
- unsigned long seconds = rawTime % 60 ;
161
- String secondStr = seconds < 10 ? " 0 " + String (seconds) : String (seconds);
226
+ return String (tt) ;
227
+ }
162
228
163
- return hoursStr + " :" + minuteStr + " :" + secondStr;
229
+ String NTPClient::getFullFormattedTime () const
230
+ {
231
+ time_t rawtime = this ->getEpochTime ();
232
+ struct tm *ti;
233
+ ti = localtime (&rawtime);
234
+
235
+ char *tt = (char *)malloc (20 );
236
+ sprintf (tt, " %02u-%02u-%04u %02u:%02u:%02u" , ti->tm_mday , (ti->tm_mon + 1 ), (ti->tm_year + 1900 ), ti->tm_hour , ti->tm_min , ti->tm_sec );
237
+
238
+ return String (tt);
164
239
}
165
240
166
- void NTPClient::end () {
241
+ void NTPClient::end ()
242
+ {
167
243
this ->_udp ->stop ();
168
244
169
245
this ->_udpSetup = false ;
170
246
}
171
247
172
- void NTPClient::setTimeOffset (int timeOffset) {
173
- this ->_timeOffset = timeOffset;
248
+ void NTPClient::setTimeOffset (int timeOffset)
249
+ {
250
+ this ->_timeOffset = timeOffset;
174
251
}
175
252
176
- void NTPClient::setUpdateInterval (unsigned long updateInterval) {
253
+ void NTPClient::setUpdateInterval (unsigned long updateInterval)
254
+ {
177
255
this ->_updateInterval = updateInterval;
178
256
}
179
257
180
- void NTPClient::setPoolServerName (const char * poolServerName) {
181
- this ->_poolServerName = poolServerName;
258
+ void NTPClient::setPoolServerName (const char *poolServerName)
259
+ {
260
+ this ->_poolServerName = poolServerName;
182
261
}
183
262
184
- void NTPClient::sendNTPPacket () {
263
+ void NTPClient::sendNTPPacket ()
264
+ {
185
265
// set all bytes in the buffer to 0
186
266
memset (this ->_packetBuffer , 0 , NTP_PACKET_SIZE);
187
267
// Initialize values needed to form NTP request
188
- this ->_packetBuffer [0 ] = 0b11100011 ; // LI, Version, Mode
189
- this ->_packetBuffer [1 ] = 0 ; // Stratum, or type of clock
190
- this ->_packetBuffer [2 ] = 6 ; // Polling Interval
191
- this ->_packetBuffer [3 ] = 0xEC ; // Peer Clock Precision
268
+ this ->_packetBuffer [0 ] = 0b11100011 ; // LI, Version, Mode
269
+ this ->_packetBuffer [1 ] = 0 ; // Stratum, or type of clock
270
+ this ->_packetBuffer [2 ] = 6 ; // Polling Interval
271
+ this ->_packetBuffer [3 ] = 0xEC ; // Peer Clock Precision
192
272
// 8 bytes of zero for Root Delay & Root Dispersion
193
- this ->_packetBuffer [12 ] = 49 ;
194
- this ->_packetBuffer [13 ] = 0x4E ;
195
- this ->_packetBuffer [14 ] = 49 ;
196
- this ->_packetBuffer [15 ] = 52 ;
273
+ this ->_packetBuffer [12 ] = 49 ;
274
+ this ->_packetBuffer [13 ] = 0x4E ;
275
+ this ->_packetBuffer [14 ] = 49 ;
276
+ this ->_packetBuffer [15 ] = 52 ;
197
277
198
278
// all NTP fields have been given values, now
199
279
// you can send a packet requesting a timestamp:
200
- if (this ->_poolServerName ) {
280
+ if (this ->_poolServerName )
281
+ {
201
282
this ->_udp ->beginPacket (this ->_poolServerName , 123 );
202
- } else {
283
+ }
284
+ else
285
+ {
203
286
this ->_udp ->beginPacket (this ->_poolServerIP , 123 );
204
287
}
205
288
this ->_udp ->write (this ->_packetBuffer , NTP_PACKET_SIZE);
206
289
this ->_udp ->endPacket ();
207
290
}
208
291
209
- void NTPClient::setRandomPort (unsigned int minValue, unsigned int maxValue) {
292
+ void NTPClient::setRandomPort (unsigned int minValue, unsigned int maxValue)
293
+ {
210
294
randomSeed (analogRead (0 ));
211
295
this ->_port = random (minValue, maxValue);
212
296
}
0 commit comments