From b4003d4ea188311ed57f6bb6435ec35336bf9a0f Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 18 Jan 2023 18:10:54 -0600 Subject: [PATCH] Fix actuator manual control with button press and overheat protection (#1403) Manual control with a button press triggered the relay but 2 times (when pressing and releasing), fixing this. Check that we have two consecutive readings of temperature above the threshold to trigger the actuator to OFF And remove it by default to the shelly, as I see unjustified jumps of temperature above 80C. --- main/ZactuatorONOFF.ino | 23 ++++++++++++----------- main/ZsensorGPIOInput.ino | 9 ++++++++- main/config_ONOFF.h | 8 ++++---- platformio.ini | 5 ++++- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/main/ZactuatorONOFF.ino b/main/ZactuatorONOFF.ino index 11222f3..7a03fbc 100644 --- a/main/ZactuatorONOFF.ino +++ b/main/ZactuatorONOFF.ino @@ -109,12 +109,15 @@ void MQTTtoONOFF(char* topicOri, char* datacallback) { void OverHeatingRelayOFF() { # if defined(ESP32) && !defined(NO_INT_TEMP_READING) if (millis() > (timeinttemp + TimeBetweenReadingIntTemp)) { + static float previousInternalTempc = 0; float internalTempc = intTemperatureRead(); Log.trace(F("Internal temperature of the ESP32 %F" CR), internalTempc); - if (internalTempc > MAX_TEMP_ACTUATOR && digitalRead(ACTUATOR_ONOFF_GPIO) == ACTUATOR_ON) { + // We switch OFF the actuator if the temperature of the ESP32 is more than MAX_TEMP_ACTUATOR two consecutive times, so as to avoid false single readings to trigger the relay OFF. + if (internalTempc > MAX_TEMP_ACTUATOR && previousInternalTempc > MAX_TEMP_ACTUATOR && digitalRead(ACTUATOR_ONOFF_GPIO) == ACTUATOR_ON) { Log.error(F("[ActuatorONOFF] OverTemperature detected ( %F > %F ) switching OFF Actuator" CR), internalTempc, MAX_TEMP_ACTUATOR); - ActuatorManualTrigger(!ACTUATOR_ON); + ActuatorTrigger(); } + previousInternalTempc = internalTempc; timeinttemp = millis(); } # endif @@ -123,15 +126,13 @@ void OverHeatingRelayOFF() { void OverHeatingRelayOFF() {} # endif -void ActuatorManualTrigger(uint8_t level) { -# ifdef ACTUATOR_BUTTON_TRIGGER_LEVEL - if (level == ACTUATOR_BUTTON_TRIGGER_LEVEL) { - // Change level value to the opposite of the current level - level = !digitalRead(ACTUATOR_ONOFF_GPIO); - } -# else - level = !digitalRead(ACTUATOR_ONOFF_GPIO); -# endif +/* + Handling of actuator control following the cases below: + -Button press, if the button goes to ACTUATOR_BUTTON_TRIGGER_LEVEL we change the Actuator level + -Status less switch state change (a switch without ON OFF labels), an action of this type of switch will trigger a change of the actuator state independently from the switch position +*/ +void ActuatorTrigger() { + uint8_t level = !digitalRead(ACTUATOR_ONOFF_GPIO); Log.trace(F("Actuator triggered %d" CR), level); digitalWrite(ACTUATOR_ONOFF_GPIO, level); // Send the state of the switch to the broker so as to update the status diff --git a/main/ZsensorGPIOInput.ino b/main/ZsensorGPIOInput.ino index 279defb..e18b460 100644 --- a/main/ZsensorGPIOInput.ino +++ b/main/ZsensorGPIOInput.ino @@ -89,7 +89,14 @@ void MeasureGPIOInput() { # if defined(ZactuatorONOFF) && defined(ACTUATOR_TRIGGER) //Trigger the actuator if we are not at startup - if (InputState != 3) ActuatorManualTrigger(InputState); + if (InputState != 3) { +# if defined(ACTUATOR_BUTTON_TRIGGER_LEVEL) + if (InputState == ACTUATOR_BUTTON_TRIGGER_LEVEL) + ActuatorTrigger(); // Button press trigger +# else + ActuatorTrigger(); // Switch trigger +# endif + } # endif InputState = reading; } diff --git a/main/config_ONOFF.h b/main/config_ONOFF.h index 11e8bb8..6e8cfe4 100644 --- a/main/config_ONOFF.h +++ b/main/config_ONOFF.h @@ -40,11 +40,11 @@ extern void MQTTtoONOFF(char* topicOri, JsonObject& RFdata); # define ACTUATOR_ON LOW // LOW or HIGH, set to the output level of the GPIO pin to turn the actuator on. #endif //# define ACTUATOR_ONOFF_DEFAULT !ACTUATOR_ON // ACTUATOR_ON or !ACTUATOR_ON, set to the state desired on reset. -#ifndef ACTUATOR_BUTTON_TRIGGER_LEVEL -//# 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 #ifndef ACTUATOR_TRIGGER -# define ACTUATOR_TRIGGER false // false or true, enable to control an actuator directly from the board switch (default behavior if true), or by button if ACTUATOR_BUTTON_TRIGGER_LEVEL is defined +# define ACTUATOR_TRIGGER false // false or true, enable to control an actuator directly from the board switch (default behavior if true), or by button if ACTUATOR_BUTTON_TRIGGER_LEVEL is also defined +#endif +#ifndef ACTUATOR_BUTTON_TRIGGER_LEVEL +//# define ACTUATOR_BUTTON_TRIGGER_LEVEL LOW // 0 or 1, set to the level which to detect a button press to change the actuator state. #endif #ifndef MAX_TEMP_ACTUATOR //# define MAX_TEMP_ACTUATOR 70 // Temperature that will trigger the relay to go OFF diff --git a/platformio.ini b/platformio.ini index ca97d65..d45c7fc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -279,6 +279,8 @@ build_flags = '-DZgatewayIR="IR"' '-DZgatewayBT="BT"' '-DZactuatorONOFF="ONOFF"' + '-DACTUATOR_BUTTON_TRIGGER_LEVEL=0' + '-ACTUATOR_TRIGGER=true' '-DZactuatorFASTLED="FASTLED"' '-DZactuatorPWM="PWM"' '-DZsensorINA226="INA226"' @@ -296,6 +298,7 @@ build_flags = '-DZsensorGPIOInput="GPIOInput"' '-DZsensorGPIOKeyCode="GPIOKeyCode"' '-DZsensorRN8209="RN8209"' + '-DMAX_TEMP_ACTUATOR=80' '-DZgatewayWeatherStation="WeatherStation"' '-DsimplePublishing=true' '-DWM_PWD_FROM_MAC=true' @@ -1066,7 +1069,7 @@ build_flags = '-DACTUATOR_ONOFF_GPIO=26' '-DZsensorADC="ADC"' '-DADC_GPIO=32' - '-DMAX_TEMP_ACTUATOR=70' + ;'-DMAX_TEMP_ACTUATOR=70' ; uncomment to activate automatic switch off following ESP32 temperature measurement (experimental) '-DUSE_MAC_AS_GATEWAY_NAME' '-DFRAMEWORK_ARDUINO_SOLO1' '-DGATEWAY_MANUFACTURER="Shelly"'