From 5e11690b692d3eb5432f675bcf90bc59d007963b Mon Sep 17 00:00:00 2001 From: h2zero Date: Thu, 8 Jul 2021 22:09:50 -0600 Subject: [PATCH] Allow trigger gpio and input gpio to be the same pin. This will provide the expected funtionality for the reset trigger when the button is pressed for more than 10 seconds. Also allows the same pin to be used as an actuator/relay trigger if pressed for less than 10 seconds. --- main/ZsensorGPIOInput.ino | 17 ++++++++++++++++- main/config_ONOFF.h | 6 +++--- main/main.ino | 4 ++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/main/ZsensorGPIOInput.ino b/main/ZsensorGPIOInput.ino index 9c6a7be..5d4873b 100644 --- a/main/ZsensorGPIOInput.ino +++ b/main/ZsensorGPIOInput.ino @@ -29,7 +29,9 @@ #include "User_config.h" #ifdef ZsensorGPIOInput - +# if defined(TRIGGER_GPIO) && INPUT_GPIO == TRIGGER_GPIO +unsigned long resetTime = 0; +# endif unsigned long lastDebounceTime = 0; int InputState = 3; // Set to 3 so that it reads on startup int lastInputState = 3; @@ -57,6 +59,19 @@ void MeasureGPIOInput() { // delay, so take it as the actual current state: # if defined(ESP8266) || defined(ESP32) yield(); +# endif +# if defined(TRIGGER_GPIO) && INPUT_GPIO == TRIGGER_GPIO + if (reading == LOW) { + if (resetTime == 0) { + resetTime = millis(); + } else if ((millis() - resetTime) > 10000) { + Log.trace(F("Button Held" CR)); + Log.notice(F("Erasing ESP Config, restarting" CR)); + setup_wifimanager(true); + } + } else { + resetTime = 0; + } # endif // if the Input state has changed: if (reading != InputState) { diff --git a/main/config_ONOFF.h b/main/config_ONOFF.h index 8f1b358..8fa22ef 100644 --- a/main/config_ONOFF.h +++ b/main/config_ONOFF.h @@ -37,13 +37,13 @@ extern void MQTTtoONOFF(char* topicOri, JsonObject& RFdata); #define ONKey "setON" #define OFFKey "setOFF" #ifndef ACTUATOR_ON -# define ACTUATOR_ON LOW +# define ACTUATOR_ON LOW // 0 or 1, set to the output level of the gpio pin to turn the actuator on. #endif #ifndef ACTUATOR_ONOFF_DEFAULT -# define ACTUATOR_ONOFF_DEFAULT !ACTUATOR_ON +# define ACTUATOR_ONOFF_DEFAULT !ACTUATOR_ON // ACTUATOR_ON or !ACTUATOR_ON, set to the state desired on reset. #endif #ifndef ACTUATOR_BUTTON_TRIGGER_LEVEL -# define ACTUATOR_BUTTON_TRIGGER_LEVEL LOW +# define ACTUATOR_BUTTON_TRIGGER_LEVEL LOW // 0 or 1, set to the sensing level which to detect a button press to change the actuator state. #endif /*-------------------PIN DEFINITIONS----------------------*/ diff --git a/main/main.ino b/main/main.ino index 5164f8d..5ac655d 100644 --- a/main/main.ino +++ b/main/main.ino @@ -969,6 +969,9 @@ void saveConfigCallback() { # ifdef TRIGGER_GPIO void checkButton() { // code from tzapu/wifimanager examples +# if defined(INPUT_GPIO) && defined(ZsensorGPIOInput) && INPUT_GPIO == TRIGGER_GPIO + MeasureGPIOInput(); +# else // check for button press if (digitalRead(TRIGGER_GPIO) == LOW) { // poor mans debounce/press-hold, code not ideal for production @@ -984,6 +987,7 @@ void checkButton() { // code from tzapu/wifimanager examples } } } +# endif } # else void checkButton() {}