From 1fabfc5b8d3ef24d3fb5f1b925c0e8a341f68da1 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Fri, 8 Apr 2016 16:34:51 -0400 Subject: [PATCH] Add UDP instance argument in constructor to support more architectures --- NTPClient.cpp | 35 ++++++++++++++++++---------------- NTPClient.h | 17 ++++++++--------- README.md | 6 ++++++ examples/Advanced/Advanced.ino | 8 +++++++- examples/Basic/Basic.ino | 9 ++++++--- library.properties | 4 ++-- 6 files changed, 48 insertions(+), 31 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index f58fbda..3014718 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -21,22 +21,28 @@ #include "NTPClient.h" -NTPClient::NTPClient() {} +NTPClient::NTPClient(UDP& udp) { + this->_udp = &udp; +} -NTPClient::NTPClient(int timeOffset) { +NTPClient::NTPClient(UDP& udp, int timeOffset) { + this->_udp = &udp; this->_timeOffset = timeOffset; } -NTPClient::NTPClient(const char* poolServerName) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName) { + this->_udp = &udp; this->_poolServerName = poolServerName; } -NTPClient::NTPClient(const char* poolServerName, int timeOffset) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) { + this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; } -NTPClient::NTPClient(const char* poolServerName, int timeOffset, int updateInterval) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) { + this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; this->_updateInterval = updateInterval; @@ -47,24 +53,21 @@ void NTPClient::forceUpdate() { Serial.println("Update from NTP Server"); #endif - IPAddress address; - WiFi.hostByName(this->_poolServerName, address); - - this->sendNTPPacket(address); + this->sendNTPPacket(); // Wait till data is there or timeout... byte timeout = 0; int cb = 0; do { delay ( 10 ); - cb = this->_udp.parsePacket(); + cb = this->_udp->parsePacket(); if (timeout > 100) return; // timeout after 1000 ms timeout++; } while (cb == 0); this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time - this->_udp.read(this->_packetBuffer, NTP_PACKET_SIZE); + this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); @@ -78,7 +81,7 @@ 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. + if (this->_lastUpdate == 0) this->_udp->begin(this->_port); // Start _udp if there was no update yet. this->forceUpdate(); } } @@ -116,7 +119,7 @@ String NTPClient::getFormattedTime() { return hoursStr + ":" + minuteStr + ":" + secondStr; } -void NTPClient::sendNTPPacket(IPAddress ip) { +void NTPClient::sendNTPPacket() { // set all bytes in the buffer to 0 memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request @@ -133,8 +136,8 @@ void NTPClient::sendNTPPacket(IPAddress ip) { // all NTP fields have been given values, now // you can send a packet requesting a timestamp: - this->_udp.beginPacket(ip, 123); //NTP requests are to port 123 - this->_udp.write(this->_packetBuffer, NTP_PACKET_SIZE); - this->_udp.endPacket(); + this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123 + this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); + this->_udp->endPacket(); } diff --git a/NTPClient.h b/NTPClient.h index 9c92d42..0af383b 100644 --- a/NTPClient.h +++ b/NTPClient.h @@ -2,15 +2,14 @@ #include "Arduino.h" -#include -#include +#include #define SEVENZYYEARS 2208988800UL #define NTP_PACKET_SIZE 48 class NTPClient { private: - WiFiUDP _udp; + UDP* _udp; const char* _poolServerName = "time.nist.gov"; // Default time server int _port = 1337; @@ -23,14 +22,14 @@ class NTPClient { byte _packetBuffer[NTP_PACKET_SIZE]; - void sendNTPPacket(IPAddress _timeServerIP); + void sendNTPPacket(); public: - NTPClient(); - NTPClient(int timeOffset); - NTPClient(const char* poolServerName); - NTPClient(const char* poolServerName, int timeOffset); - NTPClient(const char* poolServerName, int timeOffset, int updateInterval); + NTPClient(UDP& udp); + NTPClient(UDP& udp, int timeOffset); + NTPClient(UDP& udp, const char* poolServerName); + NTPClient(UDP& udp, const char* poolServerName, int timeOffset); + NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval); /** * This should be called in the main loop of your application. By default an update from the NTP Server is only diff --git a/README.md b/README.md index 05e7e31..9166f24 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,17 @@ Connect to a NTP server, here is how: ```cpp #include +// change next line to use with another board/shield #include +//#include // for WiFi shield +//#include // for WiFi 101 shield or MKR1000 +#include const char *ssid = ""; const char *password = ""; +WiFiUDP ntpUDP(ntpUDP); + // By default 'time.nist.gov' is used. NTPClient timeClient; diff --git a/examples/Advanced/Advanced.ino b/examples/Advanced/Advanced.ino index f359b34..4f2dc1d 100644 --- a/examples/Advanced/Advanced.ino +++ b/examples/Advanced/Advanced.ino @@ -1,12 +1,18 @@ #include +// change next line to use with another board/shield #include +//#include // for WiFi shield +//#include // for WiFi 101 shield or MKR1000 +#include const char *ssid = ""; const char *password = ""; +WiFiUDP ntpUDP; + // You can specify the time server pool and the offset, (in seconds) // additionaly you can specify the update interval (in milliseconds). -NTPClient timeClient("europe.pool.ntp.org", 3600, 60000); +NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000); void setup(){ Serial.begin(115200); diff --git a/examples/Basic/Basic.ino b/examples/Basic/Basic.ino index c22f8b0..776596a 100644 --- a/examples/Basic/Basic.ino +++ b/examples/Basic/Basic.ino @@ -1,12 +1,15 @@ #include +// change next line to use with another board/shield #include +//#include // for WiFi shield +//#include // for WiFi 101 shield or MKR1000 +#include const char *ssid = ""; const char *password = ""; - - -NTPClient timeClient; +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP); void setup(){ Serial.begin(115200); diff --git a/library.properties b/library.properties index 2dba9ad..f4916d8 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=NTPClient -version=2.0.0 +version=3.0.0 author=Fabrice Weinberg maintainer=Fabrice Weinberg sentence=An NTPClient to connect to a time server paragraph=Get time from a NTP server and keep it in sync. category=Timing url=https://github.com/FWeinb/NTPClient -architectures=esp8266 \ No newline at end of file +architectures=*