-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyRTC_functions.h
106 lines (91 loc) · 1.95 KB
/
tinyRTC_functions.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <ESP32Time.h>
#include <RTClib.h>
// RTCESP_DATA_ATTR boolean flag = false; //se indica si el reset ha sido por el mmodo deep sleep
// RTCESP_DATA_ATTR boolean rebootNow = false; //se indica si el reset ha sido por el mmodo deep sleep
// reloj externo
RTC_DS1307 rtc;
DateTime now;
// reloj interno de la esp
ESP32Time rtcESP;
RTC_DATA_ATTR boolean flag = false;
void setTimeRTC(long timeRequest)
{
rtcESP.setTime(timeRequest);
rtc.adjust(DateTime(timeRequest));
now = rtc.now();
}
String getTimeRTC()
{
if (rtc.begin())
{
Serial.println("hora modulo RTC");
now = rtc.now();
return now.timestamp();
}
else
{
Serial.println("hora esp");
return rtcESP.getTime("%FT%T");
}
}
int getMinute()
{
if (rtc.begin())
{
now = rtc.now();
return now.minute();
}else
{
return rtcESP.getMinute();
}
}
void setupRTC()
{
lastTime = millis();
DateTime rtcOneCharge = DateTime(F(__DATE__), F(__TIME__));
long rtcUnixOneCharge = long(rtcOneCharge.unixtime());
long rtcUnix;
boolean noRTC = true;
boolean lastTimeRTC = millis();
while (noRTC)
{
if(!rtc.begin())
{
if (((millis() - lastTimeRTC) > CONECTION_SD_TIME))
{
digitalWrite(LED,true);
noRTC = false;
}
Serial.println("Modulo RTC no conectado");
digitalWrite(LED,stateLed);
delay(500);
stateLed = !stateLed;
}else
{
noRTC = false;
}
}
if (rtc.begin())
{
Serial.println("módulo RTC conectado");
now = rtc.now();
rtcUnix = long(now.unixtime());
if (rtcUnix < rtcUnixOneCharge)
{
rtc.adjust(rtcOneCharge);
now = rtc.now();
Serial.println("hora ajustada");
}
rtcESP.setTime(long(now.unixtime()));
}
else
{
Serial.println("módulo RTC no conectado");
rtcUnix = rtcESP.getEpoch();
if (rtcUnix < rtcUnixOneCharge)
{
rtcESP.setTime(rtcOneCharge.unixtime());
}
}
Serial.println(getTimeRTC());
}