Files
wordclock/include/ntp_client_plus.h
2023-08-21 20:26:38 +02:00

91 lines
2.6 KiB
C++

#ifndef NTPCLIENTPLUS_H
#define NTPCLIENTPLUS_H
#include <Arduino.h>
#include <WiFiUdp.h>
#define UNIX_TIMESTAMP_1900 2208988800UL // careful: positive value
#define NTP_PACKET_SIZE 48
#define NTP_DEFAULT_LOCAL_PORT 1337
#define MAX_NTP_CONN_TRIES 50 // 50 * NTP_RECEIVE_WAIT_TIME_MS => 500ms
#define NTP_RECEIVE_WAIT_TIME_MS 10 // 10ms
typedef enum
{
NTP_UPDATE_TIMEOUT = -1,
NTP_UPDATE_SUCCESS = 0,
NTP_UPDATE_DIFFTOOHIGH = 1,
NTP_UPDATE_TIME_INVALID = 2
} ntp_return_values;
/**
* @brief Own NTP Client library for Arduino with code from:
* - https://github.com/arduino-libraries/NTPClient
* - SPS&Technik - Projekt WordClock v1.02
*
*/
class NTPClientPlus
{
public:
NTPClientPlus(UDP &udp, const char *poolServerName, int utcx, bool _swChange);
void setupNTPClient();
int updateNTP();
void end();
void setTimeOffset(int timeOffset);
void setPoolServerName(const char *poolServerName);
unsigned long getSecsSince1900() const;
unsigned long getEpochTime() const;
int getHours24() const;
int getHours12() const;
int getMinutes() const;
int getSeconds() const;
String getFormattedTime() const;
String getFormattedDate();
void calcDate();
unsigned int getDayOfWeek();
unsigned int getYear();
bool isLeapYear(unsigned int year);
int getMonth(int dayOfYear);
long getTimeOffset();
bool updateSWChange();
private:
UDP *_udp;
bool _udpSetup = false;
bool _swChange = 1;
const char *_poolServerName = "pool.ntp.org"; // Default time server
int _utcx = 0;
IPAddress _poolServerIP;
long _timeOffset = 0;
unsigned int _port = NTP_DEFAULT_LOCAL_PORT;
unsigned long _updateInterval = 60000; // In ms
unsigned long _currentEpoc = 0; // In s
unsigned long _lastUpdate = 0; // In ms
unsigned long _secsSince1900 = 0; // seconds since 1. Januar 1900, 00:00:00
unsigned long _lastSecsSince1900 = 0;
unsigned int _dateYear = 0;
unsigned int _dateMonth = 0;
unsigned int _dateDay = 0;
unsigned int _dayOfWeek = 0;
unsigned char _packetBuffer[NTP_PACKET_SIZE] = {0};
void sendNTPPacket();
void setSummertime(bool summertime);
static const unsigned long secondperday = 86400;
static const unsigned long secondperhour = 3600;
static const unsigned long secondperminute = 60;
static const unsigned long minuteperhour = 60;
static const unsigned long millisecondpersecond = 1000;
// number of days in months
unsigned int daysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
};
void wait(unsigned long time);
#endif /* NTPCLIENTPLUS_H */