Time Synchronization: Can't Set RTC Time Upon Reboot [SOLVED]

Hello there,

So I’m using the Spark.syncTime(); function in my void setup() and I’m running into an issue that I can’t seem to work around.

void setup()
{
Spark.connect();
Spark.syncTime();  
Spark.process();
Spark.disconnect();
Time.zone(-5);
Serial.begin(115200);
}

When I flash the core the RTC is set correctly, however if I disconnect power or click the reset button on the Core it goes back to 1969-12-31 19:00:00 (as it should) But my core then should be running through the above lines to set it back to the current time, do I have something setup wrong?

As always any insight or direction is greatly appreciated :smile:

Cheers,
UST

EDIT: Was able to solve the issue, it was the same issue in a previous thread I did not see in the suggestions. Refer to Post 5 in this thread, if you come across this thread instead of the original. :smiley:

There was a similar thread previously. Not sure if any of the suggestions worked, but:

Thanks for the quick reply @dougal !

I see what you’re saying; it might be that the Core is zooming past the void setup() before the RTC time even begins counting time? So if we make some kind of check to ensure it is counting then sync time it ensures RTC is running properply.

Okay I will give this a shot and post back in 10min~

No luck with the fix you suggested, although I thought that would be it for sure !

Maybe it is the way I am flashing and connecting my core? I do it in the following steps:

  1. Having the Core plugging into a USB port on my laptop I use PuTTY to set the internet credentials for my Core, and connect successfully to the SparkCloud and WiFi.

  2. I flash my core with my own firmware and it happens successfully. I can see that the RTC on the Core is set correctly because I am sending data via WiFi to a local server where it posts the timestamp.

  3. I unplug my Core from the laptop and after 30 seconds plug it into an external power source. I realize now the RTC on the Core should be reset, effectively it should go back to 1969-12-31 19:00:00

  4. When the Core boots up it connects to WiFi successfully and runs my code correctly too.

Aha, I should have looked more closely first. If you check the first post in that thread, the original author found the solution. It was similar to my suggestion, but checking the year, instead of the seconds, and also throwing a small delay in:

1 Like

I see, so something like:

void setup()
{
    Spark.connect();
    while(Time.year() == 1969) // 1969 in my case 
        {
            Spark.syncTime();  
            Spark.process();
            delay(100);
        }
    Spark.disconnect();
    Time.zone(-5);
    Serial.begin(115200);
}

That way it will just loop through the While loop until Spark.syncTime() actually syncs the time? I will give this a shot and post in 10min~ Thanks again for the quick reply and reference!

No luck! This is what I used in my void setup():

void setup()
{
Spark.connect();
while(Spark.connected() == false) {
 delay(100);
}
while(Time.year()==1969)
{
Spark.syncTime();  
Spark.process();
delay(100);
}
Spark.disconnect();
while(Spark.connected() == true) {
delay(100);
}
Time.zone(-5);
Serial.begin(115200);
}

I figured I would give everything time to connect to ensure correct synchronization. But it still goes to 1969-12-31 19:15:00

Should I flash the core before I unplug it? Because when it flashes, time is set correctly.

EDIT: Going to do some more digging and if I find a solution I’ll post it here!

2 Likes

So changing

while(Time.year() == 1969) // 1969 in my case

to

while(Time.year() == 1970) // doh !

fixed the issue!! I guess the time was still starting at 1970 , but maybe because of Time.zone(-5); it was changing the way it showed up on my database!

Thank you again for the post !

2 Likes

When it first starts up, before the time has synced or you have set the timezone, it’s going to default to the ‘epoch’ start time of Jan 1, 1970 00:00:00 GMT. If you wanted to be extra safe, you could always check for Time.year() <= 1970, which would be valid for all timezones at the epoch start.

Glad you got it working!

2 Likes

That’s actually genius, almost like an all encompassing check statement!