Add begin API

I’ve made calling this mandatory for now.
This commit is contained in:
Sandeep Mistry 2016-04-12 11:44:45 -04:00
parent 89aa3359a2
commit 5eff0bd78e
5 changed files with 27 additions and 2 deletions

View File

@ -48,6 +48,16 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int u
this->_updateInterval = updateInterval;
}
void NTPClient::begin() {
this->begin(NTP_DEFAULT_LOCAL_PORT);
}
void NTPClient::begin(int port) {
this->_port = port;
this->_udp->begin(this->_port);
}
void NTPClient::forceUpdate() {
#ifdef DEBUG_NTPClient
Serial.println("Update from NTP Server");
@ -81,7 +91,6 @@ void NTPClient::forceUpdate() {
void NTPClient::update() {
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|| this->_lastUpdate == 0) { // Update if there was no update yet.
if (this->_lastUpdate == 0) this->_udp->begin(this->_port); // Start _udp if there was no update yet.
this->forceUpdate();
}
}

View File

@ -6,13 +6,14 @@
#define SEVENZYYEARS 2208988800UL
#define NTP_PACKET_SIZE 48
#define NTP_DEFAULT_LOCAL_PORT 1337
class NTPClient {
private:
UDP* _udp;
const char* _poolServerName = "time.nist.gov"; // Default time server
int _port = 1337;
int _port = NTP_DEFAULT_LOCAL_PORT;
int _timeOffset = 0;
unsigned int _updateInterval = 60000; // In ms
@ -31,6 +32,16 @@ class NTPClient {
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
/**
* Starts the underlying UDP client with the default local port
*/
void begin();
/**
* Starts the underlying UDP client with the specified local port
*/
void begin(int port);
/**
* This should be called in the main loop of your application. By default an update from the NTP Server is only
* made every 60 seconds. This can be configured in the NTPClient constructor.

View File

@ -31,6 +31,7 @@ void setup(){
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {

View File

@ -23,6 +23,8 @@ void setup(){
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {

View File

@ -20,6 +20,8 @@ void setup(){
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {