Skip to content

Commit fd4ef32

Browse files
committed
First working Arduino compatible version
1 parent eadac40 commit fd4ef32

File tree

6 files changed

+208
-97
lines changed

6 files changed

+208
-97
lines changed

NtpClientLib.cpp

+119-47
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
#include "NtpClientLib.h"
99

10-
#ifdef ARDUINO_ARCH_ESP8266
11-
//NTPClient ntpClient;
12-
1310
boolean ntpClient::instanceFlag = false;
1411
ntpClient *ntpClient::s_client = NULL;
1512

@@ -35,6 +32,7 @@ ntpClient *ntpClient::getInstance(String ntpServerName, int timeOffset, boolean
3532
}*/
3633

3734
#ifdef NTP_TIME_SYNC
35+
#if NETWORK_TYPE == NETWORK_ESP8266
3836
time_t ntpClient::getTime() {
3937
ntpClient *client = s_client;
4038

@@ -106,6 +104,76 @@ time_t ntpClient::getTime() {
106104
return 0;
107105
}
108106
}
107+
#endif //NETWORK_TYPE == ESP8266
108+
109+
#if NETWORK_TYPE == NETWORK_W5100
110+
time_t ntpClient::getTime() {
111+
ntpClient *client = s_client;
112+
DNSClient dns;
113+
114+
#ifdef DEBUG
115+
Serial.println("Starting UDP");
116+
#endif
117+
s_client->_udp.begin(DEFAULT_NTP_PORT);
118+
#ifdef DEBUG
119+
Serial.print("Local port: ");
120+
Serial.println(client->_udp.localPort());
121+
#endif
122+
while (client->_udp.parsePacket() > 0); // discard any previously received packets
123+
dns.begin(Ethernet.dnsServerIP());
124+
uint8_t dnsResult = dns.getHostByName(client->_ntpServerName, client->_timeServerIP);
125+
#ifdef DEBUG
126+
Serial.print("NTP Server hostname: ");
127+
Serial.println(client->_ntpServerName);
128+
Serial.print("NTP Server IP address: ");
129+
Serial.println(client->_timeServerIP);
130+
Serial.print("Result code: ");
131+
Serial.print(dnsResult);
132+
Serial.print(" ");
133+
Serial.println("-- Wifi Connected. Waiting for sync");
134+
Serial.println("-- Transmit NTP Request");
135+
#endif //DEDUG
136+
if (dnsResult == 1) { //If DNS lookup resulted ok
137+
client->sendNTPpacket(client->_timeServerIP);
138+
uint32_t beginWait = millis();
139+
while (millis() - beginWait < 1500) {
140+
int size = client->_udp.parsePacket();
141+
if (size >= NTP_PACKET_SIZE) {
142+
#ifdef DEBUG
143+
Serial.println("-- Receive NTP Response");
144+
#endif
145+
client->_udp.read(client->_ntpPacketBuffer, NTP_PACKET_SIZE); // read packet into the buffer
146+
time_t timeValue = client->decodeNtpMessage(client->_ntpPacketBuffer);
147+
setSyncInterval(client->_longInterval);
148+
#ifdef DEBUG
149+
Serial.println("Sync frequency set low");
150+
#endif // DEBUG
151+
client->_udp.stop();
152+
client->_lastSyncd = timeValue;
153+
#ifdef DEBUG
154+
Serial.printf("Succeccful NTP sync at %s", client->getTimeString(client->_lastSyncd));
155+
#endif // DEBUG
156+
return timeValue;
157+
}
158+
}
159+
#ifdef DEBUG
160+
Serial.println("-- No NTP Response :-(");
161+
#endif //DEBUG
162+
client->_udp.stop();
163+
164+
return 0; // return 0 if unable to get the time
165+
}
166+
else {
167+
#ifdef DEBUG
168+
Serial.println("-- Invalid address :-((");
169+
#endif //DEBUG
170+
client->_udp.stop();
171+
172+
return 0; // return 0 if unable to get the time
173+
}
174+
}
175+
176+
#endif //NETWORK_TYPE == W5100
109177

110178
#endif // NTP_TIME_SYNC
111179

@@ -347,40 +415,6 @@ String ntpClient::printDigits(int digits) {
347415
return _udpPort;
348416
}*/
349417

