-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTCManager.cpp
More file actions
105 lines (87 loc) · 3.11 KB
/
RTCManager.cpp
File metadata and controls
105 lines (87 loc) · 3.11 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
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
#include "RTCManager.h"
#include "Config.h" // Include this to use rtc, pinSDA, and pinSCL
#include "Logging.h"
#include "WebServerManager.h"
#include "TimeSync.h"
#include "PumpManager.h"
#include <RTClib.h>
#include <Wire.h>
#include <Ticker.h>
// RTC DS3231
RTC_DS3231 rtc;
const int pinSDA = 20;
const int pinSCL = 21;
const int sqwPin = 47; // GPIO pin connected to DS3231 SQW
DateTime CurrentTime; // This holds the current time updated periodically
Ticker dateTimeTickerObject; // This is the Ticker object
void setupRTC() {
Wire.begin(pinSDA, pinSCL); // Initialize I2C
if (!rtc.begin()) {
Serial.print("Couldn't find RTC");
while (1);
}
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
}
DateTime getCurrentTime() {
return CurrentTime;
}
void printCurrentTime() {
Serial.print("Current time: ");
Serial.print(CurrentTime.year(), DEC);
Serial.print('/');
Serial.print(CurrentTime.month(), DEC);
Serial.print('/');
Serial.print(CurrentTime.day(), DEC);
Serial.print(" ");
Serial.print(CurrentTime.hour(), DEC);
Serial.print(':');
Serial.print(CurrentTime.minute(), DEC);
Serial.print(':');
Serial.println(CurrentTime.second(), DEC);
}
void adjustTime(const DateTime& dt) {
rtc.adjust(dt);
}
String getCurrentDateString() {
char dateStr[11]; // Buffer for YYYY-MM-DD format
sprintf(dateStr, "%04d-%02d-%02d", CurrentTime.year(), CurrentTime.month(), CurrentTime.day());
return String(dateStr);
}
// 03-17-24 confirmed this function properly reports date/time and is used in the pump on and off logs
String getRtcTimeString() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d %02d:%02d:%02d",
CurrentTime.year(), CurrentTime.month(), CurrentTime.day(),
CurrentTime.hour(), CurrentTime.minute(), CurrentTime.second());
return String(buffer);
}
// Function to get the current year as an integer
int getCurrentYear() {
return CurrentTime.year();
}
String getCurrentDateStringMDY() {
char dateStr[11]; // Enough to hold MM-DD-YYYY\0
sprintf(dateStr, "%02d-%02d-%04d", CurrentTime.month(), CurrentTime.day(), CurrentTime.year());
return String(dateStr);
}
void broadcastDateTime() {
char buffer[32];
// Format the current date and time into the buffer
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
CurrentTime.year(), CurrentTime.month(), CurrentTime.day(),
CurrentTime.hour(), CurrentTime.minute(), CurrentTime.second());
// Use ws to broadcast the buffer's content to all connected WebSocket clients
ws.textAll(buffer);
}
void refreshCurrentTime() {
CurrentTime = rtc.now(); // Refresh the global CurrentTime variable
broadcastDateTime(); // Formats time and then broadcasts through ws (websocket)
}
// not used since we inmplemented tasks
void dateTimeTicker() {
dateTimeTickerObject.attach(1, refreshCurrentTime); // Use the Ticker object here
}