Files
wordclock/include/ntp_client_plus.h

91 lines
2.7 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
} NtpReturnValue;
/**
* @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 *pool_server_name, int utcx, bool sw_change);
bool is_leap_year(unsigned int year);
bool check_daylight_saving_time();
int get_hours_12() const;
int get_hours_24() const;
int get_minutes() const;
int get_month(int dayOfYear);
int get_seconds() const;
int update_ntp();
long get_time_offset();
String get_formatted_date();
String get_formatted_time() const;
unsigned int get_day_of_week();
unsigned int get_year();
unsigned long get_epoch_time() const;
unsigned long get_secs_since_1900() const;
void calc_date();
void end();
void set_pool_server_name(const char *pool_server_name);
void set_time_offset(int time_offset);
void setup_ntp_client();
private:
UDP *_udp;
bool _udp_setup = false;
bool _sw_change = 1;
const char *_pool_server_name = "pool.ntp.org"; // Default time server
int _utcx = 0;
IPAddress _pool_server_ip;
long _time_offset = 0;
unsigned int _port = NTP_DEFAULT_LOCAL_PORT;
unsigned long _update_interval = 60000; // In ms
unsigned int _date_day = 0;
unsigned int _date_month = 0;
unsigned int _date_year = 0;
unsigned int _day_of_week = 0;
unsigned long _current_epoc = 0; // In s
unsigned long _last_secs_since_1900 = 0;
unsigned long _last_update = 0; // In ms
unsigned long _secs_since_1900 = 0; // seconds since 1. Januar 1900, 00:00:00
unsigned char _packet_buffer[NTP_PACKET_SIZE] = {0};
void send_ntp_packet();
void set_summertime(bool summertime);
static const unsigned long milliseconds_per_second = 1000;
static const unsigned long minutes_per_hour = 60;
static const unsigned long seconds_per_day = 86400;
static const unsigned long seconds_per_hour = 3600;
static const unsigned long seconds_per_minute = 60;
// number of days in months
unsigned int _days_per_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
};
void wait(unsigned long time_ms);
#endif /* NTPCLIENTPLUS_H */