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

Get the lib time in the thingy 91

Hello,

I'm coming to you with a beginner question, I would like to use the functions localtime, difftime, strftime but I saw that time.h is not included. Is it possible to get it or to do the same thing as these functions? I tried including NEWLIB but there is no time lib.

In my project I get a timestamp and I would like to have the difference of time elapsed between this timestamp and the timestamp of now.

Thanks in advance for your help!

Parents
  • Hello,

    I'm coming to you with a beginner question

    No problem at all, we are happy to help with any question you might have! Slight smile

    I would like to use the functions localtime, difftime, strftime but I saw that time.h is not included. Is it possible to get it or to do the same thing as these functions? I tried including NEWLIB but there is no time lib.

    In my project I get a timestamp and I would like to have the difference of time elapsed between this timestamp and the timestamp of now.

    When you say that you need a timestamp, do you mean a timestamp from the network, or a RTC counting locally on your device? As in, do you need the timestamp to be the actual current time, which is used in the network?
    Or, do you just need to see how long time has elapsed since the previous timestamp, with no regards to what the time actually is in the network?
    In the first case, where you need the network time, please see the answer by my colleague in this ticket.
    In the latter case, where you just need to see how long time has passed since last, you can use the nRF9160's RTC peripheral directly. 

    Please do not hesitate to let me know if any part of my answer is unclear, or if you should have any other issues or questions!

    Best regards,
    Karl

  • Hello !

    Thank you for your answer, I understand better! I would like to recover the timestamp network which must be the gmtime, I use this command:

    char buf_time[MODEM_INFO_JSON_STRING_SIZE];
    modem_info_string_get(MODEM_INFO_DATE_TIME, buf_time, MODEM_INFO_JSON_STRING_SIZE);
    printk("%s\n",buf_time);
    double diff_t = atoi(payload_buf) - atoi(buf_time);

    However, I can't read the result. There would be something that I missed? thank you in advance for your time ;) !

  • Hello,

    ActiTAG said:
    Thank you for your answer, I understand better!

    Great, I am happy to hear that! :) 

    ActiTAG said:
    However, I can't read the result. There would be something that I missed? thank you in advance for your time ;) !

    What happens when you run the code snippet above? What is printed to your logger?
    Please check the returned value of the modem_info_string_get call, it will return the length of the received data if the call was successful - or a negative error code if the call failed.
    Please check this, and let me know what modem_info_string_get returns.

    On a general note, you should always make sure to check the returned values and error codes from your functions if there are any, as this is your only indication whether the function call succeeded or not.

    Best regards,
    Karl

  • Oh you're right, I should have thought to check the return of the function! It's return -5.  With the debugger I see that in the lib modem_info line 482  the response in recv_buf is empty.

    err = at_cmd_write(modem_data[info]->cmd,
    			  recv_buf,
    			  CONFIG_MODEM_INFO_BUFFER_SIZE,
    			  NULL);

    But when I made 

    modem_info_string_get(MODEM_INFO_RSRP, buf_rsrp, MODEM_INFO_JSON_STRING_SIZE);

    I have something in recv_buf and no error. 

    Do you have an idea please ? 

    Have a nice day Smiley

Reply
  • Oh you're right, I should have thought to check the return of the function! It's return -5.  With the debugger I see that in the lib modem_info line 482  the response in recv_buf is empty.

    err = at_cmd_write(modem_data[info]->cmd,
    			  recv_buf,
    			  CONFIG_MODEM_INFO_BUFFER_SIZE,
    			  NULL);

    But when I made 

    modem_info_string_get(MODEM_INFO_RSRP, buf_rsrp, MODEM_INFO_JSON_STRING_SIZE);

    I have something in recv_buf and no error. 

    Do you have an idea please ? 

    Have a nice day Smiley

