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,

    Yes, I was trying to turn off my modem for a certain time and then turn it back on later. Even when it is turned back on (AT+CFUN = 1), I still get the same error. 

    2020-08-20T11:13:36.441Z DEBUG modem << AT send: AT+CFUN=1
    2020-08-20T11:13:36.546Z DEBUG modem << AT ret: OK
    2020-08-20T11:13:37.283Z DEBUG modem << [00:01:05.495,147] [1;31m<err> date_time: sntp_query, error: -1[0m
    2020-08-20T11:13:37.294Z DEBUG modem << [00:01:05.503,540] [1;31m<err> date_time: getaddrinfo, error: -11[0m
    2020-08-20T11:13:37.302Z DEBUG modem << [00:01:05.513,458] [1;31m<err> date_time: getaddrinfo, error: -11[0m
    2020-08-20T11:13:37.312Z DEBUG modem << [00:01:05.522,064] [1;31m<err> date_time: getaddrinfo, error: -11[0m
    2020-08-20T11:13:37.319Z DEBUG modem << [00:01:05.530,700] [1;31m<err> date_time: getaddrinfo, error: -11[0m
    2020-08-20T11:13:37.329Z DEBUG modem << [00:01:05.538,482] [1;31m<err> date_time: Not getting time from any NTP server[0m

    Is there any procedure or a way to use the date_time library. Is there any specific example to use that library ?

  • Hi Adeel

    After turning the modem back on it will take some time for it to reconnect to the network. 

    If you wait for the CEREG notification informing you that the modem is connected is the date_time library still reporting errors?

    Best regards
    Torbjørn

  • Hi Tobjorn,

    Unfortunately yes, its still reporting the same error. The CEREG value says the modem is connected but the error still remains. Is there any way or other example where this could have been implemented for a reference ?

    Regards,

    Adeel. 

  • This is the output coming up on the screen. I even tried different NTP servers to try and get the error out.

    Regards,

    Adeel.

  • Hi Adeel

    One of my colleagues made an example for NTP that he made available online, but I am not sure the date_time library is used, so I don't know how relevant it is for you:
    https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/ntp

    Are you able to share your code with me?
    Then I will give it a go and see if I can get it to work on my end. 

    Best regards
    Torbjørn

Reply Children
  • Hi Torbjorn,

    I am attaching my main file for you to have a look. I have added the date_time_update() ; time_modem_get() and date_time_now() functions in the main loop part of the code. I am just trying to build up on the asset tracker application using the date_time library. 

    You can try it out and let me know if it works or you can modify it as well :).

    Regards,

    Adeel.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    /*
    * Copyright (c) 2018 Nordic Semiconductor ASA
    *
    * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    */
    #include <drivers/gpio.h>
    #include <nrf9160.h>
    #include <zephyr.h>
    #include <kernel_structs.h>
    #include <stdio.h>
    #include <string.h>
    #include <device.h>
    #include <date_time.h>
    #include <time.h>
    //#include <sntp.h>
    #include <kernel.h>
    #include <drivers/gps.h>
    #include <drivers/sensor.h>
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Hi Adeel 

    Sorry for the slow response. To be honest I haven't handled much nRF91 support up until now, and have spent some time just to get the tools and basic examples running. 

    I will do my best to integrate your changes and get back to you by tomorrow. 

    Best regards
    Torbjørn

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