-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNTP.cpp
69 lines (56 loc) · 1.99 KB
/
NTP.cpp
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
#include "NTP.h"
#define DEBUG_NTPClient 1
NTP::NTP() {}
void NTP::init() {
while (!DateTime.isTimeValid()) {
Serial.println("Failed to get time from server, retry.");
DateTime.begin();
delay(500);
}
this->setRTC();
}
void NTP::setRTC(unsigned long timestamp) {
UnixTime stamp(0);
stamp.getDateTime(timestamp);
Serial.println(String(stamp.year)+" "+String(stamp.month)+" "+String(stamp.day)+" "+String(stamp.hour)+" "+String(stamp.minute)+" "+String(stamp.second));
DateTime.setTime(timestamp, true);
DateTimeParts p = DateTime.getParts();
RTC_TimeTypeDef timeStruct;
timeStruct.Seconds = stamp.second;
timeStruct.Minutes = stamp.minute;
timeStruct.Hours = stamp.hour;
M5.Rtc.SetTime(&timeStruct);
RTC_DateTypeDef DateStruct;
DateStruct.Date = stamp.day;
DateStruct.Month = stamp.month;
DateStruct.Year = stamp.year;
DateStruct.WeekDay = stamp.dayOfWeek;
M5.Rtc.SetData(&DateStruct);
/*DateTime.setTime(timestamp, true);
DateTimeParts p = DateTime.getParts();
RTC_TimeTypeDef timeStruct;
timeStruct.Seconds = p.getSeconds();
timeStruct.Minutes = p.getMinutes();
timeStruct.Hours = p.getHours();
M5.Rtc.SetTime(&timeStruct);
RTC_DateTypeDef DateStruct;
DateStruct.Date = p.getMonthDay();
DateStruct.Month = p.getMonth()+1;
DateStruct.Year = p.getYear();
DateStruct.WeekDay = p.getWeekDay();
M5.Rtc.SetData(&DateStruct);*/
}
void NTP::setRTC() {
unsigned long timestamp = DateTime.now();
this->setRTC();
}
unsigned long NTP::getEpoch() {
RTC_TimeTypeDef timeStruct;
RTC_DateTypeDef DateStruct;
M5.Rtc.GetTime(&timeStruct);
M5.Rtc.GetData(&DateStruct);
UnixTime stamp(0);
//Serial.println(String(DateStruct.Year)+" "+String(DateStruct.Month)+" "+String(DateStruct.Date)+" "+String(timeStruct.Hours)+" "+String(timeStruct.Minutes)+" "+String(timeStruct.Seconds));
stamp.setDateTime(DateStruct.Year, DateStruct.Month, DateStruct.Date, timeStruct.Hours, timeStruct.Minutes, timeStruct.Seconds);
return stamp.getUnix();
}