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

Using RTC to control the real timestamp

Hi Dev Team,

I had a task to work on that required a timsetamp to schedule a certain event, like for example I wanted my modem to start off every morning at 10:00:00 and switch off at 10:05:00.

What could be the best way to achieve this kind of functionality ?

Regards,

Adeel.

Parents
  • Hi Adeel

    You might want to have a look at the calendar example on our Github account:
    https://github.com/NordicPlayground/nrf5-calendar-example

    It basically uses the standard time.h library in C to decode rolling second counter into seconds, minutes, hours, days etc, and uses the RTC timer in the nRF52 to update the time every second. 

    Please be aware that if you need to keep track of time over long periods you should use an external 32k crystal for the best accuracy.

    Best regards
    Torbjørn

  • Hi Torbjorn,

    Is  there any possibility of an example for nRF9160DK. I am using NCS v1.3.0 nRF Connect for desktop and running the asset tracker application. 

    I tried using the date_time.h library to try and get the time either using NTP or through MODEM. I can use the AT+CCLK command to fix the time manually and then read it but I would like the network to get the current time for me.   

    Regards,

    Adeel.

  • Hi Tobjorn,

    I appreciate your support. You can get back to me tomorrow, no issues :).

  • Hi Adeel

    I messed around with it today, and finally got it to work with some help from the developer. 

    You can find my example here:
    https://github.com/too1/mqtt_w_date_time

    It is based on the mqtt_simple example, but I moved all the mqtt functions into a separate file for clarity, and added the date_time calls into main. 

    Some things worth noting: 

    1) To make date_time.c build I had to make a small change to the code. I explain this in the readme of the repository. 

    According to the developer this issue is fixed in the master branch, but was not properly tested in the v1.3.0 tag.

    2) In order to get the time information properly he suggested I set CONFIG_DATE_TIME_MODEM=n in the project configuration (prj.conf). This prevents the library from trying to get the time from the modem, and instead accesses the NTP server. 

    3) He also suggested adding some delay between the first call to date_time_update() and date_time_now(), as you can see in my example. 

    With these things in place I am able to see the current time printed after the program has run for a bit: 

    Best regards
    Torbjørn

  • Hi Tobjorn,

    Thanks, I was able to run the example file you sent and it works. Thanks for the effort.

    I just wanted to know one last thing. How would you suggest to go about and convert the time I am getting into a date_time format as we use normally. Any such available library or struct I could use.

    Really appreciate the help on this Slight smile.

  • Hi Adeel

    You could do something like this, using the time.h library similar to how it's done in the calendar example:

    // Convert to string using time.h
    time_t time = unix_time_ms / 1000;
    struct tm local_time;
    local_time = *localtime(&time);
    char time_string[80];
    strftime(time_string, 80, "%x - %H:%M:%S", &local_time);
    printk("Local time from time.h: %s\n", time_string);

    The main difference is that you need to divide the timestamp by 1000, since the time.h library expects a second based time value. 

    In order to use time.h you also need to enable one of the standard C libraries, which can be done by adding the following line to prj.conf:

    CONFIG_NEWLIB_LIBC=y

    Best regards
    Torbjørn

  • Hi Tobjorn,

    Thanks for the help on this Slight smile. Solved my issue of getting the time through NTP:

    Regards,

    Adeel.

Reply Children
Related