OmEspHelpers
OmNtp.h
1 /*
2  * OmNtp.h
3  * 2016-12-13
4  *
5  * This class implements a background time service.
6  *
7  * HOW DOES IT WORK
8  *
9  * It is not very accurate. Every couple of minutes it queries a well-known
10  * time server (time.nist.gov), and adds on-board millis() when you ask
11  * for the time. It gives you the time in Hours, Minutes, and Seconds.
12  *
13  * If Wifi is not available, the time is "unknown" or (-1,-1,-1).
14  *
15  * EXAMPLE
16  *
17  * OmNtp ntp;
18  *
19  * in setup():
20  *
21  * ntp.setTimeZone(-8); // for California in the winter
22  *
23  * when wifi is established:
24  *
25  * ntp.setWifiAvailable(true);
26  *
27  * in loop():
28  *
29  * ntp.tick();
30  *
31  * when you want the time:
32  *
33  * int hour, minute, second;
34  * bool gotTheTime = ntp.getTime(hour, minute, second);
35  * const char *string = ntp.getTimeString(); // HH:MM:SS or "unknown"
36  *
37  * Adapted from https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/NTPClient/NTPClient.ino
38  */
39 
40 #ifndef __OmNtp__
41 #define __OmNtp__
42 
43 #include "OmLog.h"
44 #include "WiFiUdp.h"
45 
47 class OmNtp
48 {
49 public:
50  OmNtp();
51 
53  void setWifiAvailable(bool wifiAvailable);
54 
56  void tick(long milliseconds); // pass in the current apparent "millis", we'll pace ourselves accordingly.
57 
60  void setTimeZone(int hourOffset);
61 
62  int getTimeZone();
63 
65  void setLocalTimeZone();
66 
67  /*
68  <?
69  print date("Y-m-d H:i:s");
70  print " + ";
71  print date("e");
72  ?>
73  */
77  void setTimeUrl(const char *timeUrl);
78 
80  bool getTime(int &hourOut, int &minuteOut, int &secondOut);
82  bool getTime(int &hourOut, int &minuteOut, float &secondOut);
83 
86  const char *getTimeString();
87 
88  bool getUTime(uint32_t &uTimeOut, int &uFracOut); // seconds of 1970-1-1, and milliseconds.
89  uint32_t getUTime(); // lite version
90  bool uTimeToTime(uint32_t uTime, int uFrac, int &hourOut, int &minuteOut, float &secondOut); // in local time zone
91 
93  static OmNtp *ntp();
94 
95 private:
96  // Internal implementation methods and details.
97  void begin();
98  void checkForPacket();
99 
100  WiFiUDP udp; // the connection
101  long intervalMilliseconds = 120017; // ticks between NTP queries; 2 minutes
102  int timeZone; // offset the hour reported by this much.
103  unsigned long int uTimeAcquiredMillis = 0; // the millis() when this last time was got
104  unsigned long int uTime = 0; // time since 1970-01-01
105  int hour = -1;
106  int minute = -1;
107  int second = -1;
108  bool begun = false;
109  bool wifiAvailable;
110  static const int kNtpPacketSize = 48;
111  byte packetBuffer[kNtpPacketSize]; //buffer to hold incoming and outgoing packets
112  IPAddress ntpServerIp; // resolved IP address (time.nist.gov)
113 
114  static OmNtp *lastNtpBegun;
115 
116  bool ntpRequestSent = 0; // is set "true" after sending ntp request, and cleared when we get answer.
117 
118  const char *timeUrl = 0;
119  bool localTimeGot = false; // set when we get it, and cleared when used
120  bool localTimeEverGot = false; // set if it has ever ever worked.
121  int localHour = -1;
122  int localMinute = -1;
123  int localSecond = -1;
124  unsigned long int localTimeGotMillis = 0;
125 
126  int localTimeRefetchCountdown = 0; // now and then, reget local time.
127 
128  long countdownMilliseconds = -1;
129  long lastMilliseconds = 0;
130 };
131 
132 #endif // __OmNtp__
OmNtp::ntp
static OmNtp * ntp()
Definition: OmNtp.cpp:326
OmNtp::setWifiAvailable
void setWifiAvailable(bool wifiAvailable)
Definition: OmNtp.cpp:26
OmNtp::getTimeString
const char * getTimeString()
Definition: OmNtp.cpp:156
OmNtp
Definition: OmNtp.h:48
OmNtp::setTimeZone
void setTimeZone(int hourOffset)
Definition: OmNtp.cpp:37
OmNtp::tick
void tick(long milliseconds)
Definition: OmNtp.cpp:265
OmNtp::setTimeUrl
void setTimeUrl(const char *timeUrl)
Definition: OmNtp.cpp:331
OmNtp::getTime
bool getTime(int &hourOut, int &minuteOut, int &secondOut)
Definition: OmNtp.cpp:148
OmNtp::setLocalTimeZone
void setLocalTimeZone()
Definition: OmNtp.cpp:47