-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.h
More file actions
72 lines (64 loc) · 1.59 KB
/
clock.h
File metadata and controls
72 lines (64 loc) · 1.59 KB
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
#pragma once
#include <Wire.h>
#include <time.h>
#include <RTClib.h>
/*
int _EXFUN(settimeofday, (const struct timeval *, const struct timezone *));
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
*/
#define DS3231_ADDRESS 0
#define NTP_CHECK_DELAY 300000L
// 01/01/2018
#define DEF_TIME 1514764800
RTC_DS3231 rtc;
int32_t timeZone = 0;
uint8_t ntpId = 0;
uint32_t syncRTC() {
WDEBUG("TimeZone: %s, TimeOffset: %d\n", tz.c_str(), timeZone);
if (time(NULL) > DEF_TIME) {
status.ntpSync = true;
if (status.rtcPresent) {
rtc.adjust(DateTime(time(NULL)));
status.rtcValid = !rtc.lostPower();
}
}
return NTP_CHECK_DELAY;
}
//Update time from NTP server
uint32_t initNTP() {
timeZone = atoi(tz.c_str()) * 3600;
configTime(timeZone, 0, ntp1.c_str(), ntp2.c_str(), ntp3.c_str());
taskAddWithDelay(syncRTC, NTP_CHECK_DELAY);
return RUN_DELETE;
}
time_t getTime() {
time_t t = 0;
t = time(NULL);
return t % 86400;
}
uint32_t initRTC() {
Wire.begin(SDA,SCL);
rtc.begin();
Wire.beginTransmission(DS3231_ADDRESS); // Check if RTC is
status.rtcPresent = (Wire.endTransmission() == 0); // present
status.rtcValid = !rtc.lostPower();
if (status.rtcValid) {
//int _EXFUN(settimeofday, (const struct timeval *, const struct timezone *));
timezone tzs;
tzs.tz_minuteswest = timeZone / 60;
tzs.tz_dsttime = 0;
timeval tvs;
DateTime now = rtc.now();
tvs.tv_sec = now.unixtime();
tvs.tv_usec = 0;
settimeofday(&tvs, &tzs);
}
return RUN_DELETE;
}