Skip to content

Commit 0167eae

Browse files
committed
Add flag to track if UDP was setup, setup in update if it hasn't been
1 parent 20bcfe4 commit 0167eae

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

NTPClient.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ void NTPClient::begin(int port) {
5656
this->_port = port;
5757

5858
this->_udp->begin(this->_port);
59+
60+
this->_udpSetup = true;
5961
}
6062

6163
void NTPClient::forceUpdate() {
@@ -91,6 +93,7 @@ void NTPClient::forceUpdate() {
9193
void NTPClient::update() {
9294
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
9395
|| this->_lastUpdate == 0) { // Update if there was no update yet.
96+
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
9497
this->forceUpdate();
9598
}
9699
}
@@ -130,6 +133,8 @@ String NTPClient::getFormattedTime() {
130133

131134
void NTPClient::end() {
132135
this->_udp->stop();
136+
137+
this->_udpSetup = false;
133138
}
134139

135140
void NTPClient::sendNTPPacket() {

NTPClient.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class NTPClient {
1212
private:
1313
UDP* _udp;
14+
bool _udpSetup = false;
1415

1516
const char* _poolServerName = "time.nist.gov"; // Default time server
1617
int _port = NTP_DEFAULT_LOCAL_PORT;

0 commit comments

Comments
 (0)