Added microseconds functionality

This commit is contained in:
Miguel Reis 2020-08-17 12:58:28 +01:00
parent 894b21de11
commit 6cc6336872
2 changed files with 17 additions and 37 deletions

47
NTPClient.cpp Executable file → Normal file
View File

@ -35,25 +35,12 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
this->_poolServerName = 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) { NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) {
this->_udp = &udp; this->_udp = &udp;
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName; 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) { NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) {
this->_udp = &udp; this->_udp = &udp;
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
@ -61,14 +48,6 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsi
this->_updateInterval = updateInterval; 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() { void NTPClient::begin() {
this->begin(NTP_DEFAULT_LOCAL_PORT); this->begin(NTP_DEFAULT_LOCAL_PORT);
} }
@ -86,10 +65,6 @@ bool NTPClient::forceUpdate() {
Serial.println("Update from NTP Server"); Serial.println("Update from NTP Server");
#endif #endif
// flush any existing packets
while(this->_udp->parsePacket() != 0)
this->_udp->flush();
this->sendNTPPacket(); this->sendNTPPacket();
// Wait till data is there or timeout... // Wait till data is there or timeout...
@ -112,9 +87,16 @@ bool NTPClient::forceUpdate() {
// this is NTP time (seconds since Jan 1 1900): // this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord; 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; this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
return true; // return true after successful update return true;
} }
bool NTPClient::update() { bool NTPClient::update() {
@ -123,7 +105,7 @@ bool NTPClient::update() {
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
return this->forceUpdate(); return this->forceUpdate();
} }
return false; // return false if update does not occur return true;
} }
unsigned long NTPClient::getEpochTime() const { unsigned long NTPClient::getEpochTime() const {
@ -144,6 +126,9 @@ int NTPClient::getMinutes() const {
int NTPClient::getSeconds() const { int NTPClient::getSeconds() const {
return (this->getEpochTime() % 60); return (this->getEpochTime() % 60);
} }
int NTPClient::getMilliSeconds() const {
return (millis()-this->_lastUpdate+this->_ms)%1000;
}
String NTPClient::getFormattedTime() const { String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime(); unsigned long rawTime = this->getEpochTime();
@ -156,7 +141,7 @@ String NTPClient::getFormattedTime() const {
unsigned long seconds = rawTime % 60; unsigned long seconds = rawTime % 60;
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
return hoursStr + ":" + minuteStr + ":" + secondStr; return hoursStr + ":" + minuteStr + ":" + secondStr+":" +(String) this->getMilliSeconds();
} }
void NTPClient::end() { void NTPClient::end() {
@ -194,11 +179,7 @@ void NTPClient::sendNTPPacket() {
// all NTP fields have been given values, now // all NTP fields have been given values, now
// you can send a packet requesting a timestamp: // you can send a packet requesting a timestamp:
if (this->_poolServerName) { this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
this->_udp->beginPacket(this->_poolServerName, 123);
} else {
this->_udp->beginPacket(this->_poolServerIP, 123);
}
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
this->_udp->endPacket(); this->_udp->endPacket();
} }

7
NTPClient.h Executable file → Normal file
View File

@ -14,9 +14,9 @@ class NTPClient {
bool _udpSetup = false; bool _udpSetup = false;
const char* _poolServerName = "pool.ntp.org"; // Default time server const char* _poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP;
int _port = NTP_DEFAULT_LOCAL_PORT; int _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0; long _timeOffset = 0;
uint16_t _ms =0;
unsigned long _updateInterval = 60000; // In ms unsigned long _updateInterval = 60000; // In ms
@ -25,6 +25,7 @@ class NTPClient {
byte _packetBuffer[NTP_PACKET_SIZE]; byte _packetBuffer[NTP_PACKET_SIZE];
void sendNTPPacket(); void sendNTPPacket();
public: public:
@ -33,9 +34,6 @@ class NTPClient {
NTPClient(UDP& udp, const char* poolServerName); NTPClient(UDP& udp, const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset); NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval); 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 * Set time server name
@ -73,6 +71,7 @@ class NTPClient {
int getHours() const; int getHours() const;
int getMinutes() const; int getMinutes() const;
int getSeconds() const; int getSeconds() const;
int getMilliSeconds() const;
/** /**
* Changes the time offset. Useful for changing timezones dynamically * Changes the time offset. Useful for changing timezones dynamically