-
Notifications
You must be signed in to change notification settings - Fork 0
/
NTP.h
84 lines (68 loc) · 1.74 KB
/
NTP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef NTP_H_INCLUDED
#define NTP_H_INCLUDED
/*
* NTP.h
*
* Author: Hiromasa Ihara (taisyo)
* Created: 2016-04-22
*/
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Dns.h>
#define NTP_DEFAULT_TIMEOUT_MS 5000
#define NTP_PORT 123
class NTPClient
{
public:
void begin(){
}
int getTime(String server_host, uint32_t *timestamp1970){
DNSClient dns;
IPAddress server_ip;
uint32_t timestamp1900; // ntp timestamp
dns.begin(Ethernet.dnsServerIP());
dns.getHostByName(server_host.c_str(), server_ip);
Udp.begin(1024+(millis() & 0xFF));
sendPacket(server_ip);
recvPacket(NTP_DEFAULT_TIMEOUT_MS, timestamp1900);
*timestamp1970 = convert1900to1970(timestamp1900);
return 0;
}
private:
uint32_t convert1900to1970(uint32_t timestamp1900)
{
return timestamp1900 - 2208988800UL;
}
int sendPacket(IPAddress server_ip){
Udp.beginPacket(server_ip, NTP_PORT);
uint8_t oneByteBuf;
oneByteBuf = 0b00001011;
Udp.write(&oneByteBuf, 1);
for (int i = 1; i < 48; i++) {
oneByteBuf = 0;
Udp.write(&oneByteBuf, 1);
}
Udp.endPacket();
}
int recvPacket(unsigned long timeout_ms, uint32_t ×tamp1900){
unsigned int start_ms = millis();
uint8_t oneByteBuf;
while(!Udp.parsePacket()){
if(millis() - start_ms > timeout_ms){
return -1;
}
}
for (int i = 0; i < 40; i++) {
Udp.read(&oneByteBuf, 1);
}
timestamp1900 = 0;
for (int i = 40; i < 44; i++) {
Udp.read(&oneByteBuf, 1);
timestamp1900 <<= 8;
timestamp1900 += oneByteBuf;
}
return 0;
}
EthernetUDP Udp;
};
#endif /* NTP_H_INCLUDED */