AceTimeClock  1.3.0
Clock classes for Arduino that can synchronize from an NTP server or an RTC chip
EspSntpClock.h
1 /*
2  * MIT License
3  * Copyright (c) 2022 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_ESP_SNTP_CLOCK_H
7 #define ACE_TIME_ESP_SNTP_CLOCK_H
8 
9 #if defined(ESP8266) || defined(ESP32) || defined(EPOXY_CORE_ESP8266)
10 
11 #include <time.h> // time()
12 #include <AceTime.h> // LocalDate
13 #include "Clock.h"
14 
15 namespace ace_time {
16 namespace clock {
17 
25 class EspSntpClock: public Clock {
26  public:
28  static const char kDefaultNtpServer[];
29 
31  static const uint32_t kDefaultTimeoutMillis = 15000;
32 
33  explicit EspSntpClock() {}
34 
46  bool setup(
47  const char* ntpServer = kDefaultNtpServer,
48  uint32_t timeoutMillis = kDefaultTimeoutMillis);
49 
58  acetime_t getNow() const override {
59  return time(nullptr)
60  - Epoch::secondsToCurrentEpochFromUnixEpoch64();
61  }
62 };
63 
64 }
65 }
66 
67 #endif
68 
69 #endif
Abstract base class for objects that provide and store time.
Definition: Clock.h:19
An implementation of Clock that configures the built-in SNTP client on the ESP8266 and ESP32 using th...
Definition: EspSntpClock.h:25
static const uint32_t kDefaultTimeoutMillis
Default time out for setup().
Definition: EspSntpClock.h:31
bool setup(const char *ntpServer=kDefaultNtpServer, uint32_t timeoutMillis=kDefaultTimeoutMillis)
Setup the SNTP client.
static const char kDefaultNtpServer[]
Default NTP server, "pool.ntp.org".
Definition: EspSntpClock.h:28
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
Definition: EspSntpClock.h:58