Set time via publish message

Despite revisiting this issue time and time again and receiving some fantastic help from members I’ve never managed to get my Particle Core to maintain accurate time. It’s off by a minutes and obviously the drift only gets worse rather than better!

Just wondering - could anyone point me at a code snippet where I can tell my Core with a Particle Publish:
“right, it’s 22:00 NOW” !!
the Core then sets the time to 22:00.
I’m aware that there will be a couple of seconds inaccuracy with this method (at best) but it’s a lot better than the couple of minutes issue I have at the moment!

thank you!!

There are a couple of libraries available to sync time via NTP. If you need accurate time that’s the best route rather than using a cloud function or other means. You’ll get way more accurate time.

1 Like

Using Time.setTime() could work.

// Expect data to be unix timestamp
void timeHandler(const char *event, const char *data)
{
int timeFromData = atoi(data);
Time.setTime(timeFromData);
}

// In setup()
Particle.subscribe("setTime", timeHandler, MY_DEVICES);

Alternatively, you could make use of Particle.syncTime() and call it routinely. This would be a more straightforward remedy and you wouldn’t need to manually sync the time with a publish.

1 Like

Thanks, I’ve tried this, but it doesn’t seem to work. I’ve had help from all sorts of forum superstars over the years but it’s never worked. It might work on a Photon but I’ve not got it to work on a Core. It works once, but then never again.

Thanks for the code snippet, that sounds like it might do the job! I just send a unix timestamp with topic “setTime” ?

1 Like

Yup, that should do it.