From cf242bb8721aef5d45f874ed5dc1330c50876c17 Mon Sep 17 00:00:00 2001 From: Alexander Drovosekov Date: Thu, 6 Jun 2024 10:59:54 +0300 Subject: [PATCH 1/4] Update NTPClient.h add getDate(), getMonth(), getYear(), getFullFormattedTime(), and getTM_t() --- NTPClient.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/NTPClient.h b/NTPClient.h index a31d32f..1a0a962 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -85,7 +85,11 @@ class NTPClient { int getHours() const; int getMinutes() const; int getSeconds() const; - + int getYear() const; + int getMonth() const; + int getDate() const; + + void getTM_t(tm &ti) const; /** * Changes the time offset. Useful for changing timezones dynamically */ @@ -102,6 +106,8 @@ class NTPClient { */ String getFormattedTime() const; + String getFullFormattedTime() const; + /** * @return time in seconds since Jan. 1, 1970 */ From bf34387d695a07165bedbbf1a98677ab49586f1f Mon Sep 17 00:00:00 2001 From: Alexander Drovosekov Date: Sun, 23 Jun 2024 22:55:19 +0300 Subject: [PATCH 2/4] Update NTPClient.cpp --- NTPClient.cpp | 238 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 161 insertions(+), 77 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index b435855..312bb4e 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -21,59 +21,77 @@ #include "NTPClient.h" -NTPClient::NTPClient(UDP& udp) { - this->_udp = &udp; +NTPClient::NTPClient(UDP &udp) +{ + this->_udp = &udp; } -NTPClient::NTPClient(UDP& udp, long timeOffset) { - this->_udp = &udp; - this->_timeOffset = timeOffset; +NTPClient::NTPClient(UDP &udp, long timeOffset) +{ + this->_udp = &udp; + this->_timeOffset = timeOffset; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName) { - this->_udp = &udp; +NTPClient::NTPClient(UDP &udp, const char *poolServerName) +{ + this->_udp = &udp; this->_poolServerName = poolServerName; } -NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP) { - this->_udp = &udp; - this->_poolServerIP = poolServerIP; +NTPClient::NTPClient(UDP &udp, IPAddress poolServerIP) +{ + this->_udp = &udp; + this->_poolServerIP = poolServerIP; this->_poolServerName = NULL; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) { - this->_udp = &udp; - this->_timeOffset = timeOffset; +NTPClient::NTPClient(UDP &udp, const char *poolServerName, long timeOffset) +{ + this->_udp = &udp; + this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; } -NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset){ - this->_udp = &udp; - this->_timeOffset = timeOffset; - this->_poolServerIP = poolServerIP; +NTPClient::NTPClient(UDP &udp, IPAddress poolServerIP, long timeOffset) +{ + this->_udp = &udp; + this->_timeOffset = timeOffset; + this->_poolServerIP = poolServerIP; this->_poolServerName = NULL; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) { - this->_udp = &udp; - this->_timeOffset = timeOffset; +NTPClient::NTPClient(UDP &udp, const char *poolServerName, long timeOffset, unsigned long updateInterval) +{ + this->_udp = &udp; + this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; this->_updateInterval = updateInterval; } -NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval) { - this->_udp = &udp; - this->_timeOffset = timeOffset; - this->_poolServerIP = poolServerIP; +NTPClient::NTPClient(UDP &udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval) +{ + this->_udp = &udp; + this->_timeOffset = timeOffset; + this->_poolServerIP = poolServerIP; this->_poolServerName = NULL; this->_updateInterval = updateInterval; } -void NTPClient::begin() { +void NTPClient::config_update(UDP &udp, const char *poolServerName, long timeOffset, unsigned long updateInterval) +{ + this->_udp = &udp; + this->_timeOffset = timeOffset; + this->_poolServerName = poolServerName; + this->_updateInterval = updateInterval; +} + +void NTPClient::begin() +{ this->begin(NTP_DEFAULT_LOCAL_PORT); } -void NTPClient::begin(unsigned int port) { +void NTPClient::begin(unsigned int port) +{ this->_port = port; this->_udp->begin(this->_port); @@ -81,13 +99,10 @@ void NTPClient::begin(unsigned int port) { this->_udpSetup = true; } -bool NTPClient::forceUpdate() { - #ifdef DEBUG_NTPClient - Serial.println("Update from NTP Server"); - #endif - +bool NTPClient::forceUpdate() +{ // flush any existing packets - while(this->_udp->parsePacket() != 0) + while (this->_udp->parsePacket() != 0) this->_udp->flush(); this->sendNTPPacket(); @@ -95,10 +110,12 @@ bool NTPClient::forceUpdate() { // Wait till data is there or timeout... byte timeout = 0; int cb = 0; - do { - delay ( 10 ); + do + { + delay(10); cb = this->_udp->parsePacket(); - if (timeout > 100) return false; // timeout after 1000 ms + if (timeout > 100) + return false; // timeout after 1000 ms timeout++; } while (cb == 0); @@ -114,99 +131,166 @@ bool NTPClient::forceUpdate() { this->_currentEpoc = secsSince1900 - SEVENZYYEARS; - return true; // return true after successful update + return true; // return true after successful update } -bool NTPClient::update() { - if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval - || this->_lastUpdate == 0) { // Update if there was no update yet. - if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // setup the UDP client if needed +bool NTPClient::update() +{ + if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval + || this->_lastUpdate == 0) + { // Update if there was no update yet. + if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) + this->begin(this->_port); // setup the UDP client if needed return this->forceUpdate(); } - return false; // return false if update does not occur + return false; // return false if update does not occur } -bool NTPClient::isTimeSet() const { +bool NTPClient::isTimeSet() const +{ return (this->_lastUpdate != 0); // returns true if the time has been set, else false } -unsigned long NTPClient::getEpochTime() const { - return this->_timeOffset + // User offset - this->_currentEpoc + // Epoch returned by the NTP server +unsigned long NTPClient::getEpochTime() const +{ + return this->_timeOffset + // User offset + this->_currentEpoc + // Epoch returned by the NTP server ((millis() - this->_lastUpdate) / 1000); // Time since last update } -int NTPClient::getDay() const { - return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday +int NTPClient::getDay() const +{ + return (((this->getEpochTime() / 86400L) + 4) % 7); // 0 is Sunday } -int NTPClient::getHours() const { - return ((this->getEpochTime() % 86400L) / 3600); +int NTPClient::getHours() const +{ + return ((this->getEpochTime() % 86400L) / 3600); } -int NTPClient::getMinutes() const { +int NTPClient::getMinutes() const +{ return ((this->getEpochTime() % 3600) / 60); } -int NTPClient::getSeconds() const { +int NTPClient::getSeconds() const +{ return (this->getEpochTime() % 60); } -String NTPClient::getFormattedTime() const { +int NTPClient::getYear() const +{ + time_t rawtime = this->getEpochTime(); + struct tm *ti; + ti = localtime(&rawtime); + return ti->tm_year + 1900; +} + +int NTPClient::getMonth() const +{ + time_t rawtime = this->getEpochTime(); + struct tm *ti; + ti = localtime(&rawtime); + return ti->tm_mon + 1; +} + +int NTPClient::getDate() const +{ + time_t rawtime = this->getEpochTime(); + struct tm *ti; + ti = localtime(&rawtime); + return ti->tm_mday; +} + +void NTPClient::getTM_t(tmElements_t &tm) const +{ + time_t rawtime = this->getEpochTime(); + struct tm *ti; + ti = localtime(&rawtime); + + tm.Year = ti->tm_year + 1900; + tm.Month = ti->tm_mon + 1; + tm.Day = ti->tm_mday; + tm.Hour = ti->tm_hour; + tm.Minute = ti->tm_min; + tm.Second = ti->tm_sec; +} + +String NTPClient::getFormattedTime() const +{ unsigned long rawTime = this->getEpochTime(); - unsigned long hours = (rawTime % 86400L) / 3600; - String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); + uint8_t hours = (rawTime % 86400L) / 3600; + uint8_t minutes = (rawTime % 3600) / 60; + uint8_t seconds = rawTime % 60; - unsigned long minutes = (rawTime % 3600) / 60; - String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); + char *tt = (char *)malloc(10); + sprintf(tt, "%02u:%02u:%02u", hours, minutes, seconds); - unsigned long seconds = rawTime % 60; - String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); + return String(tt); +} - return hoursStr + ":" + minuteStr + ":" + secondStr; +String NTPClient::getFullFormattedTime() const +{ + time_t rawtime = this->getEpochTime(); + struct tm *ti; + ti = localtime(&rawtime); + + char *tt = (char *)malloc(20); + sprintf(tt, "%02u-%02u-%04u %02u:%02u:%02u", ti->tm_mday, (ti->tm_mon + 1), (ti->tm_year + 1900), ti->tm_hour, ti->tm_min, ti->tm_sec); + + return String(tt); } -void NTPClient::end() { +void NTPClient::end() +{ this->_udp->stop(); this->_udpSetup = false; } -void NTPClient::setTimeOffset(int timeOffset) { - this->_timeOffset = timeOffset; +void NTPClient::setTimeOffset(int timeOffset) +{ + this->_timeOffset = timeOffset; } -void NTPClient::setUpdateInterval(unsigned long updateInterval) { +void NTPClient::setUpdateInterval(unsigned long updateInterval) +{ this->_updateInterval = updateInterval; } -void NTPClient::setPoolServerName(const char* poolServerName) { - this->_poolServerName = poolServerName; +void NTPClient::setPoolServerName(const char *poolServerName) +{ + this->_poolServerName = poolServerName; } -void NTPClient::sendNTPPacket() { +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 - 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 + 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; + 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: - if (this->_poolServerName) { + if (this->_poolServerName) + { this->_udp->beginPacket(this->_poolServerName, 123); - } else { + } + else + { this->_udp->beginPacket(this->_poolServerIP, 123); } this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); this->_udp->endPacket(); } -void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) { +void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) +{ randomSeed(analogRead(0)); this->_port = random(minValue, maxValue); } From cda1cfa0e5455848c0df9ebecf0f49da90109138 Mon Sep 17 00:00:00 2001 From: Alexander Drovosekov Date: Sun, 23 Jun 2024 23:01:32 +0300 Subject: [PATCH 3/4] Update NTPClient.cpp --- NTPClient.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 312bb4e..307e6b3 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -199,20 +199,6 @@ int NTPClient::getDate() const return ti->tm_mday; } -void NTPClient::getTM_t(tmElements_t &tm) const -{ - time_t rawtime = this->getEpochTime(); - struct tm *ti; - ti = localtime(&rawtime); - - tm.Year = ti->tm_year + 1900; - tm.Month = ti->tm_mon + 1; - tm.Day = ti->tm_mday; - tm.Hour = ti->tm_hour; - tm.Minute = ti->tm_min; - tm.Second = ti->tm_sec; -} - String NTPClient::getFormattedTime() const { unsigned long rawTime = this->getEpochTime(); From 017a61b50ad10378818f6cc51ed6bd6e0eda5cc4 Mon Sep 17 00:00:00 2001 From: Alexander Drovosekov Date: Sun, 23 Jun 2024 23:06:14 +0300 Subject: [PATCH 4/4] Update NTPClient.h --- NTPClient.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NTPClient.h b/NTPClient.h index 1a0a962..83db87a 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -37,6 +37,11 @@ class NTPClient { NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset); NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval); + /** + * Update config by runtime + */ + void config_update(UDP &udp, const char *poolServerName, long timeOffset, unsigned long updateInterval); + /** * Set time server name *