First of all, my apologies for posting this despite various existing attempts.
I realized that most of the methods already conceived, prioritize precision control of time synchronization, and that certainly has it's appeal. But the way they work are above my understanding, and I admit that outright. I was looking at the sample code of RTCLib and stumbeled upon this line:
rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
What if, we pass data to the DateTime function in the format it expects. Would it synchronize the time? Accuracy isn't important to me. The GPS can simply be used to set the DS3231 in case the RTC is reset or the battery runs flat. So, with whatever I know, I attempted to do that (and I can see the seasoned veterans cringing at the very act). It didn't work. Here's the code:
#include <TimeLib.h>
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <TinyGPS++.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
bool syncOnce = true;
SoftwareSerial SerialGPS = SoftwareSerial(4, 3);
TinyGPSPlus tinyGPS;
const int offset = 19800; // hours (in seconds)
time_t prevDisplay = 0;
void setup() {
Serial.begin(9600);
while (!Serial)
;
SerialGPS.begin(9600);
rtc.begin();
}
void loop() {
while (SerialGPS.available())
tinyGPS.encode(SerialGPS.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
int Year = tinyGPS.date.year();
int Month = tinyGPS.date.month();
int Day = tinyGPS.date.day();
int Hour = tinyGPS.time.hour();
int Minute = tinyGPS.time.minute();
int Second = tinyGPS.time.second();
setTime(Hour, Minute, Second, Day, Month, Year);
adjustTime(offset);
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if the time has changed
prevDisplay = now();
//GPSprintformat();
sync();
rtcPrint();
}
}
}
void GPSprintformat() {
char buf[40];
sprintf(buf, "%4d,%2d,%2d %2d,%2d,%2d", year(), month(), day(), hour(), minute(), second());
Serial.println(buf);
}
void rtcPrint() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
void sync() {
if (syncOnce) {
String TIMES = String(year()) + "," + String(month()) + "," + String(day()) + "," + String(hour()) + "," + String(minute()) + "," + String(second());
const char *STRINGS = TIMES.c_str();
rtc.adjust(DateTime(*STRINGS));
syncOnce = false;
}
}
The output is a random time value like "2106/2/6 6:29:5".
Please let me know your thoughts.