diff --git a/src/channel.c b/src/channel.c index 903c101..cfb2496 100644 --- a/src/channel.c +++ b/src/channel.c @@ -161,6 +161,13 @@ void channel_set(int idx, bool state) { } mqtt_publish_stat("channel", "{idx: %d, relay_state: %d}", idx, channel_get(idx)); + + if (state) { + // Sleep 250ms after toggling a relay to avoid cheaper boards from + // suffering a brownout and rebooting the MCU in case several channels + // are switched in short succession. + mgos_msleep(250); + } } bool channel_get(int idx) { @@ -186,6 +193,18 @@ void channel_handler(int gpio, void *arg) { LOG(LL_INFO, ("RPC triggered channel %d", idx)); } else { LOG(LL_INFO, ("Button on GPIO %d triggered channel %d", gpio, idx)); + // Scan the button for 100ms to ensure it is still pressed. This is to avoid + // RFI noise from triggering GPIO pins. + for (int i=0; i<10; i++) { + bool value; + + mgos_msleep(10); + value=mgos_gpio_read(gpio); + if (value) { + LOG(LL_WARN, ("Button on GPIO %d no longer pressed after %d ms, skipping", gpio, 10*i)); + return; + } + } } state = channel_get(idx);