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

Parents
  • 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

Reply
  • 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

Children
  • Kalyan,

    It's been a while since I worked with this, and it was with the nRF5 SDK environment.  I basically followed the example from NordicPlayground/nrf5-calendar-example.

    https://github.com/NordicPlayground/nrf5-calendar-example

    I added the nrf_calendar.c and associated nrf_calendar.h to a SDK ble_peripheral example based project.  The NordicPlayground has a good example of how to set up the RTC and set and get time.  If you have a tm structure with the epoch time, it should be easy enough to set the RTC following the nrf_calendar example.  I didn't do much else than use the functions detailed in the NordicPlayground code.

    I was then able to use the RTC maintained time for timestamps data-logging sensor data.

    Max

  • Hi Max,

    Thanks for the response.

    I have tried Zephyr kernel timers (based on the 32k RTC) and it is working as expected and I am receiving timer event handler and able to set the RTC timer or counter.

    Regards,

    Kalyan

  • Wow!  This was a LONG time ago and done for some contract work under NDA.  So I can't give a full working example of the nRF52 code.  This was for a custom board with a nRF52832 module with a lot of code you wouldn't want to wallow through.  I can't say I even recall all the details to provide helpful hints.

    The code examples at the Nordic Playground aren't enough?  All I did was integrate that code into my project (I used SES) as described in this post.  The project involved setting the date and time in the nRF52832 RTC, then reading it and providing a timestamp on sensor data written into an external EEPROM.
    According to my notes, what I did was based on the SDK Blinky example into which I merged NRF_CLI and NRF_CLI_BLE_UART as well as the Nordic Playground calendar routines.  This gave a path to connect to an external internet connected device and send a command to set date and time.  Initial debugging of the CLI was done with a hardwired serial to USB cable to send the date and time from a PC.  TeraTerm on the PC could then connect to the serial port and set date and time as well as read it.  TeraTerm had a macro language which allowed you to read time from the system which then could be transmitted to the nRF52832 with a CLI command to set the time and date.  I vaguely recall using an app on a tablet called Serial BLE Terminal to test the CLI command over BLE.  I think I just entered the command manually with some date and time data.  Eventually an app would be written to use this interface with the internet time.  This was somebody else's responsibility.
     
    I'm not sure this is the sort of interface you're looking for.  I could probably dredge up copies of the CLI commands to set and read time.  I'm not sure this will tell you much more than outlined above. 
         
    Regards,
    Max
Related