2015-12-12 16:35:04 +01:00
|
|
|
/**
|
2015-12-08 17:21:16 +01:00
|
|
|
* The MIT License (MIT)
|
|
|
|
* Copyright (c) 2015 by Fabrice Weinberg
|
2015-12-12 16:35:04 +01:00
|
|
|
*
|
2015-12-08 17:21:16 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "NTPClient.h"
|
|
|
|
|
2018-10-20 15:30:00 +02:00
|
|
|
NTPClient::NTPClient(UDP& udp)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2016-04-08 22:34:51 +02:00
|
|
|
this->_udp = &udp;
|
|
|
|
}
|
2015-12-12 16:36:34 +01:00
|
|
|
|
2018-10-20 15:30:00 +02:00
|
|
|
NTPClient::NTPClient(UDP& udp, long timeOffset)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2016-04-08 22:34:51 +02:00
|
|
|
this->_udp = &udp;
|
2015-12-08 17:21:16 +01:00
|
|
|
this->_timeOffset = timeOffset;
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:30:00 +02:00
|
|
|
NTPClient::NTPClient(UDP& udp, const char* poolServerName)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2016-04-08 22:34:51 +02:00
|
|
|
this->_udp = &udp;
|
2015-12-08 17:21:16 +01:00
|
|
|
this->_poolServerName = poolServerName;
|
|
|
|
}
|
|
|
|
|
2020-10-31 21:33:55 +01:00
|
|
|
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2017-04-26 16:14:37 +02:00
|
|
|
this->_udp = &udp;
|
2019-09-19 10:10:00 +02:00
|
|
|
this->_poolServerIP = poolServerIP;
|
2017-04-26 16:14:37 +02:00
|
|
|
this->_poolServerName = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:30:00 +02:00
|
|
|
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2016-04-08 22:34:51 +02:00
|
|
|
this->_udp = &udp;
|
2015-12-08 17:21:16 +01:00
|
|
|
this->_timeOffset = timeOffset;
|
|
|
|
this->_poolServerName = poolServerName;
|
|
|
|
}
|
|
|
|
|
2020-10-31 21:33:55 +01:00
|
|
|
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2019-09-19 10:10:00 +02:00
|
|
|
this->_udp = &udp;
|
|
|
|
this->_timeOffset = timeOffset;
|
|
|
|
this->_poolServerIP = poolServerIP;
|
|
|
|
this->_poolServerName = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:30:00 +02:00
|
|
|
NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2016-04-08 22:34:51 +02:00
|
|
|
this->_udp = &udp;
|
2015-12-08 17:21:16 +01:00
|
|
|
this->_timeOffset = timeOffset;
|
|
|
|
this->_poolServerName = poolServerName;
|
|
|
|
this->_updateInterval = updateInterval;
|
|
|
|
}
|
|
|
|
|
2020-10-31 21:33:55 +01:00
|
|
|
NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval)
|
|
|
|
:_updateStartHandler(NULL),
|
|
|
|
_updateEndHandler(NULL)
|
|
|
|
{
|
2019-09-19 10:10:00 +02:00
|
|
|
this->_udp = &udp;
|
|
|
|
this->_timeOffset = timeOffset;
|
|
|
|
this->_poolServerIP = poolServerIP;
|
|
|
|
this->_poolServerName = NULL;
|
|
|
|
this->_updateInterval = updateInterval;
|
|
|
|
}
|
|
|
|
|
2016-04-12 17:44:45 +02:00
|
|
|
void NTPClient::begin() {
|
|
|
|
this->begin(NTP_DEFAULT_LOCAL_PORT);
|
|
|
|
}
|
|
|
|
|
2020-09-03 14:52:08 +02:00
|
|
|
void NTPClient::begin(unsigned int port) {
|
2016-04-12 17:44:45 +02:00
|
|
|
this->_port = port;
|
|
|
|
|
|
|
|
this->_udp->begin(this->_port);
|
2016-04-12 20:46:48 +02:00
|
|
|
|
|
|
|
this->_udpSetup = true;
|
2016-04-12 17:44:45 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 21:30:40 +02:00
|
|
|
bool NTPClient::forceUpdate() {
|
2015-12-08 17:21:16 +01:00
|
|
|
#ifdef DEBUG_NTPClient
|
|
|
|
Serial.println("Update from NTP Server");
|
|
|
|
#endif
|
2015-12-12 16:35:04 +01:00
|
|
|
|
2018-10-20 15:30:00 +02:00
|
|
|
if (this->_updateStartHandler) {
|
|
|
|
this->_updateStartHandler();
|
|
|
|
}
|
|
|
|
|
2019-01-12 21:32:05 +01:00
|
|
|
// flush any existing packets
|
|
|
|
while(this->_udp->parsePacket() != 0)
|
|
|
|
this->_udp->flush();
|
|
|
|
|
2016-04-08 22:34:51 +02:00
|
|
|
this->sendNTPPacket();
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
// Wait till data is there or timeout...
|
|
|
|
byte timeout = 0;
|
|
|
|
int cb = 0;
|
|
|
|
do {
|
|
|
|
delay ( 10 );
|
2016-04-08 22:34:51 +02:00
|
|
|
cb = this->_udp->parsePacket();
|
2016-04-12 21:30:40 +02:00
|
|
|
if (timeout > 100) return false; // timeout after 1000 ms
|
2015-12-08 17:21:16 +01:00
|
|
|
timeout++;
|
|
|
|
} while (cb == 0);
|
2015-12-12 16:35:04 +01:00
|
|
|
|
|
|
|
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
|
|
|
|
|
2016-04-08 22:34:51 +02:00
|
|
|
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
|
|
|
|
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
|
|
|
|
// combine the four bytes (two words) into a long integer
|
|
|
|
// this is NTP time (seconds since Jan 1 1900):
|
|
|
|
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
2015-12-12 16:35:04 +01:00
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
|
2016-04-12 21:30:40 +02:00
|
|
|
|
2018-10-20 15:30:00 +02:00
|
|
|
if (this->_updateEndHandler) {
|
|
|
|
this->_updateEndHandler();
|
|
|
|
}
|
|
|
|
|
2018-09-17 06:11:26 +02:00
|
|
|
return true; // return true after successful update
|
2015-12-08 17:21:16 +01:00
|
|
|
}
|
|
|
|
|
2016-04-12 21:30:40 +02:00
|
|
|
bool NTPClient::update() {
|
2015-12-12 17:21:39 +01:00
|
|
|
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|
|
|
|
|| this->_lastUpdate == 0) { // Update if there was no update yet.
|
2020-08-31 02:25:05 +02:00
|
|
|
if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // setup the UDP client if needed
|
2016-04-12 21:30:40 +02:00
|
|
|
return this->forceUpdate();
|
2015-12-08 17:21:16 +01:00
|
|
|
}
|
2018-09-17 06:11:26 +02:00
|
|
|
return false; // return false if update does not occur
|
2015-12-08 17:21:16 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:32:17 +01:00
|
|
|
unsigned long NTPClient::getEpochTime() const {
|
2015-12-08 17:21:16 +01:00
|
|
|
return this->_timeOffset + // User offset
|
|
|
|
this->_currentEpoc + // Epoc returned by the NTP server
|
|
|
|
((millis() - this->_lastUpdate) / 1000); // Time since last update
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:32:17 +01:00
|
|
|
int NTPClient::getDay() const {
|
2016-04-18 21:08:38 +02:00
|
|
|
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
|
2016-03-31 07:26:04 +02:00
|
|
|
}
|
2018-02-20 21:32:17 +01:00
|
|
|
int NTPClient::getHours() const {
|
2016-04-18 21:08:38 +02:00
|
|
|
return ((this->getEpochTime() % 86400L) / 3600);
|
2015-12-08 17:21:16 +01:00
|
|
|
}
|
2018-02-20 21:32:17 +01:00
|
|
|
int NTPClient::getMinutes() const {
|
2016-04-18 21:08:38 +02:00
|
|
|
return ((this->getEpochTime() % 3600) / 60);
|
2015-12-08 17:21:16 +01:00
|
|
|
}
|
2018-02-20 21:32:17 +01:00
|
|
|
int NTPClient::getSeconds() const {
|
2016-04-18 21:08:38 +02:00
|
|
|
return (this->getEpochTime() % 60);
|
2015-12-08 17:21:16 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:32:17 +01:00
|
|
|
String NTPClient::getFormattedTime() const {
|
2016-04-13 15:41:16 +02:00
|
|
|
unsigned long rawTime = this->getEpochTime();
|
2015-12-08 17:21:16 +01:00
|
|
|
unsigned long hours = (rawTime % 86400L) / 3600;
|
|
|
|
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
|
2015-12-12 16:35:04 +01:00
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
unsigned long minutes = (rawTime % 3600) / 60;
|
|
|
|
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
|
2015-12-12 16:35:04 +01:00
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
unsigned long seconds = rawTime % 60;
|
|
|
|
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
|
|
|
|
|
|
|
|
return hoursStr + ":" + minuteStr + ":" + secondStr;
|
|
|
|
}
|
|
|
|
|
2016-04-12 16:45:21 +02:00
|
|
|
void NTPClient::end() {
|
|
|
|
this->_udp->stop();
|
2016-04-12 20:46:48 +02:00
|
|
|
|
|
|
|
this->_udpSetup = false;
|
2016-04-12 16:45:21 +02:00
|
|
|
}
|
|
|
|
|
2016-05-14 12:04:18 +02:00
|
|
|
void NTPClient::setTimeOffset(int timeOffset) {
|
|
|
|
this->_timeOffset = timeOffset;
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:17:43 +02:00
|
|
|
void NTPClient::setUpdateInterval(unsigned long updateInterval) {
|
2016-05-14 12:04:18 +02:00
|
|
|
this->_updateInterval = updateInterval;
|
|
|
|
}
|
|
|
|
|
2017-10-31 10:01:47 +01:00
|
|
|
void NTPClient::setPoolServerName(const char* poolServerName) {
|
|
|
|
this->_poolServerName = poolServerName;
|
|
|
|
}
|
|
|
|
|
2016-04-08 22:34:51 +02:00
|
|
|
void NTPClient::sendNTPPacket() {
|
2015-12-08 17:21:16 +01:00
|
|
|
// set all bytes in the buffer to 0
|
|
|
|
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
|
|
|
|
// Initialize values needed to form NTP request
|
|
|
|
// (see URL above for details on the packets)
|
2020-08-31 02:25:05 +02:00
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode
|
|
|
|
this->_packetBuffer[1] = 0; // Stratum, or type of clock
|
|
|
|
this->_packetBuffer[2] = 6; // Polling Interval
|
|
|
|
this->_packetBuffer[3] = 0xEC; // Peer Clock Precision
|
|
|
|
// 8 bytes of zero for Root Delay & Root Dispersion
|
|
|
|
this->_packetBuffer[12] = 49;
|
|
|
|
this->_packetBuffer[13] = 0x4E;
|
|
|
|
this->_packetBuffer[14] = 49;
|
|
|
|
this->_packetBuffer[15] = 52;
|
|
|
|
|
|
|
|
// all NTP fields have been given values, now
|
|
|
|
// you can send a packet requesting a timestamp:
|
2019-09-19 10:10:00 +02:00
|
|
|
if (this->_poolServerName) {
|
|
|
|
this->_udp->beginPacket(this->_poolServerName, 123);
|
|
|
|
} else {
|
|
|
|
this->_udp->beginPacket(this->_poolServerIP, 123);
|
|
|
|
}
|
2016-04-08 22:34:51 +02:00
|
|
|
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
|
|
|
|
this->_udp->endPacket();
|
2015-12-08 17:21:16 +01:00
|
|
|
}
|
2020-08-31 02:25:05 +02:00
|
|
|
|
2020-09-03 14:52:08 +02:00
|
|
|
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
|
2020-08-31 02:25:05 +02:00
|
|
|
randomSeed(analogRead(0));
|
2020-08-31 15:31:55 +02:00
|
|
|
this->_port = random(minValue, maxValue);
|
2020-08-31 02:25:05 +02:00
|
|
|
}
|