2015-12-08 17:21:16 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2016-04-08 22:34:51 +02:00
|
|
|
#include <Udp.h>
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
#define SEVENZYYEARS 2208988800UL
|
2017-01-09 16:49:57 +01:00
|
|
|
#define FRACTIONSPERMILLI (4294967UL)
|
2015-12-08 17:21:16 +01:00
|
|
|
#define NTP_PACKET_SIZE 48
|
2016-04-12 17:44:45 +02:00
|
|
|
#define NTP_DEFAULT_LOCAL_PORT 1337
|
2015-12-08 17:21:16 +01:00
|
|
|
|
2017-01-09 16:49:57 +01:00
|
|
|
class NTPClient;
|
|
|
|
|
|
|
|
typedef void (*NTPUpdateCallbackFunction)(NTPClient* c);
|
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
class NTPClient {
|
|
|
|
private:
|
2016-04-08 22:34:51 +02:00
|
|
|
UDP* _udp;
|
2016-04-12 20:46:48 +02:00
|
|
|
bool _udpSetup = false;
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
const char* _poolServerName = "time.nist.gov"; // Default time server
|
2016-04-12 17:44:45 +02:00
|
|
|
int _port = NTP_DEFAULT_LOCAL_PORT;
|
2016-04-13 15:29:27 +02:00
|
|
|
int _timeOffset = 0;
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
unsigned int _updateInterval = 60000; // In ms
|
2016-10-11 23:37:32 +02:00
|
|
|
unsigned int _retryInterval = 1000; // In ms
|
2015-12-08 17:21:16 +01:00
|
|
|
|
2016-04-13 15:29:27 +02:00
|
|
|
unsigned long _currentEpoc = 0; // In s
|
2017-01-09 16:49:57 +01:00
|
|
|
unsigned long _currentFraction = 0; // In 1/(2^32) s
|
2015-12-08 17:21:16 +01:00
|
|
|
unsigned long _lastUpdate = 0; // In ms
|
2016-10-11 23:37:32 +02:00
|
|
|
unsigned long _lastRequest = 0; // IN ms
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
byte _packetBuffer[NTP_PACKET_SIZE];
|
|
|
|
|
2018-01-16 23:37:22 +01:00
|
|
|
NTPUpdateCallbackFunction _updateCallback = NULL;
|
2017-01-09 16:49:57 +01:00
|
|
|
|
2016-04-08 22:34:51 +02:00
|
|
|
void sendNTPPacket();
|
2016-10-11 23:37:32 +02:00
|
|
|
bool checkResponse();
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
public:
|
2016-04-08 22:34:51 +02:00
|
|
|
NTPClient(UDP& udp);
|
|
|
|
NTPClient(UDP& udp, int timeOffset);
|
|
|
|
NTPClient(UDP& udp, const char* poolServerName);
|
|
|
|
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
|
|
|
|
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
|
2015-12-08 17:21:16 +01:00
|
|
|
|
2016-04-12 17:44:45 +02:00
|
|
|
/**
|
|
|
|
* Starts the underlying UDP client with the default local port
|
|
|
|
*/
|
|
|
|
void begin();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the underlying UDP client with the specified local port
|
|
|
|
*/
|
|
|
|
void begin(int port);
|
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
/**
|
|
|
|
* This should be called in the main loop of your application. By default an update from the NTP Server is only
|
|
|
|
* made every 60 seconds. This can be configured in the NTPClient constructor.
|
2016-04-12 21:30:40 +02:00
|
|
|
*
|
|
|
|
* @return true on success, false on failure
|
2015-12-08 17:21:16 +01:00
|
|
|
*/
|
2016-04-12 21:30:40 +02:00
|
|
|
bool update();
|
2015-12-08 17:21:16 +01:00
|
|
|
|
2016-12-10 05:35:20 +01:00
|
|
|
/**
|
|
|
|
* Has the time ever been sucessfully updated
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool updated();
|
|
|
|
|
2017-01-09 16:49:57 +01:00
|
|
|
/**
|
|
|
|
* Register a callback function for when the time gets updated
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void setUpdateCallback(NTPUpdateCallbackFunction f);
|
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
/**
|
|
|
|
* This will force the update from the NTP Server.
|
2016-10-11 23:37:32 +02:00
|
|
|
* This can block for a full second
|
2016-04-12 21:30:40 +02:00
|
|
|
* @return true on success, false on failure
|
2015-12-08 17:21:16 +01:00
|
|
|
*/
|
2016-04-12 21:30:40 +02:00
|
|
|
bool forceUpdate();
|
2015-12-08 17:21:16 +01:00
|
|
|
|
2016-04-18 21:08:38 +02:00
|
|
|
int getDay();
|
|
|
|
int getHours();
|
|
|
|
int getMinutes();
|
|
|
|
int getSeconds();
|
2015-12-08 17:21:16 +01:00
|
|
|
|
2016-05-14 12:04:18 +02:00
|
|
|
/**
|
|
|
|
* Changes the time offset. Useful for changing timezones dynamically
|
|
|
|
*/
|
|
|
|
void setTimeOffset(int timeOffset);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the update interval to another frequency. E.g. useful when the
|
|
|
|
* timeOffset should not be set in the constructor
|
|
|
|
*/
|
|
|
|
void setUpdateInterval(int updateInterval);
|
|
|
|
|
2015-12-08 17:21:16 +01:00
|
|
|
/**
|
|
|
|
* @return time formatted like `hh:mm:ss`
|
|
|
|
*/
|
|
|
|
String getFormattedTime();
|
|
|
|
|
|
|
|
/**
|
2016-04-13 15:41:16 +02:00
|
|
|
* @return time in seconds since Jan. 1, 1970
|
2015-12-08 17:21:16 +01:00
|
|
|
*/
|
2016-04-13 15:41:16 +02:00
|
|
|
unsigned long getEpochTime();
|
2016-04-12 16:45:21 +02:00
|
|
|
|
2017-01-09 16:49:57 +01:00
|
|
|
/**
|
|
|
|
* @return time in milliseconds since Jan. 1, 1970
|
|
|
|
*/
|
|
|
|
unsigned long long getEpochMillis();
|
|
|
|
|
2016-04-12 16:45:21 +02:00
|
|
|
/**
|
|
|
|
* Stops the underlying UDP client
|
|
|
|
*/
|
|
|
|
void end();
|
2015-12-08 17:21:16 +01:00
|
|
|
};
|