Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Cron.Add in C code in main.c

I am very new to all this. I am using an espressif chip with mongoose os and I am trying to turn on and off the led at a particular time /day using the following commands from the Web UI terminal and it works :
call Cron.Add '{"at":"00 35 14 * * *", "action":"ledon"}'

call Cron.Add '{"at":"00 16 13 * * *", "action" : "ledoff"}'

If I want to insert these commands in the main.c in C language so it runs as one code , how do I go about that ?

Thanks in advance.

Comments

  • @nliviu Can u elaboraten on that please? , or an example perhaps with the call command and the counterpart ?

  • mamuespmamuesp Germany/Northern coast
    edited July 5

    RTFM (Read The Fine Manual ...) ;-) Look here and/or here and you may figure it out by yourself. If not, then you may have to find another approach. And also you should use the searxh function here in the forum - your question has been answered several times.

  • nliviunliviu Romania
    edited July 5

    Something like this:

    #include "mgos.h"
    #include "mgos_cron.h"
    
    void ledon(void *user_data, mgos_cron_id_t id) {
      LOG(LL_INFO, ("ledoff"));
      (void) user_data;
      (void) id;
    }
    
    void ledoff(void *user_data, mgos_cron_id_t id) {
      LOG(LL_INFO, ("ledoff"));
      (void) user_data;
      (void) id;
    }
    
    enum mgos_app_init_result mgos_app_init(void) {
      //Cron.Add '{"at":"00 35 14 * * *", "action":"ledon"}'
      mgos_cron_add("00 35 14 * * *", ledon, NULL);
      //Cron.Add '{"at":"00 16 13 * * *", "action" : "ledoff"}'
      mgos_cron_add("00 16 13 * * *", ledoff, NULL);
      return MGOS_APP_INIT_SUCCESS;
    }
    
  • edited July 6

    So this was my original code , and using terminal I was calling the cron jobs--
    I edited it the way you described but it still doesnt work-- sorry more help needed

    #include <mgos>
    #include "common/mg_str.h"
    #include "mgos_gpio.h"
    #include "mgos_crontab.h"
    
    #define PREC_LED_GPIO 2
    
    void ledoff(struct mg_str action, struct mg_str payload, void *userdata) {
      mgos_gpio_write(PREC_LED_GPIO, 0);
      (void) payload;
      (void) userdata;
      (void) action;
    }
    
    void ledon(struct mg_str action, struct mg_str payload, void *userdata) {
      mgos_gpio_write(PREC_LED_GPIO, 1);
      (void) payload;
      (void) userdata;
      (void) action;
    }
    
    enum mgos_app_init_result mgos_app_init(void) {
      /* Set LED_GPIOs as output */
      mgos_gpio_set_mode(PREC_LED_GPIO, MGOS_GPIO_MODE_OUTPUT);
    
      /* Register crontab handler - LED OFF */
      mgos_crontab_register_handler(mg_mk_str("ledoff"), ledoff, NULL);
    
      /* Register crontab handler - LED ON */
      mgos_crontab_register_handler(mg_mk_str("ledon"), ledon, NULL);
    
      return MGOS_APP_INIT_SUCCESS;
    }
    
  • nliviunliviu Romania

    Your code looks ok for crontab.
    What do you mean by "it doesn't work"?
    Does not compile? Because #include <mgos> should be #include <mgos.h>.
    Do you have

      - origin: https://github.com/mongoose-os-libs/crontab
    

    in the libs section of mos.yml?

    My code is for cron.

    cron - allows you to define cron jobs in your code and if you want to modify them you need to modify the code, rebuild and reflash/OTA update the device.
    crontab - allows you to associate the actions related to cron jobs defined with Cron.Add and modify/remove them with Cron.Edit/Cron.Remove.

  • edited July 6

    It compiles fine, and I have all the libs as well.. The command which I do from terminal works, but it does not work from this code which is my main aim..

    the command is : call Cron.Add '{"at":"00 51 08 * * *", "action" : "ledoff"}'

    And the edited code for it :

     #include <mgos.h>
     #include "common/mg_str.h"
     #include "mgos_gpio.h"
     #include "mgos_crontab.h"
    
     #define PREC_LED_GPIO 2
    
    void ledoff(struct mg_str action, struct mg_str payload, void *userdata) {
      mgos_gpio_write(PREC_LED_GPIO, 0);
      (void) payload;
      (void) userdata;
      (void) action;
    }
    
    void ledon(struct mg_str action, struct mg_str payload, void *userdata) {
      mgos_gpio_write(PREC_LED_GPIO, 1);
      (void) payload;
      (void) userdata;
      (void) action;
    }
    
    enum mgos_app_init_result mgos_app_init(void) {
      /* Set LED_GPIOs as output */
      mgos_gpio_set_mode(PREC_LED_GPIO, MGOS_GPIO_MODE_OUTPUT);
    
      /* Register crontab handler - LED OFF */
      mgos_crontab_register_handler(mg_mk_str("ledoff"), ledoff, NULL);
    
      /* Register crontab handler - LED ON */
      mgos_crontab_register_handler(mg_mk_str("ledon"), ledon, NULL);
        //Cron.Add '{"at":"00 49 08 * * *", "action":"ledon"}'
      mgos_cron_add("00 49 08 * * *", ledon, NULL);
      //Cron.Add '{"at":"00 51 08 * * *", "action" : "ledoff"}'
      mgos_cron_add("00 51 08 * * *", ledoff, NULL);
    
    
      return MGOS_APP_INIT_SUCCESS;
    }
    
  • nliviunliviu Romania

    Don't mix cron and crontab.
    Did you add sntp?
    If you have it and the time you want to fire the cron job are local times, did you set sys.tz_spec in config_schema?

  • Yes, I have all the things that's why the command works from terminal.

    But when I incorporate it in main.c it doesn't-- maybe something is wrong in the way I add in main.c file ?

  • nliviunliviu Romania

    Your code works very well:

    mos --port ws://IP/rpc call Cron.Add '{"at":"0 18 12 * * *", "action":"ledon"}'
    {
      "id": 1
    }
    mos --port ws://IP/rpc call Cron.Add '{"at":"0 19 12 * * *", "action":"ledoff"}'
    {
      "id": 2
    }
    

    Results

    [Jul  6 12:17:59.792] cron_cb              Cron job 1 is firing: "ledon"
    [Jul  6 12:18:59.779] s_print_time         Next invocation: 1530955140 [2018/07/07 09:19:00 UTC]
    [Jul  6 12:18:59.792] cron_cb              Cron job 2 is firing: "ledoff"
    

    Remove the mgos_cron_add lines.

  • I think we have a mis-communication.
    I know the code works well, If i use call Cron:Add via the web UI or terminal..
    My main problem is to call it via the main.c .. do you understand me now ?

Sign In or Register to comment.