This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF Connect SDK: How to get the system timestamp(epoch time) on nRf52840dk

Hi nRF support team,

In my application, I need to show system date and time, so I am trying to get the system epoch time by using the nRF Connect SDK on nRF52840 dev kit.

Can you please let me know, what needs to configure to get the system epoch time OR sample code to get system epoch time?

Thanks in advance.
Regards,
Kalyan

  • Kalyan,

    You might want to look at https://github.com/NordicPlayground/nrf5-calendar-example

    To do this "right" you need to get the time from an internet connected device that accesses the time server(s) and sets the RTC.  I've downloaded the time from a USB connected terminal (TeraTerm) which has the capability of sending the system time to the UART connection via running a simple macro.  Not sure something like this works for you, but once you set the time the RTC will maintain it fairly effectively (as long as power is on).  I've also downloaded system time from a BLE connected Android tablet, but that used a custom BLE service to send time from the tablet.

    Note there's a transmission delay setting the time this way, so it will be slightly off but not by much.

    Max (online user, not a Nordic support person)

  • I agree with on this one. You can use something like the RTS (Real time service) to fetch the current time, and then use the RTC (real time counter), which is not the same as a Real Time Clock. Whenever the nRF is not running, it does not keep track of time. That would require that the RTC is running, which it is not when it is powered off. Basically, whenever the application is starting from scratch, the nRF has no way of knowing what time it is before checking with a device that either is connected to the internet, or that has an RTC

    Best regards,

    Edvin

  • Hi Edvin, Maxx44,

    Thanks for the clarification.

    Can you please share the working sample code to get system epoch time from Zephyr or nRF connect sdk.

    Regards,

    Kalyan

  • Kalyan,  
     
    It seems you didn't understand our responses.  The epoch time does NOT come from Zephyr or the nRF modules, nor is it available there without setting it from an external connection.  You need to obtain epoch time from an external device connected to and synced to the internet time servers. Methods to do that are via a custom download or using BLE Current Time Service.  The nRF52 SDK has an example of CTS  at https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/ble_sdk_app_cts_c.html?cp=7_1_4_2_2_7

    Once you obtain the epoch time, you can set an RTC to maintain epoch time and use it in your application with the standard C/C++ time.h library.

    Hope this helps.  Sorry, no plug and play solution.

    Regards,  Max

  • Hi Max,

    Thanks for the quick response.
    As you suggested, I can get the epoch time from the BLE GATT attribute externally set by mobile app.

    As you know nrf52840 did not have RTC hardware, once I obtain the epoch time from the mobile app I have to set this value in RTC timer.  RTC timer or API's I did not found in the Zephyr or nRF connect SDK. Can you please share is there any sample code or reference links which can direct me to set the epoch time in RTC timer or counter?

    FYI, only gmtime() functions are avilable in zephyr timer.h.
    /*
     * Conversion between civil time and UNIX time.  The companion
     * localtime() and inverse mktime() are not provided here since they
     * require access to time zone information.
     */
    struct tm *gmtime(const time_t *timep);
    struct tm *gmtime_r(const time_t *_MLIBC_RESTRICT timep,
                struct tm *_MLIBC_RESTRICT result);

    Thanks in advance.
    Regards,
    Kalyan

Related