From c58e765253c4e9010e0813f8b2a4b8959f04c075 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Mon, 28 Nov 2022 20:28:40 -0600 Subject: [PATCH] Enable to keep actuator on off state in case of restart (#1334) and publish status as a json when triggering actuator on off through a button --- main/ZactuatorONOFF.ino | 13 ++++++++++++- main/config_ONOFF.h | 4 +--- main/main.ino | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/main/ZactuatorONOFF.ino b/main/ZactuatorONOFF.ino index b15d499..cf54391 100644 --- a/main/ZactuatorONOFF.ino +++ b/main/ZactuatorONOFF.ino @@ -30,6 +30,13 @@ #ifdef ZactuatorONOFF +void setupONOFF() { + pinMode(ACTUATOR_ONOFF_GPIO, OUTPUT); +# ifdef ACTUATOR_ONOFF_DEFAULT + digitalWrite(ACTUATOR_ONOFF_GPIO, ACTUATOR_ONOFF_DEFAULT); +# endif +} + # if jsonReceiving void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) { if (cmpToMainTopic(topicOri, subjectMQTTtoONOFF)) { @@ -104,7 +111,11 @@ void ActuatorButtonTrigger() { } Log.trace(F("Actuator triggered %s by button" CR), level_string); digitalWrite(ACTUATOR_ONOFF_GPIO, level); - pub(subjectGTWONOFFtoMQTT, level_string); + // Send the state of the switch to the broker so as to update the status + StaticJsonDocument jsonBuffer; + JsonObject ONOFFdata = jsonBuffer.to(); + ONOFFdata["cmd"] = (int)level; + pub(subjectGTWONOFFtoMQTT, ONOFFdata); } #endif diff --git a/main/config_ONOFF.h b/main/config_ONOFF.h index 0fcb3c2..c5e66cc 100644 --- a/main/config_ONOFF.h +++ b/main/config_ONOFF.h @@ -39,9 +39,7 @@ extern void MQTTtoONOFF(char* topicOri, JsonObject& RFdata); #ifndef ACTUATOR_ON # define ACTUATOR_ON LOW // LOW or HIGH, 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 // ACTUATOR_ON or !ACTUATOR_ON, set to the state desired on reset. -#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 diff --git a/main/main.ino b/main/main.ino index 3e5dbdb..615a9a4 100644 --- a/main/main.ino +++ b/main/main.ino @@ -723,8 +723,8 @@ void setup() { delay(1500); #ifdef ZactuatorONOFF - pinMode(ACTUATOR_ONOFF_GPIO, OUTPUT); - digitalWrite(ACTUATOR_ONOFF_GPIO, ACTUATOR_ONOFF_DEFAULT); + setupONOFF(); + modules.add(ZactuatorONOFF); #endif #ifdef ZsensorBME280 setupZsensorBME280();