-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTC.ino
88 lines (75 loc) · 2.65 KB
/
RTC.ino
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
/*
#### WaterBunny ####
Module: RTC
Description: Functions to work with the RTC
Copyright (C) 2013 WaterBunny
Torsten Amshove <torsten@amshove.net>, Stefan Moestel <stefan@moestel.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
///////////////////////////////
//// Set RTC Time and Date ////
///////////////////////////////
void rtc_set_time(String dateTime){
tmElements_t newTime;
unsigned int newHour, newMin, newSec, newDay, newMonth, newYear;
char dateTimeChar[20];
if(dateTime.length() != 19){
Serial.println(F("ERROR: Dateformat wrong"));
return;
}
dateTime.toCharArray(dateTimeChar,dateTime.length()+1);
if(sscanf(dateTimeChar,"%d:%d:%d %d-%d-%d",&newHour,&newMin,&newSec,&newDay,&newMonth,&newYear) != 6){
Serial.println(F("ERROR: Dateformat wrong"));
return;
}
newTime.Hour = newHour;
newTime.Minute = newMin;
newTime.Second = newSec;
newTime.Day = newDay;
newTime.Month = newMonth;
newTime.Year = CalendarYrToTm(newYear);
if(RTC.write(newTime)) Serial.println(F("Date and Time successful set"));
else Serial.println(F("ERROR: Date and Time couldn't be set"));
}
//////////////////////////////
//// Get Timestamp of RTC ////
//////////////////////////////
String rtc_get_timestamp(){
String timestamp = "";
RTC.read(rtcTime);
// Format: YYYY-MM-DD HH:MM:SS
timestamp += tmYearToCalendar(rtcTime.Year);
timestamp += "-";
if(rtcTime.Month < 10) timestamp += "0";
timestamp += rtcTime.Month;
timestamp += "-";
if(rtcTime.Day < 10) timestamp += "0";
timestamp += rtcTime.Day;
timestamp += " ";
if(rtcTime.Hour < 10) timestamp += "0";
timestamp += rtcTime.Hour;
timestamp += ":";
if(rtcTime.Minute < 10) timestamp += "0";
timestamp += rtcTime.Minute;
timestamp += ":";
if(rtcTime.Second < 10) timestamp += "0";
timestamp += rtcTime.Second;
return timestamp;
}
/////////////////////
//// Show health ////
/////////////////////
void rtc_health(){
Serial.print(F("RTC TIMESTAMP: "));
Serial.println(rtc_get_timestamp());
}