Skip to content

Commit 894b21d

Browse files
authored
Merge pull request #77 from arduino-libraries/add-missing-ctors
Added support for server IPAddress argument to NTPClient
2 parents ff9775d + a800869 commit 894b21d

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

NTPClient.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,38 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
3737

3838
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP) {
3939
this->_udp = &udp;
40-
this->_poolServerIP = poolServerIP;
40+
this->_poolServerIP = poolServerIP;
4141
this->_poolServerName = NULL;
4242
}
4343

44-
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) {
44+
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) {
4545
this->_udp = &udp;
4646
this->_timeOffset = timeOffset;
4747
this->_poolServerName = poolServerName;
4848
}
4949

50+
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset){
51+
this->_udp = &udp;
52+
this->_timeOffset = timeOffset;
53+
this->_poolServerIP = poolServerIP;
54+
this->_poolServerName = NULL;
55+
}
56+
5057
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) {
5158
this->_udp = &udp;
5259
this->_timeOffset = timeOffset;
5360
this->_poolServerName = poolServerName;
5461
this->_updateInterval = updateInterval;
5562
}
5663

64+
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval) {
65+
this->_udp = &udp;
66+
this->_timeOffset = timeOffset;
67+
this->_poolServerIP = poolServerIP;
68+
this->_poolServerName = NULL;
69+
this->_updateInterval = updateInterval;
70+
}
71+
5772
void NTPClient::begin() {
5873
this->begin(NTP_DEFAULT_LOCAL_PORT);
5974
}
@@ -179,7 +194,11 @@ void NTPClient::sendNTPPacket() {
179194

180195
// all NTP fields have been given values, now
181196
// you can send a packet requesting a timestamp:
182-
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
197+
if (this->_poolServerName) {
198+
this->_udp->beginPacket(this->_poolServerName, 123);
199+
} else {
200+
this->_udp->beginPacket(this->_poolServerIP, 123);
201+
}
183202
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
184203
this->_udp->endPacket();
185204
}

NTPClient.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class NTPClient {
1414
bool _udpSetup = false;
1515

1616
const char* _poolServerName = "pool.ntp.org"; // Default time server
17+
IPAddress _poolServerIP;
1718
int _port = NTP_DEFAULT_LOCAL_PORT;
1819
long _timeOffset = 0;
1920

@@ -30,9 +31,11 @@ class NTPClient {
3031
NTPClient(UDP& udp);
3132
NTPClient(UDP& udp, long timeOffset);
3233
NTPClient(UDP& udp, const char* poolServerName);
33-
NTPClient(UDP& udp, IPAddress poolServerIP);
3434
NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
3535
NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
36+
NTPClient(UDP& udp, IPAddress poolServerIP);
37+
NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset);
38+
NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval);
3639

3740
/**
3841
* Set time server name

0 commit comments

Comments
 (0)