350-
boolean ntpClient::setInterval(int interval)
351-
{
352-
if (interval >= 10) {
353-
if (_longInterval != interval) {
354-
_longInterval = interval;
355-
#ifdef DEBUG
356-
Serial.println("Sync interval set to " + interval);
357-
#endif // DEBUG
358-
setSyncInterval(interval);
359-
}
360-
return true;
361-
} else
362-
return false;
363-
}
364-
365-
boolean ntpClient::setInterval(int shortInterval, int longInterval) {
366-
if (shortInterval >= 10 && _longInterval >= 10) {
367-
_shortInterval = shortInterval;
368-
_longInterval = longInterval;
369-
if (timeStatus() == timeNotSet) {
370-
setSyncInterval(shortInterval);
371-
} else {
372-
setSyncInterval(longInterval);
373-
}
374-
#ifdef DEBUG
375-
Serial.print("Short sync interval set to "); Serial.println(shortInterval);
376-
Serial.print("Long sync interval set to "); Serial.println(longInterval);
377-
#endif // DEBUG
378-
return true;
379-
} else
380-
return false;
381-
}
382-
383-
384418
int ntpClient::getInterval()
385419
{
386420
return _longInterval;
@@ -391,21 +425,27 @@ int ntpClient::getShortInterval()
391425
return _shortInterval;
392426
}
393427

394-
void ntpClient::setDayLight(boolean daylight)
428+
boolean ntpClient::getDayLight()
395429
{
396-
this->_daylight = daylight;
430+
return this->_daylight;
397431
}
398432

399-
boolean ntpClient::getDayLight()
433+
int ntpClient::getTimeZone()
400434
{
401-
return this->_daylight;
435+
return _timeZone;
402436
}
403437

404438
/*int ntpClient::getLongInterval()
405439
{
406440
return _longInterval;
407441
}*/
408442

443+
444+
String ntpClient::getNtpServerName()
445+
{
446+
return String(_ntpServerName);
447+
}
448+
409449
boolean ntpClient::setNtpServerName(String ntpServerName) {
410450
memset(_ntpServerName, 0, NTP_SERVER_NAME_SIZE);
411451
ntpServerName.toCharArray(_ntpServerName, NTP_SERVER_NAME_SIZE);
@@ -416,11 +456,43 @@ boolean ntpClient::setNtpServerName(String ntpServerName) {
416456
return true;
417457
}
418458

419-
String ntpClient::getNtpServerName()
459+
boolean ntpClient::setInterval(int interval)
420460
{
421-
return String(_ntpServerName);
461+
if (interval >= 10) {
462+
if (_longInterval != interval) {
463+
_longInterval = interval;
464+
#ifdef DEBUG
465+
Serial.println("Sync interval set to " + interval);
466+
#endif // DEBUG
467+
setSyncInterval(interval);
468+
}
469+
return true;
470+
}
471+
else
472+
return false;
422473
}
423474

475+
boolean ntpClient::setInterval(int shortInterval, int longInterval) {
476+
if (shortInterval >= 10 && _longInterval >= 10) {
477+
_shortInterval = shortInterval;
478+
_longInterval = longInterval;
479+
if (timeStatus() == timeNotSet) {
480+
setSyncInterval(shortInterval);
481+
}
482+
else {
483+
setSyncInterval(longInterval);
484+
}
485+
#ifdef DEBUG
486+
Serial.print("Short sync interval set to "); Serial.println(shortInterval);
487+
Serial.print("Long sync interval set to "); Serial.println(longInterval);
488+
#endif // DEBUG
489+
return true;
490+
}
491+
else
492+
return false;
493+
}
494+
495+
424496
boolean ntpClient::setTimeZone(int timeZone)
425497
{
426498
if (timeZone >= -13 || timeZone <= 13) {
@@ -435,9 +507,9 @@ boolean ntpClient::setTimeZone(int timeZone)
435507
return false;
436508
}
437509

438-
int ntpClient::getTimeZone()
510+
void ntpClient::setDayLight(boolean daylight)
439511
{
440-
return _timeZone;
512+
this->_daylight = daylight;
441513
}
442514

443515
//
@@ -454,6 +526,7 @@ boolean ntpClient::summertime(int year, byte month, byte day, byte hour, byte tz
454526
return false;
455527
}
456528

529+
457530
#ifdef NTP_TIME_SYNC
458531
// send an NTP request to the time server at the given address
459532
boolean ntpClient::sendNTPpacket(IPAddress &address) {
@@ -471,7 +544,7 @@ boolean ntpClient::sendNTPpacket(IPAddress &address) {
471544
_ntpPacketBuffer[14] = 49;
472545
_ntpPacketBuffer[15] = 52;
473546
// all NTP fields have been given values, now
474-
// you can send a packet requesting a timestamp:
547+
// you can send a packet requesting a timestamp:
475548
_udp.beginPacket(address, 123); //NTP requests are to port 123
476549
_udp.write(_ntpPacketBuffer, NTP_PACKET_SIZE);
477550
_udp.endPacket();
@@ -483,4 +556,3 @@ time_t ntpClient::getLastNTPSync() {
483556
return _lastSyncd;
484557
}
485558

486-
#endif //ARDUINO_ARCH_ESP8266

0 commit comments

Comments
 (0)