Fix buffer overwrite in PIR MQTT

This commit is contained in:
Blaz Kristan 2023-12-17 11:34:44 +01:00
parent 8522760c70
commit 13d5deddec
2 changed files with 8 additions and 9 deletions

View File

@ -96,7 +96,7 @@ private:
* switch strip on/off * switch strip on/off
*/ */
void switchStrip(bool switchOn); 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. // 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(); void publishHomeAssistantAutodiscovery();
@ -226,6 +226,7 @@ void PIRsensorSwitch::switchStrip(bool switchOn)
if (PIRtriggered && switchOn) return; //if already on and triggered before, do nothing if (PIRtriggered && switchOn) return; //if already on and triggered before, do nothing
PIRtriggered = switchOn; PIRtriggered = switchOn;
DEBUG_PRINT(F("PIR: strip=")); DEBUG_PRINTLN(switchOn?"on":"off"); DEBUG_PRINT(F("PIR: strip=")); DEBUG_PRINTLN(switchOn?"on":"off");
publishMqtt(switchOn);
if (switchOn) { if (switchOn) {
if (m_onPreset) { if (m_onPreset) {
if (currentPlaylist>0 && !offMode) { if (currentPlaylist>0 && !offMode) {
@ -269,23 +270,22 @@ void PIRsensorSwitch::switchStrip(bool switchOn)
} }
} }
void PIRsensorSwitch::publishMqtt(const char* state) void PIRsensorSwitch::publishMqtt(bool switchOn)
{ {
#ifndef WLED_DISABLE_MQTT #ifndef WLED_DISABLE_MQTT
//Check if MQTT Connected, otherwise it will crash the 8266 //Check if MQTT Connected, otherwise it will crash the 8266
if (WLED_MQTT_CONNECTED) { if (WLED_MQTT_CONNECTED) {
char buf[128]; char buf[128];
sprintf_P(buf, PSTR("%s/motion"), mqttDeviceTopic); //max length: 33 + 7 = 40 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 // Domoticz formatted message
if (idx > 0) { if (idx > 0) {
StaticJsonDocument <128> msg; StaticJsonDocument <128> msg;
msg[F("idx")] = idx; msg[F("idx")] = idx;
msg[F("RSSI")] = WiFi.RSSI(); msg[F("RSSI")] = WiFi.RSSI();
msg[F("command")] = F("switchlight"); msg[F("command")] = F("switchlight");
strcpy(buf, state); buf[0] = toupper(buf[0]); msg[F("switchcmd")] = switchOn ? F("On") : F("Off");
msg[F("switchcmd")] = (const char *)buf; serializeJson(msg, buf, 128);
serializeJson(msg, buf, 127);
mqtt->publish("domoticz/in", 0, false, buf); mqtt->publish("domoticz/in", 0, false, buf);
} }
} }
@ -337,7 +337,7 @@ bool PIRsensorSwitch::updatePIRsensorState()
offTimerStart = 0; offTimerStart = 0;
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true); if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true);
else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND); else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND);
publishMqtt("on"); //publishMqtt("on");
} else { } else {
// start switch off timer // start switch off timer
offTimerStart = millis(); offTimerStart = millis();
@ -355,7 +355,7 @@ bool PIRsensorSwitch::handleOffTimer()
if (enabled == true) { if (enabled == true) {
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()) || PIRtriggered)) switchStrip(false); if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()) || PIRtriggered)) switchStrip(false);
else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND); else if (NotifyUpdateMode != CALL_MODE_NO_NOTIFY) updateInterfaces(CALL_MODE_WS_SEND);
publishMqtt("off"); //publishMqtt("off");
} }
return true; return true;
} }

View File

@ -283,7 +283,6 @@ void UsermodTemperature::loop() {
msg[F("nvalue")] = 0; msg[F("nvalue")] = 0;
msg[F("svalue")] = String(getTemperatureC()); msg[F("svalue")] = String(getTemperatureC());
serializeJson(msg, subuf, 127); serializeJson(msg, subuf, 127);
DEBUG_PRINTLN(subuf);
mqtt->publish("domoticz/in", 0, false, subuf); mqtt->publish("domoticz/in", 0, false, subuf);
} }
} else { } else {