hardwario / lora-modem

Open LoRaWAN modem for Murata Type ABZ
BSD 3-Clause "New" or "Revised" License
14 stars 3 forks source link

Implement time synchronization #73

Closed janakj closed 1 year ago

janakj commented 2 years ago

The LoRaWAN protocol provides a couple of mechanisms that could be used to synchronize the clock on the device. Design an ATCI protocol through which LoRaWAN-based time synchronization could be extended all the way to the main MCU on the Core module. With that, we could synchronize the wall clock time in the RTC via LoRa without the need to have a GPS receiver on the device.

This could be useful in devices such as the LoRa tester where the time could be displayed on the LCD.

vicatcu commented 1 year ago

I've been wanting this too, for setting the time on an hardware RTC from "network time"

janakj commented 1 year ago

What accuracy are you looking to obtain? Second-level accuracy is relatively simple. Anything better may require support from the LoRaWAN network.

-Jan

vicatcu commented 1 year ago

Second-level accuracy is more than adequate adequate for my purposes

janakj commented 1 year ago

Hi @vicatcu,

I just committed the first version of time synchronization support based on the DeviceTimeReq LoRaWAN MAC command. See this wiki page for more information. This method should be accurate to +- 100 milliseconds.

There is a new AT command called AT$DEVTIME which triggers time synchronization in the modem. You will eventually get the current time via an +ANS notification:

AT$DEVTIME
+OK

+EVENT=2,1

+ANS=13,1369371195,200

Number 13 in the +ANS notification identifies a time synchronization response. The second parameter is GPS time, i.e., the number of seconds since the GPS epoch (January 6, 1980 UTC). The third parameter represents a fraction of a second (in milliseconds).

Converting GPS time to UTC, e.g., if you want to display current date and time to the user, generally requires a leap second table. At the moment, you can convert the value to GPS by adding 315964800 (difference between GPS and UTC epochs) and subtracting 18 (18 leap seconds have been inserted since GPS epoch).

This method requires LoRaWAN 1.0.3 or higher. I have tested it with the The Things Network where it seems to work well.

This feature will eventually be released as part of version 1.5.0. If you know how to build your own firmware binary the source code, feel free to give it a try now.

-Jan

janakj commented 1 year ago

This feature is now implemented in the firmware, in the companion Python library, and documented in the wiki.