supla-device
Loading...
Searching...
No Matches
DS3231RTC.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_CLOCK_DS3231RTC_H_
5#define SRC_SUPLA_CLOCK_DS3231RTC_H_
6
7/*
8Dependency: https://github.com/adafruit/RTClib,
9use library manager to install it
10*/
11
12#include <supla/log_wrapper.h>
13#include <time.h>
14
15#include "RTClib.h"
16#include "clock.h"
17
18namespace Supla {
19
20class DS3231RTC : public Clock {
21 public:
22 DS3231RTC() {
23 }
24
25 void onInit() {
26 if (!rtc.begin()) {
27 SUPLA_LOG_DEBUG("Unable to find RTC");
28 } else {
29 struct tm timeinfo {};
30 isRTCReady = true;
31 RTCLostPower = (rtc.lostPower()) ? true : false;
32 DateTime now = rtc.now();
33
34 timeinfo.tm_year = now.year() - 1900;
35 timeinfo.tm_mon = now.month() - 1;
36 timeinfo.tm_mday = now.day();
37 timeinfo.tm_hour = now.hour();
38 timeinfo.tm_min = now.minute();
39 timeinfo.tm_sec = now.second();
40
41 localtime = mktime(&timeinfo);
42
43#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
44 timeval tv = {localtime, 0};
45 settimeofday(&tv, nullptr);
46#elif defined(ARDUINO_ARCH_AVR)
47 set_system_time(mktime(&timeinfo));
48#endif
49 printCurrentTime("from RTC:");
50
51 if (getYear() >= 2023) {
52 isClockReady = true;
53 } else {
54 SUPLA_LOG_DEBUG("Clock is not ready");
55 }
56 }
57 }
58
59 bool rtcIsReady() const {
60 return isRTCReady;
61 }
62
63 bool getRTCLostPowerFlag() const {
64 return RTCLostPower;
65 }
66
67 void resetRTCLostPowerFlag() {
68 RTCLostPower = false;
69 }
70
71 void parseLocaltimeFromServer(TSDC_UserLocalTimeResult *result) {
72 struct tm timeinfo {};
73 isClockReady = true;
74 printCurrentTime("current");
75
76 timeinfo.tm_year = result->year - 1900;
77 timeinfo.tm_mon = result->month - 1;
78 timeinfo.tm_mday = result->day;
79 timeinfo.tm_hour = result->hour;
80 timeinfo.tm_min = result->min;
81 timeinfo.tm_sec = result->sec;
82
83 localtime = mktime(&timeinfo);
84
85#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
86 timeval tv = {localtime, 0};
87 settimeofday(&tv, nullptr);
88#elif defined(ARDUINO_ARCH_AVR)
89 set_system_time(mktime(&timeinfo));
90#endif
91
92 printCurrentTime("new");
93 // Update RTC if minutes or seconds are different
94 // from the time obtained from the server
95 if (isRTCReady) {
96 DateTime now = rtc.now();
97 if ((now.year() != getYear()) || (now.month() != getMonth()) ||
98 (now.day() != getDay()) || (now.hour() != getHour()) ||
99 (now.minute() != getMin()) || (now.second() - getSec() > 5) ||
100 (now.second() - getSec() < -5)) {
101 rtc.adjust(DateTime(getTimeStamp()));
102 SUPLA_LOG_DEBUG("Update RTC time from server");
103 }
104 }
105 }
106
107 protected:
108 RTC_DS3231 rtc;
109 bool RTCLostPower = false;
110 bool isRTCReady = false;
111};
112
113}; // namespace Supla
114
115#endif // SRC_SUPLA_CLOCK_DS3231RTC_H_
Definition clock.h:16
Definition DS3231RTC.h:20
void onInit()
Third method called on element in SuplaDevice.begin()
Definition DS3231RTC.h:25
Definition proto.h:2913