supla-device
Loading...
Searching...
No Matches
DS3231RTC.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15*/
16
17#ifndef SRC_SUPLA_CLOCK_DS3231RTC_H_
18#define SRC_SUPLA_CLOCK_DS3231RTC_H_
19
20/*
21Dependency: https://github.com/adafruit/RTClib,
22use library manager to install it
23*/
24
25#include <supla/log_wrapper.h>
26#include <time.h>
27
28#include "RTClib.h"
29#include "clock.h"
30
31namespace Supla {
32
33class DS3231RTC : public Clock {
34 public:
35 DS3231RTC() {
36 }
37
38 void onInit() {
39 if (!rtc.begin()) {
40 SUPLA_LOG_DEBUG("Unable to find RTC");
41 } else {
42 struct tm timeinfo {};
43 isRTCReady = true;
44 RTCLostPower = (rtc.lostPower()) ? true : false;
45 DateTime now = rtc.now();
46
47 timeinfo.tm_year = now.year() - 1900;
48 timeinfo.tm_mon = now.month() - 1;
49 timeinfo.tm_mday = now.day();
50 timeinfo.tm_hour = now.hour();
51 timeinfo.tm_min = now.minute();
52 timeinfo.tm_sec = now.second();
53
54 localtime = mktime(&timeinfo);
55
56#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
57 timeval tv = {localtime, 0};
58 settimeofday(&tv, nullptr);
59#elif defined(ARDUINO_ARCH_AVR)
60 set_system_time(mktime(&timeinfo));
61#endif
62 printCurrentTime("from RTC:");
63
64 if (getYear() >= 2023) {
65 isClockReady = true;
66 } else {
67 SUPLA_LOG_DEBUG("Clock is not ready");
68 }
69 }
70 }
71
72 bool rtcIsReady() const {
73 return isRTCReady;
74 }
75
76 bool getRTCLostPowerFlag() const {
77 return RTCLostPower;
78 }
79
80 void resetRTCLostPowerFlag() {
81 RTCLostPower = false;
82 }
83
84 void parseLocaltimeFromServer(TSDC_UserLocalTimeResult *result) {
85 struct tm timeinfo {};
86 isClockReady = true;
87 printCurrentTime("current");
88
89 timeinfo.tm_year = result->year - 1900;
90 timeinfo.tm_mon = result->month - 1;
91 timeinfo.tm_mday = result->day;
92 timeinfo.tm_hour = result->hour;
93 timeinfo.tm_min = result->min;
94 timeinfo.tm_sec = result->sec;
95
96 localtime = mktime(&timeinfo);
97
98#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
99 timeval tv = {localtime, 0};
100 settimeofday(&tv, nullptr);
101#elif defined(ARDUINO_ARCH_AVR)
102 set_system_time(mktime(&timeinfo));
103#endif
104
105 printCurrentTime("new");
106 // Update RTC if minutes or seconds are different
107 // from the time obtained from the server
108 if (isRTCReady) {
109 DateTime now = rtc.now();
110 if ((now.year() != getYear()) || (now.month() != getMonth()) ||
111 (now.day() != getDay()) || (now.hour() != getHour()) ||
112 (now.minute() != getMin()) || (now.second() - getSec() > 5) ||
113 (now.second() - getSec() < -5)) {
114 rtc.adjust(DateTime(getTimeStamp()));
115 SUPLA_LOG_DEBUG("Update RTC time from server");
116 }
117 }
118 }
119
120 protected:
121 RTC_DS3231 rtc;
122 bool RTCLostPower = false;
123 bool isRTCReady = false;
124};
125
126}; // namespace Supla
127
128#endif // SRC_SUPLA_CLOCK_DS3231RTC_H_
void onInit()
Third method called on element in SuplaDevice.begin()
Definition DS3231RTC.h:38