Hello!
I`m new to this and try to understand how to set my RTC, DS3231. I use the library DS1307RTC.h in order to write the RTC time, together with GPS that give me GPS UTC time, in a SD card datalogger. So, if GPS time is not available, RTC time will be available. It works just fine. However, I would like to be able to set the RTC time, by GPS time (if valid) or simply, press a key on serial input (sets the RTC clock after laptop system time.
This is the library: https://github.com/PaulStoffregen/DS1307RTC
What I do not understand, it the (RTC.write(tm)
part.
If try to use (RTC.write(day,month,year) as integers or try my strings from tinygps++:
String Tid=String(gps.time.hour())+(":")+(gps.time.minute())+(":")+(gps.time.second());
String Dato=String(gps.date.day())+("/")+(gps.date.month())+("/")+(gps.date.year());
The RTC it not set. I dont understand what "tm" contains in order to write to RTC. I try to use RTC.set, but no luck.
This is what the library owner write:
RTC.set(t);
Sets the date & time, using a 32 bit "time_t" number. Returns true for success, or false if any error occurs.
RTC.read(tm);
Read the current date & time as TimeElements variable. See the Time library for TimeElements details. Returns true on success, or false if the time could not be read.
https://www.pjrc.com/teensy/td_libs_DS1307RTC.html
Now, I need to compile and load the time set example, evry time I want to set the RTC time.
Anyone that can help me out ?
This is the DS1307RTC.h code for set the RTC. I need to compile and load:
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}