Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NTPClient.h #208

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 147 additions & 77 deletions NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,84 +21,101 @@

#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);

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();

// 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);

Expand All @@ -114,99 +131,152 @@ 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;
}

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;

char *tt = (char *)malloc(10);
sprintf(tt, "%02u:%02u:%02u", hours, minutes, seconds);

unsigned long minutes = (rawTime % 3600) / 60;
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
return String(tt);
}

String NTPClient::getFullFormattedTime() const
{
time_t rawtime = this->getEpochTime();
struct tm *ti;
ti = localtime(&rawtime);

unsigned long seconds = rawTime % 60;
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
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 hoursStr + ":" + minuteStr + ":" + secondStr;
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);
}
13 changes: 12 additions & 1 deletion NTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -85,7 +90,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
*/
Expand All @@ -102,6 +111,8 @@ class NTPClient {
*/
String getFormattedTime() const;

String getFullFormattedTime() const;

/**
* @return time in seconds since Jan. 1, 1970
*/
Expand Down
Loading