Skip to content

Commit b910b12

Browse files
committed
Merge pull request arduino-libraries#8 from sandeepmistry/all-architectures
Support for other boards and shields
2 parents 056b9b4 + 1fabfc5 commit b910b12

File tree

6 files changed

+48
-31
lines changed

6 files changed

+48
-31
lines changed

NTPClient.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,28 @@
2121

2222
#include "NTPClient.h"
2323

24-
NTPClient::NTPClient() {}
24+
NTPClient::NTPClient(UDP& udp) {
25+
this->_udp = &udp;
26+
}
2527

26-
NTPClient::NTPClient(int timeOffset) {
28+
NTPClient::NTPClient(UDP& udp, int timeOffset) {
29+
this->_udp = &udp;
2730
this->_timeOffset = timeOffset;
2831
}
2932

30-
NTPClient::NTPClient(const char* poolServerName) {
33+
NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
34+
this->_udp = &udp;
3135
this->_poolServerName = poolServerName;
3236
}
3337

34-
NTPClient::NTPClient(const char* poolServerName, int timeOffset) {
38+
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) {
39+
this->_udp = &udp;
3540
this->_timeOffset = timeOffset;
3641
this->_poolServerName = poolServerName;
3742
}
3843

39-
NTPClient::NTPClient(const char* poolServerName, int timeOffset, int updateInterval) {
44+
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) {
45+
this->_udp = &udp;
4046
this->_timeOffset = timeOffset;
4147
this->_poolServerName = poolServerName;
4248
this->_updateInterval = updateInterval;
@@ -47,24 +53,21 @@ void NTPClient::forceUpdate() {
4753
Serial.println("Update from NTP Server");
4854
#endif
4955

50-
IPAddress address;
51-
WiFi.hostByName(this->_poolServerName, address);
52-
53-
this->sendNTPPacket(address);
56+
this->sendNTPPacket();
5457

5558
// Wait till data is there or timeout...
5659
byte timeout = 0;
5760
int cb = 0;
5861
do {
5962
delay ( 10 );
60-
cb = this->_udp.parsePacket();
63+
cb = this->_udp->parsePacket();
6164
if (timeout > 100) return; // timeout after 1000 ms
6265
timeout++;
6366
} while (cb == 0);
6467

6568
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
6669

67-
this->_udp.read(this->_packetBuffer, NTP_PACKET_SIZE);
70+
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
6871

6972
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
7073
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
@@ -78,7 +81,7 @@ void NTPClient::forceUpdate() {
7881
void NTPClient::update() {
7982
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
8083
|| this->_lastUpdate == 0) { // Update if there was no update yet.
81-
if (this->_lastUpdate == 0) this->_udp.begin(this->_port); // Start _udp if there was no update yet.
84+
if (this->_lastUpdate == 0) this->_udp->begin(this->_port); // Start _udp if there was no update yet.
8285
this->forceUpdate();
8386
}
8487
}
@@ -116,7 +119,7 @@ String NTPClient::getFormattedTime() {
116119
return hoursStr + ":" + minuteStr + ":" + secondStr;
117120
}
118121

119-
void NTPClient::sendNTPPacket(IPAddress ip) {
122+
void NTPClient::sendNTPPacket() {
120123
// set all bytes in the buffer to 0
121124
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
122125
// Initialize values needed to form NTP request
@@ -133,8 +136,8 @@ void NTPClient::sendNTPPacket(IPAddress ip) {
133136

134137
// all NTP fields have been given values, now
135138
// you can send a packet requesting a timestamp:
136-
this->_udp.beginPacket(ip, 123); //NTP requests are to port 123
137-
this->_udp.write(this->_packetBuffer, NTP_PACKET_SIZE);
138-
this->_udp.endPacket();
139+
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
140+
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
141+
this->_udp->endPacket();
139142
}
140143

NTPClient.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
#include "Arduino.h"
44

5-
#include <ESP8266WiFi.h>
6-
#include <WiFiUdp.h>
5+
#include <Udp.h>
76

87
#define SEVENZYYEARS 2208988800UL
98
#define NTP_PACKET_SIZE 48
109

1110
class NTPClient {
1211
private:
13-
WiFiUDP _udp;
12+
UDP* _udp;
1413

1514
const char* _poolServerName = "time.nist.gov"; // Default time server
1615
int _port = 1337;
@@ -23,14 +22,14 @@ class NTPClient {
2322

2423
byte _packetBuffer[NTP_PACKET_SIZE];
2524

26-
void sendNTPPacket(IPAddress _timeServerIP);
25+
void sendNTPPacket();
2726

2827
public:
29-
NTPClient();
30-
NTPClient(int timeOffset);
31-
NTPClient(const char* poolServerName);
32-
NTPClient(const char* poolServerName, int timeOffset);
33-
NTPClient(const char* poolServerName, int timeOffset, int updateInterval);
28+
NTPClient(UDP& udp);
29+
NTPClient(UDP& udp, int timeOffset);
30+
NTPClient(UDP& udp, const char* poolServerName);
31+
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
32+
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
3433

3534
/**
3635
* This should be called in the main loop of your application. By default an update from the NTP Server is only

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ Connect to a NTP server, here is how:
44

55
```cpp
66
#include <NTPClient.h>
7+
// change next line to use with another board/shield
78
#include <ESP8266WiFi.h>
9+
//#include <WiFi.h> // for WiFi shield
10+
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
11+
#include <WiFiUdp.h>
812

913
const char *ssid = "<SSID>";
1014
const char *password = "<PASSWORD>";
1115

16+
WiFiUDP ntpUDP(ntpUDP);
17+
1218
// By default 'time.nist.gov' is used.
1319
NTPClient timeClient;
1420

examples/Advanced/Advanced.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#include <NTPClient.h>
2+
// change next line to use with another board/shield
23
#include <ESP8266WiFi.h>
4+
//#include <WiFi.h> // for WiFi shield
5+
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
6+
#include <WiFiUdp.h>
37

48
const char *ssid = "<SSID>";
59
const char *password = "<PASSWORD>";
610

11+
WiFiUDP ntpUDP;
12+
713
// You can specify the time server pool and the offset, (in seconds)
814
// additionaly you can specify the update interval (in milliseconds).
9-
NTPClient timeClient("europe.pool.ntp.org", 3600, 60000);
15+
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
1016

1117
void setup(){
1218
Serial.begin(115200);

examples/Basic/Basic.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#include <NTPClient.h>
2+
// change next line to use with another board/shield
23
#include <ESP8266WiFi.h>
4+
//#include <WiFi.h> // for WiFi shield
5+
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
6+
#include <WiFiUdp.h>
37

48
const char *ssid = "<SSID>";
59
const char *password = "<PASSWORD>";
610

7-
8-
9-
NTPClient timeClient;
11+
WiFiUDP ntpUDP;
12+
NTPClient timeClient(ntpUDP);
1013

1114
void setup(){
1215
Serial.begin(115200);

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=NTPClient
2-
version=2.0.0
2+
version=3.0.0
33
author=Fabrice Weinberg
44
maintainer=Fabrice Weinberg <[email protected]>
55
sentence=An NTPClient to connect to a time server
66
paragraph=Get time from a NTP server and keep it in sync.
77
category=Timing
88
url=https://github.com/FWeinb/NTPClient
9-
architectures=esp8266
9+
architectures=*

0 commit comments

Comments
 (0)