Skip to content

Commit

Permalink
[ONOFF] ON OFF behavior fixes (#1828)
Browse files Browse the repository at this point in the history
-Avoid plug restart in case of OverCurrent detection, introduced with central queue add
-publish uselaststate value
-if uselaststate is false we switch off the relay at restart
  • Loading branch information
1technophile authored Dec 7, 2023
1 parent 87cb2fe commit 8c8dc6b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
21 changes: 13 additions & 8 deletions main/ZactuatorONOFF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ void setupONOFF() {
# ifdef ACTUATOR_ONOFF_DEFAULT
digitalWrite(ACTUATOR_ONOFF_GPIO, ACTUATOR_ONOFF_DEFAULT);
# elif defined(ESP32)
if (ONOFFConfig.useLastStateOnStart)
if (ONOFFConfig.useLastStateOnStart) {
digitalWrite(ACTUATOR_ONOFF_GPIO, ONOFFConfig.ONOFFState);
} else {
digitalWrite(ACTUATOR_ONOFF_GPIO, !ACTUATOR_ON);
}
# endif
updatePowerIndicator();
Log.trace(F("ZactuatorONOFF setup done" CR));
Expand Down Expand Up @@ -194,6 +197,7 @@ void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) {

// Load config from json if available
ONOFFConfig_fromJson(ONOFFdata);
stateONOFFMeasures();
}
}
# endif
Expand Down Expand Up @@ -285,14 +289,12 @@ void ActuatorTrigger() {
} else {
PowerIndicatorOFF();
}
// Send the state of the switch to the broker so as to update the status
StaticJsonDocument<64> jsonBuffer;
JsonObject ONOFFdata = jsonBuffer.to<JsonObject>();
ONOFFdata["cmd"] = (int)level;
ONOFFdata["origin"] = subjectGTWONOFFtoMQTT;
handleJsonEnqueue(ONOFFdata, QueueSemaphoreTimeOutTask);

# ifdef ESP32
if (ONOFFConfig.useLastStateOnStart) {
StaticJsonDocument<64> jsonBuffer;
JsonObject ONOFFdata = jsonBuffer.to<JsonObject>();
ONOFFdata["cmd"] = (int)level;
ONOFFdata["save"] = true;
ONOFFConfig_fromJson(ONOFFdata);
}
Expand All @@ -302,9 +304,12 @@ void ActuatorTrigger() {

void stateONOFFMeasures() {
//Publish actuator state
StaticJsonDocument<64> jsonBuffer;
StaticJsonDocument<128> jsonBuffer;
JsonObject ONOFFdata = jsonBuffer.to<JsonObject>();
ONOFFdata["cmd"] = (int)digitalRead(ACTUATOR_ONOFF_GPIO);
# ifdef ESP32
ONOFFdata["uselaststate"] = ONOFFConfig.useLastStateOnStart;
# endif
ONOFFdata["origin"] = subjectGTWONOFFtoMQTT;
handleJsonEnqueue(ONOFFdata, QueueSemaphoreTimeOutTask);
}
Expand Down
5 changes: 3 additions & 2 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ void pubMqttDiscovery() {

# ifdef ZactuatorONOFF
Log.trace(F("actuatorONOFFDiscovery" CR));
char* actuatorONOFF[8] = {"switch", "actuatorONOFF", "", "", "", "{\"cmd\":1}", "{\"cmd\":0}", ""};
char* actuatorONOFF[8] = {"switch", "actuatorONOFF", "", "", "{{ value_json.cmd }}", "{\"cmd\":1}", "{\"cmd\":0}", ""};
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement

//trc(actuatorONOFF[1]);
Expand All @@ -959,7 +959,8 @@ void pubMqttDiscovery() {
actuatorONOFF[5], actuatorONOFF[6], actuatorONOFF[7],
0, Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoONOFF,
"", "", "", "", false, // device name, device manufacturer, device model, device ID, retain
stateClassNone //State Class
stateClassNone, //State Class
"0", "1" //state_off, state_on
);
# endif

Expand Down
2 changes: 1 addition & 1 deletion main/ZsensorRN8209.ino
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void setupRN8209() {
cal.EC = RN8209_EC;
set_user_param(cal);
init_8209c_interface();
xTaskCreate(rn8209_loop, "rn8209_loop", 5000, NULL, 10, &rn8209TaskHandle);
xTaskCreate(rn8209_loop, "rn8209_loop", 5500, NULL, 10, &rn8209TaskHandle);
//esp_task_wdt_add(rn8209TaskHandle);
Log.trace(F("ZsensorRN8209 setup done " CR));
}
Expand Down

0 comments on commit 8c8dc6b

Please sign in to comment.