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

Added microseconds functionality #115

Open
wants to merge 1 commit 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
47 changes: 14 additions & 33 deletions NTPClient.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,19 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
this->_poolServerName = poolServerName;
}

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;
this->_poolServerName = poolServerName;
}

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;
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;
this->_poolServerName = NULL;
this->_updateInterval = updateInterval;
}

void NTPClient::begin() {
this->begin(NTP_DEFAULT_LOCAL_PORT);
}
Expand All @@ -86,10 +65,6 @@ bool NTPClient::forceUpdate() {
Serial.println("Update from NTP Server");
#endif

// flush any existing packets
while(this->_udp->parsePacket() != 0)
this->_udp->flush();

this->sendNTPPacket();

// Wait till data is there or timeout...
Expand All @@ -112,9 +87,16 @@ bool NTPClient::forceUpdate() {
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;

uint32_t frac = (uint32_t) this->_packetBuffer[44] << 24
| (uint32_t) this->_packetBuffer[45] << 16
| (uint32_t) this->_packetBuffer[46] << 8
| (uint32_t) this->_packetBuffer[47] << 0;
uint16_t mssec = ((uint64_t) frac * 1000) >> 32;

this->_ms = mssec;
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;

return true; // return true after successful update
return true;
}

bool NTPClient::update() {
Expand All @@ -123,7 +105,7 @@ bool NTPClient::update() {
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
return this->forceUpdate();
}
return false; // return false if update does not occur
return true;
}

unsigned long NTPClient::getEpochTime() const {
Expand All @@ -144,6 +126,9 @@ int NTPClient::getMinutes() const {
int NTPClient::getSeconds() const {
return (this->getEpochTime() % 60);
}
int NTPClient::getMilliSeconds() const {
return (millis()-this->_lastUpdate+this->_ms)%1000;
}

String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime();
Expand All @@ -156,7 +141,7 @@ String NTPClient::getFormattedTime() const {
unsigned long seconds = rawTime % 60;
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);

return hoursStr + ":" + minuteStr + ":" + secondStr;
return hoursStr + ":" + minuteStr + ":" + secondStr+":" +(String) this->getMilliSeconds();
}

void NTPClient::end() {
Expand Down Expand Up @@ -194,11 +179,7 @@ void NTPClient::sendNTPPacket() {

// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
if (this->_poolServerName) {
this->_udp->beginPacket(this->_poolServerName, 123);
} else {
this->_udp->beginPacket(this->_poolServerIP, 123);
}
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
this->_udp->endPacket();
}
7 changes: 3 additions & 4 deletions NTPClient.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class NTPClient {
bool _udpSetup = false;

const char* _poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP;
int _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0;
uint16_t _ms =0;

unsigned long _updateInterval = 60000; // In ms

Expand All @@ -25,6 +25,7 @@ class NTPClient {

byte _packetBuffer[NTP_PACKET_SIZE];


void sendNTPPacket();

public:
Expand All @@ -33,9 +34,6 @@ class NTPClient {
NTPClient(UDP& udp, const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
NTPClient(UDP& udp, IPAddress poolServerIP);
NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset);
NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval);

/**
* Set time server name
Expand Down Expand Up @@ -73,6 +71,7 @@ class NTPClient {
int getHours() const;
int getMinutes() const;
int getSeconds() const;
int getMilliSeconds() const;

/**
* Changes the time offset. Useful for changing timezones dynamically
Expand Down