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. 

Parents
  • Hi, Adeel!

    You can sample the current time using the Date-Time library of NCS. If you do this on startup, after connecting to LTE, you can start a timer based on the time you get or alternatively submit a delayed work item

    Best regards,
    Carl Richard

  • Hi Carl,

    I am using the date_time library to get the real time using the NTP server. 

    Can you let me know how can I use this time to synchronize my next intervals.

    For example, my sample starts at 17:02:09 and I want my next interval of sending data over MQTT to occur at 18:00:00. 

    For some reason, even if my application sample starts at 17:25:34 , still I want it to send the first data at 18:00:00 . 

    Once I send the data for the first time, then I understand that the kernel timers can schedule it as per our timer allotment.

    How can I schedule events like this because the kernel timers are defined for fixed intervals ? 

    Ex: k_timer_start(&my_connect_timer, K_SECONDS(60), K_SECONDS(3600)); // To send data every hour but the start of the sample and my intervals don't match now. My sample starts at 17:25:34 and I need to synchronize it to send the first data at 18:00:00. Could you help me explain how tis could be done ? 

    Regards,

    Adeel.

  • 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

  • Hi Carl,

    Yes I was getting the NB-IoT connection. 

    I was able to get past the error using : CONFIG_LTE_LEGACY_PCO_MODE=y

    I wanted to understand what is the NB-IoT PCO mode and how is it different from the normal NB-IoT ?

    Regards,

    Adeel.

  • Hello!

    Great! PCO stands for Protocol Configuration Options and is used for transferring various information relevant for the connection, for example DNS. The NB-IoT spec requires ePCO, but some networks still only supports regular PCO which is why this option is available. There is some more information in this thread. It should not affect your application in any way!

    Best regards,
    Carl Richard

Reply Children
Related