-
Notifications
You must be signed in to change notification settings - Fork 1
/
WiFi.h
77 lines (65 loc) · 1.72 KB
/
WiFi.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
#pragma once
#include <WiFi101.h>
#include <WiFiUdp.h>
class WiFiRTPConnection
{
public:
void begin()
{
// Because the confounded device CRASHES if we do this in the constructor, called during global static initialisation
if (WiFi.status() == WL_NO_SHIELD)
{
return;
}
auto Status = WL_IDLE_STATUS;
while (Status != WL_CONNECTED)
{
Status = static_cast<decltype(Status)>(WiFi.begin("nice", "try"));
delay(1000);
}
}
long GetRSSI()
{
return WiFi.RSSI();
}
unsigned long GetEpoch()
{
static const unsigned int LocalPort = 2390;
UDPConnection.begin(LocalPort);
RequestTime();
delay(1000);
uint8_t PacketBuffer[NTP_PACKET_SIZE] = {};
if (UDPConnection.parsePacket())
{
static const unsigned long SeventyYears = 2208988800;
UDPConnection.read(PacketBuffer, NTP_PACKET_SIZE);
unsigned long SecsSince1900 = word(PacketBuffer[40], PacketBuffer[41]) << 16 | word(PacketBuffer[42], PacketBuffer[43]);
UDPConnection.stop();
return (SecsSince1900 - SeventyYears);
}
else
{
UDPConnection.stop();
return 0;
}
}
private:
void RequestTime()
{
uint8_t PacketBuffer[NTP_PACKET_SIZE] = {};
PacketBuffer[0] = 0b11100011;
PacketBuffer[1] = 0;
PacketBuffer[2] = 6;
PacketBuffer[3] = 0xEC;
PacketBuffer[12] = 49;
PacketBuffer[13] = 0x4E;
PacketBuffer[14] = 49;
PacketBuffer[15] = 52;
static const IPAddress TimeServer(129, 6, 15, 28);
UDPConnection.beginPacket(TimeServer, 123);
UDPConnection.write(PacketBuffer, NTP_PACKET_SIZE);
UDPConnection.endPacket();
}
WiFiUDP UDPConnection;
static const size_t NTP_PACKET_SIZE = 48;
};