在 C mongoose os 中的特定时间打开 2 个 LED

Turn on 2 LED's at a particular time in C mongoose os

所以我有一个 espressif 芯片连接到 2 个 LED,mongoose os 在上面运行

我想从 internet/computer 获取时间并在特定时间点亮 LED。

例如。在 10:00 处将 on/off LED 1 连接到引脚 2,在 16:00 处将 on/off LED 2 连接到 C.

中的引脚 3

第 1 步:将 wifi 设置添加到您的 mos.yml 以便它可以连接到您的无线 AP:

config_schema:
  - ["wifi.sta.enable", true]
  - ["wifi.sta.ssid", "MyAP"]
  - ["wifi.sta.pass", "Passwd"]

第 2 步:将这些添加到您的 mos.yml。如果您不打算通过 UART 进行 rpc 调用,请关闭 rpc-uart

libs:
  - 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
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/wifi
  - origin: https://github.com/mongoose-os-libs/rpc-uart

第 3 步:为 LED 开启和关闭添加 crontab 处理程序:

enum mgos_app_init_result mgos_app_init(void) {
  /* Set LED GPIOs as outputs */
  mgos_gpio_set_mode(YOUR_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;
}

第 4 步:添加回调:

void ledoff(struct mg_str action, struct mg_str payload, void *userdata) {
  mgos_gpio_write(YOUR_LED_GPIO, 0);
  (void) payload;
  (void) userdata;
  (void) action;
}

void ledon(struct mg_str action, struct mg_str payload, void *userdata) {
  mgos_gpio_write(YOUR_LED_GPIO, 1);
  (void) payload;
  (void) userdata;
  (void) action;
}

第 5 步:从 Web UI 或 UART:

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

参见 https://github.com/mongoose-os-libs/cron 作为 mgos 上 cron 表达式语法的参考。

缺少来自解决方案:

步骤 2a:在 main.c 添加:

#include "mgos_crontab.h"