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!

  • 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

  • Humm I set the value to 60 and 1800 but the date time get is 00000179fae6436e / 00000179fb00561a (Mon Jul 27 1970 19:05:45 GMT+0000) ...

Related