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
|
|
|
|
#define NTP_PACKET_SIZE 48
|
|
|
|
|
|
|
|
class NTPClient {
|
|
|
|
private:
|
2016-04-08 22:34:51 +02:00
|
|
|
UDP* _udp;
|
2015-12-08 17:21:16 +01:00
|
|
|
|
|
|
|
const char* _poolServerName = "time.nist.gov"; // Default time server
|
|
|
|
int _port = 1337;
|
|
|
|
int _timeOffset;
|
|
|
|
|
|
|
|
unsigned int _updateInterval = 60000; // In ms
|
|
|
|
|
|
|
|
unsigned long _currentEpoc; // In s
|
|
|
|
unsigned long _lastUpdate = 0; // In ms
|
|
|
|
|
|
|
|
byte _packetBuffer[NTP_PACKET_SIZE];
|
|
|
|
|
2016-04-08 22:34:51 +02:00
|
|
|
void sendNTPPacket();
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
void update();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will force the update from the NTP Server.
|
|
|
|
*/
|
|
|
|
void forceUpdate();
|
|
|
|
|
2016-03-31 07:26:04 +02:00
|
|
|
String getDay();
|
2015-12-08 17:21:16 +01:00
|
|
|
String getHours();
|
|
|
|
String getMinutes();
|
|
|
|
String getSeconds();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return time formatted like `hh:mm:ss`
|
|
|
|
*/
|
|
|
|
String getFormattedTime();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return time as raw seconds
|
|
|
|
*/
|
|
|
|
unsigned long getRawTime();
|
|
|
|
};
|