Microsecond RTC on device side

I am well familiar with the date() object that returns UTC server time. On agent side, I have access to the usec key that gives the microsecond count inside the second which helps me synchronize multiple agents of different instances of imp005. However, this key isn’t available on device side which causes problems because I would like a timestamp on device side when an event happens and the precision needs to be at a milliseconds precisions and synchronized as it is compared between multiple devices. Secondly, i could timestamp it from agent side, however the added random time of packet transmission between device and agent could falsify the timestamp. Is anyone familiar with a fully software solution that could help me get a synchronized usec count for devices?

I’ve worked on lots of distributed projects involving real-time. Time is hard. Trying to get lots of discrete devices to agree on what the time is, is difficult to pull off without a common time source.

This is something that I would have made great use of, on multiple Imp projects. Unfortunately, I don’t think it’s possible without a) changes to the Imp API, or b) additional hardware. The method hardware.micros() is a free running counter, but it runs asynchronously to the RTC. It’s not possible to use a combination of the two to get reliable sub-second measurement. Even if you polled time() at a ferocious rate until it ticked over and recorded the usec value of hardware.micros() to calculate an offset, the resultant values would only have relative accuracy for a short period of time.

This is because the clock value returned by date()/time() is not stable. It is periodically updated by the agent (which is updated by NTP). This process is opaque. You won’t know when it happens. If the device is always connected to the cloud, you can expect the RTC in the device to be accurate to +/-2s, no better than that. While the imp006 does have a GNSS, its RTC does not have subsecond precision, plus I haven’t seen any mention of support for a digital input for a 1 second sync signal.

An external GNSS that provides streaming NMEA messages into a UART on the imp is probably your best bet. Even then, you can’t expect millisecond accuracy all the time, as the Squirrel VM does pause from time to time