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

How to schedule events and tasks at particular time intervals

Hi Dev Team,

I am running a program that takes in sensor data at 10 min intervals and publishes it to my server through MQTT at 1hr intervals.

I have different threads running for different events (Taking data from sensor and sending data outside). I am using the kernel timers to execute these events repeatedly.

Now, I want to schedule these events in such a way that I start my program at any time and then it reads the sensor value at 00:00 (12 am), 00:10 (12:10)... with 10 mins interval and then publishes my sensor value to server through MQTT every 1 hour (01:00, 02:00....) at exact times. 

Currently, I just start the program and then the process initiates at that time and continues. But I need the data to be read at specific times i.e: start of the hour time (like exactly at 12:00,12:10, 12:20, 12:30, 12:40, 12:50, 01:00) and then publish the data exactly at 01:00,02:00,03:00... and then continue this process.

Could you let me know how to schedule events such that they happen sequentially at the time I need.

Regards,

Adeel. 

  • Hi Carl,

    I had a question regarding this ticket. The above code that you sent worked perfectly for NCS v1.3.0 .

    I did this in NCS v1.3.0 and this works perfectly.

    	modem_configure();  //to get LTE connection
    
        date_time_update(); //To get current time
    
        printk("Busy-wait %u s\n", BUSY_WAIT_S);
        k_busy_wait(BUSY_WAIT_S * USEC_PER_SEC);  //2 seconds delay
    
        int64_t curr_time;     //above code
        date_time_now(&curr_time);
    
        int64_t curr_time_s = curr_time / 1000;            //Convert to seconds
        struct tm *readable_time = gmtime(&(curr_time_s)); //Get human readable time with time.h
        printk("Curr min: %d\n", readable_time->tm_min);
        printk("Curr sec: %d\n", readable_time->tm_sec);
    
        int32_t delay = 3600 - (readable_time->tm_min) * 60 - readable_time->tm_sec;
        int32_t real_time = delay + 600;
        printk("Delay is: %d\n", delay);
        printk("Real Delay is: %d\n", real_time);

    The above output is correct.

    But when I try to do the same in NCS v1.4.2 or NCS v1.5.0, it doesn't work the way I want it to.

    	modem_configure(); //for LTE connection
    
        date_time_update_async(NULL);  //to get current time
    
        printk("Busy-wait %u s\n", BUSY_WAIT_S);
        k_busy_wait(BUSY_WAIT_S * USEC_PER_SEC);  //2 seconds delay
    
        int64_t curr_time;   //above code
        date_time_now(&curr_time);
    
        int64_t curr_time_s = curr_time / 1000;            //Convert to seconds
        struct tm *readable_time = gmtime(&(curr_time_s)); //Get human readable time with time.h
        printk("Curr min: %d\n", readable_time->tm_min);
        printk("Curr sec: %d\n", readable_time->tm_sec);
    
        int32_t delay = 3600 - (readable_time->tm_min) * 60 - readable_time->tm_sec;
        int32_t real_time = delay + 300;
        printk("Delay is: %d\n", delay);
        printk("Real Delay is: %d\n", real_time);
    

    The only change is using : date_time_update_async(NULL) as date_time_update() is not supported in higher NCS versions.

    I get the following in NCS v1.5.0 : 

    This output is incorrect (the Curr min and Curr sec are wrong).

    Could you try to reproduce this in NCS v1.5.0 and verify ?

    Regards,

    Adeel.

  • Hi, Adeel!

    You're getting an error when calling date_time_now that indicates that a valid time aren't available yet. Instead of busy_wait I suggest that you use a blocking semaphore that is released by a handler for date_time_update_async callbacks. This way you can ensure that a valid time is available when calling date_time_now.

    You can how I have done this here.

    Best regards,
    Carl Richard

  • Hi Carl,

    Thanks for this lead. Got it working by using the blocking semaphore Slight smile.

    Regards,

    Adeel.

  • Hi Carl,

    I just had another question to ask regarding the above thread. 

    I am having a pretty poor reception using : CONFIG_LTE_NETWORK_MODE_LTE_M 1

    So, I tried to change to NB-IoT using : CONFIG_LTE_NETWORK_MODE_NBIOT to improve my RSRP.

    I did it through the autoconf.h file.

    The NB-IoT is configured properly as well and I can see it when I do : 

    AT%XSYSTEMMODE?
    
    
    
    
    %XSYSTEMMODE: 0,1,0,0
    
    
    
    
    OK
    
    
    
    

    This helps me understand that I have configured for NB-IoT. 

    I try to run my application with NB-IoT config but I see that the date_time handler does not give me the right time.

    It works perfectly with the : CONFIG_LTE_NETWORK_MODE_LTE_M

    Do I need to do anything else in order to set up my device with NB-IoT config ?

    P:S : I am in Germany and NB-Iot works well here.

    Regards,

    Adeel.

  • Hello again, Adeel!

    Are you certain that the device manages to get a connection? When changing between NB-IoT and LTE-M you need to make sure that you SIM supports both, or change to another SIM.

    Secondly, when changing the project configuration I suggest that you follow the steps described in the "Configuring your application"-documentation. Namely, changing the prj.conf or using the configuration tool in SES. By changing autoconf.h directly there may be some dependencies of the configuration that are lost.

    Best regards,
    Carl Richard

Related