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

This commit is contained in:
Pim van Pelt
2018-11-01 14:47:35 +01:00
parent d332f08da4
commit 6434cb6db0

View File

@ -161,6 +161,13 @@ void channel_set(int idx, bool state) {
} }
mqtt_publish_stat("channel", "{idx: %d, relay_state: %d}", idx, channel_get(idx)); 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) { bool channel_get(int idx) {
@ -186,6 +193,18 @@ void channel_handler(int gpio, void *arg) {
LOG(LL_INFO, ("RPC triggered channel %d", idx)); LOG(LL_INFO, ("RPC triggered channel %d", idx));
} else { } else {
LOG(LL_INFO, ("Button on GPIO %d triggered channel %d", gpio, idx)); 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); state = channel_get(idx);