Schedule tasks based on current time

Hello,
Here are the details of my questions

  1. My goal is: To be able to schedule tasks based on time/day/mont/year.
    For example I want to setup a relay to start at 10:00 AM every Sunday and stop at 11:00 AM every Sunday
  2. My actions are: Currently I’m looking for a library to do that. I don’t have yet any code develop.
  3. The result I see is: n/a
  4. My expectation & question is: Doese such a libray exist for mongoose os? If not what will be the best practice to achieve such a task. In the past I was using TimeAlarms for Arduino on Arduino. Doese ther exist such a library also for Mongoose OS?

cron

@nliviu Thanks. I will give it a try.

@nliviu can you please support me here a little as I’m getting an error with my code.

static void cron_socket1_off(struct mg_str action, struct mg_str payload, void *userdata)
{
  int pin_S1 = mgos_sys_config_get_iotsocket_pin1();
  mgos_gpio_write(pin_S1, 0);                    /* Turn on/off the light */
  mgos_shadow_updatef(0, "{socket1_on: %B}", 0); /* Report status */
  LOG(LL_INFO, ("Crontab foo job fired! Payload: %.*s", payload.len,
                payload.p));
  (void)userdata;
  (void)action;
}

static void cron_socket1_on(struct mg_str action, struct mg_str payload, void *userdata)
{
  int pin_S1 = mgos_sys_config_get_iotsocket_pin1();
  mgos_gpio_write(pin_S1, 1);                    /* Turn on/off the light */
  mgos_shadow_updatef(0, "{socket1_on: %B}", 1); /* Report status */
  LOG(LL_INFO, ("Crontab foo job fired! Payload: %.*s", payload.len,
                payload.p));
  (void)userdata;
  (void)action;
}

enum mgos_app_init_result mgos_app_init(void)
{
  mgos_event_add_handler(MGOS_SHADOW_UPDATE_DELTA, delta_cb, NULL);
  mgos_event_add_handler(MGOS_SHADOW_CONNECTED, connected_cb, NULL);
  /* Register crontab handler - socke1-off/on */
  mgos_crontab_register_handler("cron_socket1_on", cron_socket1_on, NULL);
  mgos_crontab_register_handler("cron_socket1_off", cron_socket1_off, NULL);
  return MGOS_APP_INIT_SUCCESS;
}

But when I compile I’m getting this error:
/data/fwbuild-volumes/2.17.0/apps/IoT_Socket_box/esp8266/build_contexts/build_ctx_350281813/src/main.c: In function 'mgos_app_init':

/data/fwbuild-volumes/2.17.0/apps/IoT_Socket_box/esp8266/build_contexts/build_ctx_350281813/src/main.c:187:3: error: implicit declaration of function 'mgos_crontab_register_handler' [-Werror=implicit-function-declaration]

mgos_crontab_register_handler("cron_socket1_on", cron_socket1_on, NULL);
   ^

And this is what I have in libs:

libs:
  - origin: https://github.com/mongoose-os-libs/ca-bundle
  - origin: https://github.com/mongoose-os-libs/core
  - origin: https://github.com/mongoose-os-libs/dash
  - origin: https://github.com/mongoose-os-libs/dns-sd
  - origin: https://github.com/mongoose-os-libs/http-server
  - origin: https://github.com/mongoose-os-libs/provision
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-service-ota
  - origin: https://github.com/mongoose-os-libs/rpc-service-wifi
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/ota-http-server
  - origin: https://github.com/mongoose-os-libs/ota-shadow
  - origin: https://github.com/mongoose-os-libs/wifi
  - origin: https://github.com/mongoose-os-libs/sntp
  - origin: https://github.com/mongoose-os-libs/crontab
  - origin: https://github.com/mongoose-os-libs/rpc-service-cron

Do you really need crontab? It is intended to be used with rpc-service-cron.

#include "mgos.h"
#include "mgos_cron.h"

static void cron_1min_cb(void *user_data, mgos_cron_id_t id) {
  LOG(LL_INFO, ("Uptime: %.2lf, free_heap_size: %lu, min_free_heap_size: %lu",
                mgos_uptime(), (unsigned long) mgos_get_free_heap_size(),
                (unsigned long) mgos_get_min_free_heap_size()));
  (void) user_data;
  (void) id;
}

enum mgos_app_init_result mgos_app_init(void) {
  mgos_cron_add("0 */1 * * * *", cron_1min_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}

Remove the lines

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

and add

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

I’m planning to add the schedule dynamically from the app. I guess with rpc-service is much easier.

If you need to change the schedule, rpc-service-cron is the way to go. Be aware that the scheduled tasks will be lost after flashing the device.

Regarding the above error, #include "mgos_crontab.h" is missing.

LE. I’ve noticed an error in your code

 mgos_crontab_register_handler("cron_socket1_on", cron_socket1_on, NULL);

should be

 mgos_crontab_register_handler(mg_mk_str("cron_socket1_on"), cron_socket1_on, NULL);

@nliviu i have found that error. thank you again!:smile: