Skip to content

Commit 5eff0bd

Browse files
committed
Add begin API
I’ve made calling this mandatory for now.
1 parent 89aa335 commit 5eff0bd

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

NTPClient.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int u
4848
this->_updateInterval = updateInterval;
4949
}
5050

51+
void NTPClient::begin() {
52+
this->begin(NTP_DEFAULT_LOCAL_PORT);
53+
}
54+
55+
void NTPClient::begin(int port) {
56+
this->_port = port;
57+
58+
this->_udp->begin(this->_port);
59+
}
60+
5161
void NTPClient::forceUpdate() {
5262
#ifdef DEBUG_NTPClient
5363
Serial.println("Update from NTP Server");
@@ -81,7 +91,6 @@ void NTPClient::forceUpdate() {
8191
void NTPClient::update() {
8292
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
8393
|| this->_lastUpdate == 0) { // Update if there was no update yet.
84-
if (this->_lastUpdate == 0) this->_udp->begin(this->_port); // Start _udp if there was no update yet.
8594
this->forceUpdate();
8695
}
8796
}

NTPClient.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
#define SEVENZYYEARS 2208988800UL
88
#define NTP_PACKET_SIZE 48
9+
#define NTP_DEFAULT_LOCAL_PORT 1337
910

1011
class NTPClient {
1112
private:
1213
UDP* _udp;
1314

1415
const char* _poolServerName = "time.nist.gov"; // Default time server
15-
int _port = 1337;
16+
int _port = NTP_DEFAULT_LOCAL_PORT;
1617
int _timeOffset = 0;
1718

1819
unsigned int _updateInterval = 60000; // In ms
@@ -31,6 +32,16 @@ class NTPClient {
3132
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
3233
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
3334

35+
/**
36+
* Starts the underlying UDP client with the default local port
37+
*/
38+
void begin();
39+
40+
/**
41+
* Starts the underlying UDP client with the specified local port
42+
*/
43+
void begin(int port);
44+
3445
/**
3546
* This should be called in the main loop of your application. By default an update from the NTP Server is only
3647
* made every 60 seconds. This can be configured in the NTPClient constructor.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void setup(){
3131
Serial.print ( "." );
3232
}
3333

34+
timeClient.begin();
3435
}
3536

3637
void loop() {

examples/Advanced/Advanced.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ void setup(){
2323
delay ( 500 );
2424
Serial.print ( "." );
2525
}
26+
27+
timeClient.begin();
2628
}
2729

2830
void loop() {

examples/Basic/Basic.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void setup(){
2020
delay ( 500 );
2121
Serial.print ( "." );
2222
}
23+
24+
timeClient.begin();
2325
}
2426

2527
void loop() {

0 commit comments

Comments
 (0)