Skip to content

Commit bf34387

Browse files
authored
Update NTPClient.cpp
1 parent cf242bb commit bf34387

File tree

1 file changed

+161
-77
lines changed

1 file changed

+161
-77
lines changed

NTPClient.cpp

Lines changed: 161 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -21,84 +21,101 @@
2121

2222
#include "NTPClient.h"
2323

24-
NTPClient::NTPClient(UDP& udp) {
25-
this->_udp = &udp;
24+
NTPClient::NTPClient(UDP &udp)
25+
{
26+
this->_udp = &udp;
2627
}
2728

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;
3133
}
3234

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;
3538
this->_poolServerName = poolServerName;
3639
}
3740

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;
4145
this->_poolServerName = NULL;
4246
}
4347

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;
4752
this->_poolServerName = poolServerName;
4853
}
4954

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;
5460
this->_poolServerName = NULL;
5561
}
5662

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;
6067
this->_poolServerName = poolServerName;
6168
this->_updateInterval = updateInterval;
6269
}
6370

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;
6876
this->_poolServerName = NULL;
6977
this->_updateInterval = updateInterval;
7078
}
7179

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+
{
7390
this->begin(NTP_DEFAULT_LOCAL_PORT);
7491
}
7592

76-
void NTPClient::begin(unsigned int port) {
93+
void NTPClient::begin(unsigned int port)
94+
{
7795
this->_port = port;
7896

7997
this->_udp->begin(this->_port);
8098

8199
this->_udpSetup = true;
82100
}
83101

84-
bool NTPClient::forceUpdate() {
85-
#ifdef DEBUG_NTPClient
86-
Serial.println("Update from NTP Server");
87-
#endif
88-
102+
bool NTPClient::forceUpdate()
103+
{
89104
// flush any existing packets
90-
while(this->_udp->parsePacket() != 0)
105+
while (this->_udp->parsePacket() != 0)
91106
this->_udp->flush();
92107

93108
this->sendNTPPacket();
94109

95110
// Wait till data is there or timeout...
96111
byte timeout = 0;
97112
int cb = 0;
98-
do {
99-
delay ( 10 );
113+
do
114+
{
115+
delay(10);
100116
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
102119
timeout++;
103120
} while (cb == 0);
104121

@@ -114,99 +131,166 @@ bool NTPClient::forceUpdate() {
114131

115132
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
116133

117-
return true; // return true after successful update
134+
return true; // return true after successful update
118135
}
119136

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
124144
return this->forceUpdate();
125145
}
126-
return false; // return false if update does not occur
146+
return false; // return false if update does not occur
127147
}
128148

129-
bool NTPClient::isTimeSet() const {
149+
bool NTPClient::isTimeSet() const
150+
{
130151
return (this->_lastUpdate != 0); // returns true if the time has been set, else false
131152
}
132153

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
136158
((millis() - this->_lastUpdate) / 1000); // Time since last update
137159
}
138160

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
141164
}
142-
int NTPClient::getHours() const {
143-
return ((this->getEpochTime() % 86400L) / 3600);
165+
int NTPClient::getHours() const
166+
{
167+
return ((this->getEpochTime() % 86400L) / 3600);
144168
}
145-
int NTPClient::getMinutes() const {
169+
int NTPClient::getMinutes() const
170+
{
146171
return ((this->getEpochTime() % 3600) / 60);
147172
}
148-
int NTPClient::getSeconds() const {
173+
int NTPClient::getSeconds() const
174+
{
149175
return (this->getEpochTime() % 60);
150176
}
151177

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+
{
153218
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;
156222

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);
159225

160-
unsigned long seconds = rawTime % 60;
161-
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
226+
return String(tt);
227+
}
162228

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);
164239
}
165240

166-
void NTPClient::end() {
241+
void NTPClient::end()
242+
{
167243
this->_udp->stop();
168244

169245
this->_udpSetup = false;
170246
}
171247

172-
void NTPClient::setTimeOffset(int timeOffset) {
173-
this->_timeOffset = timeOffset;
248+
void NTPClient::setTimeOffset(int timeOffset)
249+
{
250+
this->_timeOffset = timeOffset;
174251
}
175252

176-
void NTPClient::setUpdateInterval(unsigned long updateInterval) {
253+
void NTPClient::setUpdateInterval(unsigned long updateInterval)
254+
{
177255
this->_updateInterval = updateInterval;
178256
}
179257

180-
void NTPClient::setPoolServerName(const char* poolServerName) {
181-
this->_poolServerName = poolServerName;
258+
void NTPClient::setPoolServerName(const char *poolServerName)
259+
{
260+
this->_poolServerName = poolServerName;
182261
}
183262

184-
void NTPClient::sendNTPPacket() {
263+
void NTPClient::sendNTPPacket()
264+
{
185265
// set all bytes in the buffer to 0
186266
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
187267
// 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
192272
// 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;
197277

198278
// all NTP fields have been given values, now
199279
// you can send a packet requesting a timestamp:
200-
if (this->_poolServerName) {
280+
if (this->_poolServerName)
281+
{
201282
this->_udp->beginPacket(this->_poolServerName, 123);
202-
} else {
283+
}
284+
else
285+
{
203286
this->_udp->beginPacket(this->_poolServerIP, 123);
204287
}
205288
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
206289
this->_udp->endPacket();
207290
}
208291

209-
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
292+
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue)
293+
{
210294
randomSeed(analogRead(0));
211295
this->_port = random(minValue, maxValue);
212296
}

0 commit comments

Comments
 (0)