// Developed from Arduino DS3232RTC Library by Jack Christensen
// https://github.com/JChristensen/DS3232RTC
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
// and Robert E Bridges (C) 2021
// Example sketch to display the date and time from a RV-30280-C7
// RTC every second.
//
// Set the date and time by entering the following on the Arduino
// serial monitor:
// year,month,day,hour,minute,second,
//
// Where
// year can be two or four digits,
// month is 1-12,
// day is 1-31,
// hour is 0-23, and
// minute and second are 0-59.
// 20,10,26,19,09,00
// Entering the final comma delimiter (after "second") will avoid a
// one-second timeout and will allow the RTC to be set more accurately.
//
// No validity checking is done, invalid values or incomplete syntax
// in the input will result in an incorrect RTC setting.
//
// Robert Bridges 11Jun2021 Jack Christensen 08Aug2013
#include <Arduino.h>
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time
#include <RV-3028-C7.h> // https://github.com/constiko/RV-3028_C7-Arduino_Library
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
RV3028 rtc;
static time_t getUnixTime() {return rtc.getUNIX();}
//static uint32_t getUnixTime() { return rtc.getUNIX(); }
void setup()
{
Serial.begin(115200);
while (!Serial);
Wire.begin();
if (rtc.begin() == false) {
Serial.println("Something went wrong, check wiring");
while (1);
}
else
Serial.println("RTC online!");
// rtc.enableTrickleCharge(TCR_3K); --- I don't use trickle charge
rtc.disableTrickleCharge();
delay(1000);
// setSyncProvider() causes the Time library to synchronize with the
// external RTC by calling getUnixTime() every five minutes by default.
setSyncProvider( getUnixTime );
Serial.println("Just set up synch provider ");
Serial << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
delay(2000);
}
void loop()
{
static time_t tLast;
time_t t;
tmElements_t tm;
// check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
if (Serial.available() >= 12) {
// note that the tmElements_t Year member is an offset from 1970,
// but the RTC wants the last two digits of the calendar year.
// use the convenience macros from the Time Library to do the conversions.
int y = Serial.parseInt();
if (y >= 100 && y < 1000)
Serial << F("Error: Year must be two digits or four digits!") << endl;
else {
if (y >= 1000)
tm.Year = CalendarYrToTm(y);
else // (y < 100)
tm.Year = y2kYearToTm(y);
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
t = makeTime(tm);
rtc.setUNIX(t); // Set rtc UNIX time
rtc.setTime(tm.Second, tm.Minute, tm.Hour, dayOfWeek(t)-1, tm.Day, tm.Month, tm.Year+1970); // Set rtc Time
setTime(t); // Set Teensy time
Serial << F("RTC set to: ");
printTeensyDateTime(t);
PrintRtcDateTime();
PrintUnixTime();
Serial << endl;
// dump any extraneous input
while (Serial.available() > 0) Serial.read();
delay(5000);
}
}
t = now(); //Get's Teensy Time
if (t != tLast) {
tLast = t;
printTeensyDateTime(t);
PrintRtcDateTime();
PrintUnixTime();
Serial.println();
}
}
void PrintUnixTime() {
Serial.print(" Unix Time : ");
Serial.print(rtc.getUNIX());
}
void PrintRtcDateTime() {
if (rtc.updateTime() == false) //Updates the time variables from RTC
{
Serial.print("RTC failed to update");
}
else {
String currentTime = rtc.stringTimeStamp();
Serial.print(" RTC: "); Serial.print(currentTime); // +" \'s\' = set time");
}
}
// print date and time to Serial
void printTeensyDateTime(time_t t)
{
Serial.print("Teensy: ");
printDate(t);
Serial << ' ';
printTime(t);
}
// print time to Serial
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
// print date to Serial
void printDate(time_t t)
{
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
// Print an integer in "00" format (with leading zero),
// followed by a delimiter character to Serial.
// Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return;
}