Lightweight WIZnet W5500 NTPClient library. Includes DST and set RTC options.
Dependents: W5500-SNTPClient-example
SNTPClient.h@1:a4f83daca033, 2019-05-02 (annotated)
- Committer:
- star297
- Date:
- Thu May 02 21:06:49 2019 +0000
- Revision:
- 1:a4f83daca033
- Parent:
- 0:f8cb34a007a4
- Child:
- 2:7e5afd34bc43
Edit example code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:f8cb34a007a4 | 1 | |
star297 | 0:f8cb34a007a4 | 2 | #ifndef SNTPCLIENT_H |
star297 | 0:f8cb34a007a4 | 3 | #define SNTPCLIENT_H |
star297 | 0:f8cb34a007a4 | 4 | |
star297 | 0:f8cb34a007a4 | 5 | #include "mbed.h" |
star297 | 0:f8cb34a007a4 | 6 | #include "UDPSocket.h" |
star297 | 0:f8cb34a007a4 | 7 | |
star297 | 0:f8cb34a007a4 | 8 | /** SNTPClient client Class. |
star297 | 0:f8cb34a007a4 | 9 | * |
star297 | 0:f8cb34a007a4 | 10 | * Example (ethernet network): |
star297 | 0:f8cb34a007a4 | 11 | * @code |
star297 | 0:f8cb34a007a4 | 12 | * #include "mbed.h" |
star297 | 0:f8cb34a007a4 | 13 | * #include "EthernetInterface.h" |
star297 | 0:f8cb34a007a4 | 14 | * #include "SNTPClient.h" |
star297 | 0:f8cb34a007a4 | 15 | * |
star297 | 1:a4f83daca033 | 16 | * EthernetInterface eth(MOSI, MISO, SCK, CS, RESET); |
star297 | 0:f8cb34a007a4 | 17 | * SNTPClient sntp; |
star297 | 0:f8cb34a007a4 | 18 | * |
star297 | 0:f8cb34a007a4 | 19 | * int main() { |
star297 | 1:a4f83daca033 | 20 | * |
star297 | 0:f8cb34a007a4 | 21 | * eth.init(); //Use DHCP |
star297 | 0:f8cb34a007a4 | 22 | * eth.connect(); |
star297 | 0:f8cb34a007a4 | 23 | * printf("IP Address is %s\n\r", eth.getIPAddress()); |
star297 | 0:f8cb34a007a4 | 24 | * // example NTPpool = "1.nl.pool.ntp.org" |
star297 | 0:f8cb34a007a4 | 25 | * // get seconds UTC + 1hour, enable DST and set MCU RTC |
star297 | 0:f8cb34a007a4 | 26 | * serial.printf("Seconds since 1970 = %d\r\n", esp.getNTP("1.nl.pool.ntp.org",1,1,1)); |
star297 | 0:f8cb34a007a4 | 27 | * |
star297 | 0:f8cb34a007a4 | 28 | * } |
star297 | 0:f8cb34a007a4 | 29 | * @endcode |
star297 | 0:f8cb34a007a4 | 30 | */ |
star297 | 0:f8cb34a007a4 | 31 | |
star297 | 0:f8cb34a007a4 | 32 | class SNTPClient |
star297 | 0:f8cb34a007a4 | 33 | { |
star297 | 0:f8cb34a007a4 | 34 | public: |
star297 | 0:f8cb34a007a4 | 35 | |
star297 | 0:f8cb34a007a4 | 36 | SNTPClient(); |
star297 | 0:f8cb34a007a4 | 37 | |
star297 | 0:f8cb34a007a4 | 38 | /** Get the NTP date and time and set RTC |
star297 | 0:f8cb34a007a4 | 39 | \param NTPpool The NTP server url eg. "2.es.pool.ntp.org" |
star297 | 0:f8cb34a007a4 | 40 | \param tzffset Timezone offset in hours eg. 3, will add 3 hours to UTC time |
star297 | 0:f8cb34a007a4 | 41 | \param dst Double Summer Time enable (EU only) enable=1, disable=0 |
star297 | 0:f8cb34a007a4 | 42 | \param setRTC Set MCU RTC if successfull enable=1, disable =0 |
star297 | 0:f8cb34a007a4 | 43 | \return the number of seconds since 1970 on success or 0 on failure |
star297 | 0:f8cb34a007a4 | 44 | */ |
star297 | 0:f8cb34a007a4 | 45 | uint32_t getNTP(char * NTPpool, uint32_t tzoffset, bool dst, uint32_t setRTC); |
star297 | 0:f8cb34a007a4 | 46 | |
star297 | 0:f8cb34a007a4 | 47 | private: |
star297 | 0:f8cb34a007a4 | 48 | UDPSocket socket; |
star297 | 0:f8cb34a007a4 | 49 | Endpoint sntp_server; |
star297 | 0:f8cb34a007a4 | 50 | }; |
star297 | 0:f8cb34a007a4 | 51 | |
star297 | 0:f8cb34a007a4 | 52 | #endif |