Children
  • ActiTAG said:
    Oh you're right, I should have thought to check the return of the function! It's return -5. 

    Hehe, yes, it is always good to check the return value of the function. In this case, it returned a negative number which means an error occurred - this is very helpful for us to know!
    The specific -5 error is I/O Error, and could be generated if the IO has not been initialized. Have you made sure to run modem_info_init prior to the command?

    Could you try using the Date Time library for retrieving the timestamp instead?
    The Date Time library checks more thoroughly for available system time.
    Have you also ensured that the network actually provides the network time? I recently spoke to a colleague that told me that there exists some that do not always provide this.

    ActiTAG said:

    I have something in recv_buf and no error. 

    Do you have an idea please ? 

    It sounds strange to me that you get the error when calling date time but not when calling for recv info.

    ActiTAG said:
    Have a nice day

    Thank you for saying that, I hope you have a nice day as well! :)

    Best regards,
    Karl

  • Hello !

    The specific -5 error is I/O Error, and could be generated if the IO has not been initialized. Have you made sure to run modem_info_init prior to the command?

    Yes, I have checked that. I ran the at_client sample and it works if I set the clock with AT+CCLCK="xxxx".

    Have you also ensured that the network actually provides the network time? I recently spoke to a colleague that told me that there exists some that do not always provide this.

    I'm using a prepaid SIM card 1NCE, I'm connecting to de DeutschTelekom network but I don't find any information to know if they provide network time. 

    Could you try using the Date Time library for retrieving the timestamp instead?
    The Date Time library checks more thoroughly for available system time.

    I was guided by this post nRF9160 date-time example - Nordic Q&A - Nordic DevZone - Nordic DevZone (nordicsemi.com) 

    #prj.conf
    CONFIG_DATE_TIME=y
    CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS=1
    CONFIG_LEGACY_TIMEOUT_API=y
    
    #main.c
    
    struct tm now = {0};
    now.tm_hour = 13;   now.tm_min = 06; now.tm_sec = 00;
    now.tm_year = 121; now.tm_mon = 05; now.tm_mday = 05; 
    err = date_time_set(&now);
    
    ...
          
    int64_t date_time_stamp;
    k_sleep(K_MSEC(25000)); //Wait some time to make sure the Date-Time update has happened
    date_time_now(&date_time_stamp); 
    printk("Date time: %016llX\n", date_time_stamp);

    But now I have a problem with the lte_lc lib when I do :

    err = lte_lc_init_and_connect();

    I've never had a problem before but I noticed it when I added this line ( :

    CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS=1

    But I have no idea how to fix the problem. Maybe you have already encountered this problem?

    [00:00:01.241,760] [1;33m<wrn> at_notif: Already initialized. Nothing to do[0m
    [00:00:01.257,904] [0m<inf> lte_lc: PDP Context: AT+CGDCONT=0,"IP","iot.1nce.net"[0m
    [00:00:02.242,248] [1;33m<wrn> date_time: getaddrinfo, error: -11[0m
    [00:00:02.242,401] [1;33m<wrn> date_time: getaddrinfo, error: -11[0m
    [00:00:02.242,584] [1;33m<wrn> date_time: getaddrinfo, error: -11[0m
    [00:00:02.242,767] [1;33m<wrn> date_time: getaddrinfo, error: -11[0m
    [00:00:02.242,980] [1;33m<wrn> date_time: getaddrinfo, error: -11
    [0m[00:00:02.242,980] [1;33m<wrn> date_time: Not getting time from any NTP server

    Thank you for your help and suggestions on how to get what I am looking for. 

  • Hello,

    ActiTAG said:
    Yes, I have checked that. I ran the at_client sample and it works if I set the clock with AT+CCLCK="xxxx".

    Great, I am glad to hear that this works, at least. 

    ActiTAG said:
    I'm using a prepaid SIM card 1NCE, I'm connecting to de DeutschTelekom network but I don't find any information to know if they provide network time.

    To officially confirm whether they do provide this or not you would unfortunately have to contact and inquire them directly, but since the at_client is able to get the network time it is safe to say that they do, so this should not be the issue here.

    ActiTAG said:
    I've never had a problem before but I noticed it when I added this line
    ActiTAG said:
    But I have no idea how to fix the problem. Maybe you have already encountered this problem?

    Is your intention for this modification to have the modem query the network time every second?
    The default value for this is 3600, so setting it to 1 might be too low - i.e you might be inquiring about this too often, and thus fail.
    Could you try to set this a little higher, and see if you still see this issue?
    What accuracy do you need for your time synchronization? The onboard timers should provide enough accuracy so that you do not have to query the network so often, to know the network time.

    ActiTAG said:
    Thank you for your help and suggestions on how to get what I am looking for. 

    No problem, I am happy to help!

    Best regards,
    Karl

  • Hello,

    I hope you're fine.

    ActiTAG said:
    Yes, I have checked that. I ran the at_client sample and it works if I set the clock with AT+CCLCK="xxxx".

    Great, I am glad to hear that this works, at least. 

    Yes but my problem is that I need to set it. In my case, I would like to have the network time to know when the thingy is running and set it automatically, to after using a timer to continue to have the time up to date. 

    I'll have a look with datetime lib by increasing it, I'll keep you informed. :)

  • ActiTAG said:
    I hope you're fine.

    Thank you, I hope you are as well!

    ActiTAG said:
    Yes but my problem is that I need to set it. In my case, I would like to have the network time to know when the thingy is running and set it automatically, to after using a timer to continue to have the time up to date. 

    Yes, but if I understand you correctly, you shouldnt have to query the network for its time every second (as indicated by the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS config - but instead query it more seldom, and keep track of it locally using a timer.
    Does this make sense? Then you would just have to query for the network time every so often, to negate the timer's drift over time.

    ActiTAG said:
    I'll have a look with datetime lib by increasing it

    My suggestion was to try increasing the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS config, if you started to receive your error after setting it to 1, in order to pinpoint that this configuration was indeed the issue.

    ActiTAG said:
    I'll keep you informed. :)

    Alright, sounds good to me! :) 

    Best regards,
    Karl

Related