From 6434cb6db087b9fbbc1fbf02c65021fd25856fe9 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Thu, 1 Nov 2018 14:47:35 +0100 Subject: [PATCH] Sleep the MCU 250ms after relay on switches, to avoid brownouts. Scan the pushbutton for 100ms to ensure it was still pressed, to avoid RFI from triggering --- src/channel.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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);