NTPClient.h 3.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

#include "Arduino.h"

#include <Udp.h>

#define SEVENZYYEARS 2208988800UL
#define NTP_PACKET_SIZE 48
#define NTP_DEFAULT_LOCAL_PORT 1337

class NTPClient {
  private:
    UDP*          _udp;
    bool          _udpSetup       = false;

    const char*   _poolServerName = "pool.ntp.org"; // Default time server
Eric Duminil's avatar
Eric Duminil committed
17
18
19
    IPAddress     _poolServerIP;
    unsigned int  _port           = NTP_DEFAULT_LOCAL_PORT;
    long          _timeOffset     = 0;
20
21
22
23
24
25
26
27
28
29
30
31

    unsigned long _updateInterval = 60000;  // In ms

    unsigned long _currentEpoc    = 0;      // In s
    unsigned long _lastUpdate     = 0;      // In ms

    byte          _packetBuffer[NTP_PACKET_SIZE];

    void          sendNTPPacket();

  public:
    NTPClient(UDP& udp);
Eric Duminil's avatar
Eric Duminil committed
32
    NTPClient(UDP& udp, long timeOffset);
33
    NTPClient(UDP& udp, const char* poolServerName);
Eric Duminil's avatar
Eric Duminil committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    NTPClient(UDP& udp, const char* poolServerName, long timeOffset);
    NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval);
    NTPClient(UDP& udp, IPAddress poolServerIP);
    NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset);
    NTPClient(UDP& udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval);

    /**
     * Set time server name
     *
     * @param poolServerName
     */
    void setPoolServerName(const char* poolServerName);

     /**
     * Set random local port
     */
    void setRandomPort(unsigned int minValue = 49152, unsigned int maxValue = 65535);
51
52
53
54
55
56
57
58
59

    /**
     * Starts the underlying UDP client with the default local port
     */
    void begin();

    /**
     * Starts the underlying UDP client with the specified local port
     */
Eric Duminil's avatar
Eric Duminil committed
60
    void begin(unsigned int port);
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    /**
     * This should be called in the main loop of your application. By default an update from the NTP Server is only
     * made every 60 seconds. This can be configured in the NTPClient constructor.
     *
     * @return true on success, false on failure
     */
    bool update();

    /**
     * This will force the update from the NTP Server.
     *
     * @return true on success, false on failure
     */
    bool forceUpdate();

Eric Duminil's avatar
Eric Duminil committed
77
78
79
80
81
82
83
84
85
86
87
    /**
     * This allows to check if the NTPClient successfully received a NTP packet and set the time.
     *
     * @return true if time has been set, else false
     */
    bool isTimeSet() const;

    int getDay() const;
    int getHours() const;
    int getMinutes() const;
    int getSeconds() const;
88
89
90
91
92
93
94
95
96
97
98
99
100

    /**
     * Changes the time offset. Useful for changing timezones dynamically
     */
    void setTimeOffset(int timeOffset);

    /**
     * Set the update interval to another frequency. E.g. useful when the
     * timeOffset should not be set in the constructor
     */
    void setUpdateInterval(unsigned long updateInterval);

    /**
Eric Duminil's avatar
Eric Duminil committed
101
102
103
     * @return time formatted like `hh:mm:ss`
     */
    String getFormattedTime() const;
104
105
106
107

    /**
     * @return time in seconds since Jan. 1, 1970
     */
Eric Duminil's avatar
Eric Duminil committed
108
    unsigned long getEpochTime() const;
109
110
111
112
113
114

    /**
     * Stops the underlying UDP client
     */
    void end();
};