0

So I have an espressif chip connected to 2 LEDs, and mongoose os runs on it

I would like to get time from the internet/computer and make the led's turn on at a particular time.

eg. at 10:00 turn on/off led 1 connected to pin 2 and at 16:00 turn on/off led 2 connected to pin 3 in C.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
futball11
  • 11
  • 8
  • I assume you mean [this Mongoose-OS](https://mongoose-os.com/), and not the [MongoDB object modeling tool](http://mongoosejs.com/docs/guide.html) that the `mongoose` tag is about? Created new tag and edited your question with the new tag. – Some programmer dude Jun 28 '18 at 11:17
  • @Someprogrammerdude yes – futball11 Jun 28 '18 at 11:21

2 Answers2

1

Step 1: Add wifi setup to your mos.yml so it can connect to your wireless AP:

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

Step 2: Add these to your mos.yml. Leave off rpc-uart if you have no intention of making rpc calls over 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

Step 3: Add crontab handlers for LED on and LED off:

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;
}

Step 4: Add callbacks:

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;
}

Step 5: From Web UI or UART:

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

See https://github.com/mongoose-os-libs/cron as a reference for the syntax of cron expressions on mgos.

Andy J
  • 1,479
  • 6
  • 23
  • 40
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/174327/discussion-on-answer-by-andy-j-turn-on-2-leds-at-a-particular-time-in-c-mongoos). – Bhargav Rao Jul 04 '18 at 07:59
  • How to incorporate the step 5 from within the main.c ? – futball11 Jul 06 '18 at 08:54
  • Calling RPC calls from within your code is non-trivial. First you need to look up which callback function `add_handler()` uses for `Cron.Add`, which in this case ultimately ends up being `add_or_edit_job()`. Then you need to figure out how to replace the calling args, or re-write the guts of that function yourself. Sometimes it can be a bit of work. – Andy J Jul 06 '18 at 12:23
  • @AndyJ : ideas on hwo to implement this with the help of buttons and 2 leds ? -- Press flash button 1 led 1 goes on at a time, press flash button 2, led 2 goes on, press flash button 3- just displays the current time. Many thanks! – futball11 Jul 26 '18 at 10:33
0

From the solution is missing:

Step 2a: In main.c add:

#include "mgos_crontab.h"
roroid
  • 35
  • 6