//Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
#define TIME_HEADER "T"
#define TIME_UNIX "U"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
Serial.println("Set the RTC by sending \"T\"year, month, day, hour, minute, second");
Serial.println("For example T2025,3,12,15,25,0 when your pc system clock hits the time you");
Serial.println("hit [Return]");
Serial.println("If you are off by a second or so, have another go!!!");
Serial.println("////////Get your local time @ https://time.is");
/*
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
*/
}
char timeStr[10] = {0};
char dateStr[12] = {0};
uint32_t lastTime = 0;
bool Set = false;
void printDateTime() {
DateTime now = rtc.now();
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
sprintf(dateStr, " %d/%d/%d", now.day(), now.month(), now.year());
sprintf(timeStr, " %02d:%02d:%02d", now.hour(), now.minute(), now.second());
Serial.print(dateStr);
Serial.println(timeStr);
Serial.println(now.unixtime());
Serial.print("Temperature: ");
Serial.print(rtc.getTemperature());
Serial.println("°C\n");
}
void loop () {
if (Set) {
if (millis() - lastTime > 1000) {
lastTime = millis();
printDateTime();
}
}
////////Set the RTC by sending "T"year, month, day, hour, minute, second//////
////////For example T2025,3,12,15,25,0 when your pc system clock hits the time you
////////hit [Return]
////////If you are off by a second or so, have another go!!!
////////Get your local time @ https://time.is
while (Serial.available()) {
char c = Serial.read();
delay(10);
if (c == 'T') {
uint16_t yr = Serial.parseInt();
uint8_t mo = Serial.parseInt();
uint8_t dy = Serial.parseInt();
uint8_t hr = Serial.parseInt();
uint8_t mn = Serial.parseInt();
uint8_t sc = Serial.parseInt();
rtc.adjust(DateTime(yr, mo, dy, hr, mn, sc));
Serial.println("RTC was Set at...");
printDateTime();
Set = true;
}
//Get an Epoch converter (Google Chrome Extension) you can input a future time
// and send the resulting stamp with a "U" prefix, for example U1741809988
// copy/paste the above to try
else if (c == 'U') {
uint32_t Unix = Serial.parseInt();
rtc.adjust(DateTime(Unix));
Serial.println("RTC was Set at...");
printDateTime();
Set = true;
}
}
}