Skip to content

Commit

Permalink
Fix buffer overwrite in PIR MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Dec 17, 2023
1 parent 8522760 commit 13d5ded
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
16 changes: 8 additions & 8 deletions usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class PIRsensorSwitch : public Usermod
* switch strip on/off
*/
void switchStrip(bool switchOn);
void publishMqtt(const char* state);
void publishMqtt(bool switchOn);

// Create an MQTT Binary Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop.
void publishHomeAssistantAutodiscovery();
Expand Down Expand Up @@ -226,6 +226,7 @@ void PIRsensorSwitch::switchStrip(bool switchOn)
if (PIRtriggered && switchOn) return; //if already on and triggered before, do nothing
PIRtriggered = switchOn;
DEBUG_PRINT(F("PIR: strip=")); DEBUG_PRINTLN(switchOn?"on":"off");
publishMqtt(switchOn);
if (switchOn) {
if (m_onPreset) {
if (currentPlaylist>0 && !offMode) {
Expand Down Expand Up @@ -269,23 +270,22 @@ void PIRsensorSwitch::switchStrip(bool switchOn)
}
}

void PIRsensorSwitch::publishMqtt(const char* state)
void PIRsensorSwitch::publishMqtt(bool switchOn)
{
#ifndef WLED_DISABLE_MQTT
//Check if MQTT Connected, otherwise it will crash the 8266
if (WLED_MQTT_CONNECTED) {
char buf[128];
sprintf_P(buf, PSTR("%s/motion"), mqttDeviceTopic); //max length: 33 + 7 = 40
mqtt->publish(buf, 0, false, state);
mqtt->publish(buf, 0, false, switchOn?"on":"off");
// Domoticz formatted message
if (idx > 0) {
StaticJsonDocument <128> msg;
msg[F("idx")] = idx;
msg[F("RSSI")] = WiFi.RSSI();
msg[F("command")] = F("switchlight");
strcpy(buf, state); buf[0] = toupper(buf[0]);
msg[F("switchcmd")] = (const char *)buf;
serializeJson(msg, buf, 127);
msg[F("switchcmd")] = switchOn ? F("On") : F("Off");
serializeJson(msg, buf, 128);
mqtt->publish("domoticz/in", 0, false, buf);
}
}
Expand Down Expand Up @@ -337,7 +337,7 @@ bool PIRsensorSwitch::updatePIRsensorState()
offTimerStart = 0;
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true);
else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND);
publishMqtt("on");
//publishMqtt("on");
} else {
// start switch off timer
offTimerStart = millis();
Expand All @@ -355,7 +355,7 @@ bool PIRsensorSwitch::handleOffTimer()
if (enabled == true) {
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()) || PIRtriggered)) switchStrip(false);
else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND);
publishMqtt("off");
//publishMqtt("off");
}
return true;
}
Expand Down
1 change: 0 additions & 1 deletion usermods/Temperature/usermod_temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ void UsermodTemperature::loop() {
msg[F("nvalue")] = 0;
msg[F("svalue")] = String(getTemperatureC());
serializeJson(msg, subuf, 127);
DEBUG_PRINTLN(subuf);
mqtt->publish("domoticz/in", 0, false, subuf);
}
} else {
Expand Down

0 comments on commit 13d5ded

Please sign in to comment.