From 7595a994743b337fdb19a4e2882c79a1cb8cbb70 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sat, 9 Mar 2019 23:38:57 +0300 Subject: [PATCH] -default topics prefix used if no "/" in "emit" parameters of output -Thermostat should be working with DHT22 -Negative PIN# in thermostat item - reverse logic: HIGH level of output should stop heating --- lighthub/inputs.cpp | 33 ++++++++++++++++++++++----------- lighthub/item.cpp | 12 ++++++------ lighthub/item.h | 2 +- lighthub/main.cpp | 12 +++++++----- lighthub/options.h | 2 +- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index b87a3f9..9e43d68 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -162,8 +162,9 @@ void Input::counterPoll() { aJsonObject *emit = aJson.getObjectItem(inputObj, "emit"); if (emit) { char valstr[10]; - char addrstr[100] = ""; - strcat(addrstr, emit->valuestring); + char addrstr[MQTT_TOPIC_LENGTH]; + strncpy(addrstr,emit->valuestring,sizeof(addrstr)); + if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); sprintf(valstr, "%d", counterValue); mqttClient.publish(addrstr, valstr); setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT); @@ -225,9 +226,11 @@ void Input::dht22Poll() { float humidity = dht.readHumidity(); #endif aJsonObject *emit = aJson.getObjectItem(inputObj, "emit"); + aJsonObject *item = aJson.getObjectItem(inputObj, "item"); + if (item) thermoSetCurTemp(item, temp); debugSerial << F("IN:") << pin << F(" DHT22 type. T=") << temp << F("°C H=") << humidity << F("%"); if (emit && temp && humidity && temp == temp && humidity == humidity) { - char addrstr[100] = ""; + char addrstr[MQTT_TOPIC_LENGTH] = ""; #ifdef WITH_DOMOTICZ if(getIdxField()){ publishDataToDomoticz(DHT_POLL_DELAY_DEFAULT, emit, "{\"idx\":%s,\"svalue\":\"%.1f;%.0f;0\"}", getIdxField(), temp, humidity); @@ -235,7 +238,9 @@ void Input::dht22Poll() { } #endif char valstr[10]; - strcat(addrstr, emit->valuestring); + + strncpy(addrstr, emit->valuestring, sizeof(addrstr)); + if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); strcat(addrstr, "T"); printFloatValueToStr(temp, valstr); mqttClient.publish(addrstr, valstr); @@ -444,13 +449,16 @@ void Input::onContactChanged(int newValue) { : publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField()); } else #endif +char addrstr[MQTT_TOPIC_LENGTH]; +strncpy(addrstr,emit->valuestring,sizeof(addrstr)); +if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); if (newValue) { //send set command - if (!scmd) mqttClient.publish(emit->valuestring, "ON", true); + if (!scmd) mqttClient.publish(addrstr, "ON", true); else if (strlen(scmd->valuestring)) - mqttClient.publish(emit->valuestring, scmd->valuestring, true); + mqttClient.publish(addrstr, scmd->valuestring, true); } else { //send reset command - if (!rcmd) mqttClient.publish(emit->valuestring, "OFF", true); - else if (strlen(rcmd->valuestring))mqttClient.publish(emit->valuestring, rcmd->valuestring, true); + if (!rcmd) mqttClient.publish(addrstr, "OFF", true); + else if (strlen(rcmd->valuestring))mqttClient.publish(addrstr, rcmd->valuestring, true); } } @@ -485,9 +493,12 @@ void Input::onAnalogChanged(int newValue) { // : publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField()); // } else //#endif - char strVal[16]; - itoa(newValue,strVal,10); - mqttClient.publish(emit->valuestring, strVal, true); + char addrstr[MQTT_TOPIC_LENGTH]; + strncpy(addrstr,emit->valuestring,sizeof(addrstr)); + if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); + char strVal[16]; + itoa(newValue,strVal,10); + mqttClient.publish(addrstr, strVal, true); } if (item) { diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 4686098..25bf240 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -287,7 +287,7 @@ if (isSet) Par[0] = map(hsv.h, 0, 255, 0, 365); Par[1] = map(hsv.s, 0, 255, 0, 100); Par[2] = map(hsv.v, 0, 255, 0, 100); - return Ctrl(0, 3, Par, send, subItem); + return Ctrl(0, 3, Par, send, subItemN); } break; } @@ -296,21 +296,21 @@ if (isSet) // if (item.getEnableCMD(500) || lanStatus == 4) return Ctrl(cmd, 0, NULL, - send, subItem); //Accept ON command not earlier then 500 ms after set settings (Homekit hack) + send, subItemN); //Accept ON command not earlier then 500 ms after set settings (Homekit hack) // else debugSerial<= thermoSetting) { - digitalWrite(thermoPin, LOW); + if (thermoPin<0) digitalWrite(-thermoPin, HIGH); digitalWrite(thermoPin, LOW); debugSerial<valueint = (int) T_ATTEMPTS; } } - } -} + return 1;} +return 0;} diff --git a/lighthub/options.h b/lighthub/options.h index 80dad75..29c8554 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -98,7 +98,7 @@ #endif #define MQTT_SUBJECT_LENGTH 20 -#define MQTT_TOPIC_LENGTH 20 +#define MQTT_TOPIC_LENGTH 64 #ifndef DMX_DISABLE #define _dmxin