AceTime  1.7.4
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
NtpClock.h
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_NTP_CLOCK_H
7 #define ACE_TIME_NTP_CLOCK_H
8 
9 #if defined(ESP8266) || defined(ESP32)
10 
11 #include <stdint.h>
12 #if defined(ESP8266)
13  #include <ESP8266WiFi.h>
14 #else
15  #include <WiFi.h>
16 #endif
17 #include <WiFiUdp.h>
18 #include "Clock.h"
19 
20 #ifndef ACE_TIME_NTP_CLOCK_DEBUG
21 #define ACE_TIME_NTP_CLOCK_DEBUG 0
22 #endif
23 
24 namespace ace_time {
25 namespace clock {
26 
48 class NtpClock: public Clock {
49  public:
51  static const char kNtpServerName[];
52 
54  static const uint16_t kLocalPort = 2390;
55 
57  static const uint16_t kRequestTimeout = 1000;
58 
65  explicit NtpClock(
66  const char* server = kNtpServerName,
67  uint16_t localPort = kLocalPort,
68  uint16_t requestTimeout = kRequestTimeout):
69  mServer(server),
70  mLocalPort(localPort),
71  mRequestTimeout(requestTimeout) {}
72 
83  void setup(
84  const char* ssid = nullptr,
85  const char* password = nullptr,
86  uint16_t connectTimeoutMillis = kConnectTimeoutMillis);
87 
89  const char* getServer() const { return mServer; }
90 
92  bool isSetup() const { return mIsSetUp; }
93 
94  acetime_t getNow() const override;
95 
96  void sendRequest() const override;
97 
98  bool isResponseReady() const override;
99 
100  acetime_t readResponse() const override;
101 
102  private:
104  static const uint8_t kNtpPacketSize = 48;
105 
110  static const uint32_t kSecondsSinceNtpEpoch = 3155673600;
111 
113  static const uint16_t kConnectTimeoutMillis = 10000;
114 
116  void sendNtpPacket(const IPAddress& address) const;
117 
118  private:
119  const char* const mServer;
120  uint16_t const mLocalPort;
121  uint16_t const mRequestTimeout;
122 
123  mutable WiFiUDP mUdp;
124  // buffer to hold incoming & outgoing packets
125  mutable uint8_t mPacketBuffer[kNtpPacketSize];
126  bool mIsSetUp = false;
127 };
128 
129 }
130 }
131 
132 #endif // defined(ESP8266) || defined(ESP32)
133 
134 #endif
ace_time::clock::NtpClock::sendRequest
void sendRequest() const override
Send a time request asynchronously.
Definition: NtpClock.cpp:69
ace_time::clock::NtpClock::kRequestTimeout
static const uint16_t kRequestTimeout
Request time out milliseconds.
Definition: NtpClock.h:57
ace_time::clock::NtpClock::kNtpServerName
static const char kNtpServerName[]
Default NTP Server.
Definition: NtpClock.h:51
ace_time::clock::NtpClock::isResponseReady
bool isResponseReady() const override
Return true if a response is ready.
Definition: NtpClock.cpp:98
ace_time::clock::NtpClock::getServer
const char * getServer() const
Return the name of the NTP server.
Definition: NtpClock.h:89
ace_time::clock::NtpClock::NtpClock
NtpClock(const char *server=kNtpServerName, uint16_t localPort=kLocalPort, uint16_t requestTimeout=kRequestTimeout)
Constructor.
Definition: NtpClock.h:65
ace_time::clock::NtpClock
A Clock that retrieves the time from an NTP server.
Definition: NtpClock.h:48
ace_time::clock::NtpClock::getNow
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
Definition: NtpClock.cpp:55
ace_time::clock::Clock
Abstract base class for objects that provide and store time.
Definition: Clock.h:20
ace_time::clock::NtpClock::readResponse
acetime_t readResponse() const override
Returns number of seconds since AceTime epoch (2000-01-01).
Definition: NtpClock.cpp:121
ace_time::clock::NtpClock::setup
void setup(const char *ssid=nullptr, const char *password=nullptr, uint16_t connectTimeoutMillis=kConnectTimeoutMillis)
Set up the WiFi connection using the given ssid and password, and prepare the UDP connection.
Definition: NtpClock.cpp:18
ace_time::clock::NtpClock::isSetup
bool isSetup() const
Return true if setup() suceeded.
Definition: NtpClock.h:92
ace_time::clock::NtpClock::kLocalPort
static const uint16_t kLocalPort
Default port used for UDP packets.
Definition: NtpClock.h:54