// Date and time functions using a DS3231 RTC connected via I2C and Wire lib #include #include "RTClib.h" #include //http://arduiniana.org/libraries/tinygpsplus/ Used for GPS parsing HardwareSerial uart(1); TinyGPSPlus gps; RTC_DS3231 rtc; int DOW, MONTH, DATE, YEAR, HOUR, MINUTE, SECOND; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; void setup () { #ifndef ESP8266 while (!Serial); // for Leonardo/Micro/Zero #endif Serial.begin(9600); uart.begin(9600, SERIAL_8N1, 17, 16); delay(3000); // wait for console opening if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (rtc.lostPower()) { Serial.println("RTC lost power, lets set the time!"); // 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)); } } void loop () { // This sketch displays information every time a new sentence is correctly encoded. while (uart.available() > 0) if (gps.encode(uart.read())) displayInfo(); if (millis() > 5000 && gps.charsProcessed() < 10) { Serial.println(F("No GPS detected: check wiring.")); while(true); } Now_Time(); if((MINUTE % 5 == 0) && (SECOND == 0)) //Change update interval here... { RTC_UPDATE(); delay(1000 * 10); } } void displayInfo() { Serial.print(F("Location: ")); if (gps.location.isValid()) { Serial.print(gps.location.lat(), 6); Serial.print(F(",")); Serial.print(gps.location.lng(), 6); } else { Serial.print(F("INVALID")); } Serial.print(F(" Date/Time: ")); if (gps.date.isValid()) { Serial.print(gps.date.month()); Serial.print(F("/")); Serial.print(gps.date.day()); Serial.print(F("/")); Serial.print(gps.date.year()); } else { Serial.print(F("INVALID")); } Serial.print(F(" ")); if (gps.time.isValid()) { if (gps.time.hour() < 10) Serial.print(F("0")); Serial.print(gps.time.hour()); Serial.print(F(":")); if (gps.time.minute() < 10) Serial.print(F("0")); Serial.print(gps.time.minute()); Serial.print(F(":")); if (gps.time.second() < 10) Serial.print(F("0")); Serial.print(gps.time.second()); Serial.print(F(".")); if (gps.time.centisecond() < 10) Serial.print(F("0")); Serial.print(gps.time.centisecond()); } else { Serial.print(F("INVALID")); } Serial.println(); } void Now_Time() { DateTime now = rtc.now(); YEAR = now.year(); MONTH = now.month(); DATE = now.day(); DOW = now.dayOfTheWeek(); HOUR = now.hour(); MINUTE = now.minute(); SECOND = now.second(); } void RTC_READ() { DateTime now = rtc.now(); Serial.print("RTC Date/Time "); Serial.println(""); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); } void RTC_UPDATE() { RTC_READ(); Serial.println(""); Serial.print(F("GPS UTC Date/Time: ")); struct tm timeinfo; unsigned long int unixtime; timeinfo.tm_year = gps.date.year() - 1900; timeinfo.tm_mon = gps.date.month() - 1; timeinfo.tm_mday = gps.date.day(); timeinfo.tm_hour = gps.time.hour(); timeinfo.tm_min = gps.time.minute(); timeinfo.tm_sec = gps.time.second(); unixtime = mktime(&timeinfo); Serial.println(""); printf("unixtime = %u\n", unixtime); rtc.adjust(DateTime(unixtime)); Serial.println("RTC updated"); Serial.println(""); RTC_READ(); }