NTPClient/NTPClient.h

115 lines
3.0 KiB
C
Raw Normal View History

2015-12-08 17:21:16 +01:00
#pragma once
#include "Arduino.h"
#include <Udp.h>
2015-12-08 17:21:16 +01:00
#define SEVENZYYEARS 2208988800UL
#define NTP_PACKET_SIZE 48
#define NTP_DEFAULT_LOCAL_PORT 1337
2015-12-08 17:21:16 +01:00
class NTPClient {
private:
UDP* _udp;
bool _udpSetup = false;
2015-12-08 17:21:16 +01:00
const char* _poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP;
2020-09-03 14:52:08 +02:00
unsigned int _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0;
2015-12-08 17:21:16 +01:00
unsigned long _updateInterval = 60000; // In ms
2015-12-08 17:21:16 +01:00
unsigned long _currentEpoc = 0; // In s
2015-12-08 17:21:16 +01:00
unsigned long _lastUpdate = 0; // In ms
byte _packetBuffer[NTP_PACKET_SIZE];
void sendNTPPacket();
2015-12-08 17:21:16 +01:00
public:
NTPClient(UDP& udp);
NTPClient(UDP& udp, long timeOffset);
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);
2015-12-08 17:21:16 +01:00
2017-10-31 10:01:47 +01:00
/**
* Set time server name
*
* @param poolServerName
*/
void setPoolServerName(const char* poolServerName);
/**
* Set random local port
*/
2020-09-07 17:14:47 +02:00
void setRandomPort(unsigned int minValue = 49152, unsigned int maxValue = 65535);
/**
* Starts the underlying UDP client with the default local port
*/
void begin();
/**
* Starts the underlying UDP client with the specified local port
*/
2020-09-03 14:52:08 +02:00
void begin(unsigned 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.
*
* @return true on success, false on failure
2015-12-08 17:21:16 +01:00
*/
bool update();
2015-12-08 17:21:16 +01:00
/**
* This will force the update from the NTP Server.
*
* @return true on success, false on failure
2015-12-08 17:21:16 +01:00
*/
bool forceUpdate();
2015-12-08 17:21:16 +01:00
/**
* This allows to check if the NTPClient successfully received a NTP packet and set the time.
*
* @return true if time has been set, else false
*/
bool isTimeSet() const;
2018-02-20 21:32:17 +01:00
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
2015-12-08 17:21:16 +01: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(unsigned long updateInterval);
2015-12-08 17:21:16 +01:00
/**
* @return time formatted like `hh:mm:ss`
*/
2018-02-20 21:32:17 +01:00
String getFormattedTime() const;
2015-12-08 17:21:16 +01:00
/**
2016-04-13 15:41:16 +02:00
* @return time in seconds since Jan. 1, 1970
2015-12-08 17:21:16 +01:00
*/
2018-02-20 21:32:17 +01:00
unsigned long getEpochTime() const;
2016-04-12 16:45:21 +02:00
/**
* Stops the underlying UDP client
*/
void end();
2015-12-08 17:21:16 +01:00
};