diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index b7aec11..1a8aa66 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -32,6 +32,7 @@ e-mail anklimov@gmail.com #endif extern PubSubClient mqttClient; +extern aJsonObject *root; #if !defined(DHT_DISABLE) || !defined(COUNTER_DISABLE) static volatile unsigned long nextPollMillisValue[5]; @@ -93,7 +94,7 @@ void Input::Parse() store = NULL; inType = 0; pin = 0; - if (inputObj && (inputObj->type == aJson_Object)) { + if (inputObj && (inputObj->type == aJson_Object) && root) { aJsonObject *itemBuffer; itemBuffer = aJson.getObjectItem(inputObj, "T"); if (itemBuffer) inType = static_cast(itemBuffer->valueint); @@ -110,7 +111,7 @@ void Input::Parse() void Input::setup() { -if (!isValid()) return; +if (!isValid() || (!root)) return; #ifndef CSSHDC_DISABLE in_ccs811 ccs811(this); @@ -222,7 +223,8 @@ void Input::counterPoll() { 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); + if (mqttClient.connected()) + mqttClient.publish(addrstr, valstr); setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT); debugSerial<valuestring,sizeof(addrstr)); +if (mqttClient.connected()) +{ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); if (newValue) { //send set command if (!scmd) mqttClient.publish(addrstr, "ON", true); @@ -531,9 +539,11 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri if (!rcmd) mqttClient.publish(addrstr, "OFF", true); else if (strlen(rcmd->valuestring))mqttClient.publish(addrstr, rcmd->valuestring, true); } - } - +} + } +} // emit if (item) { + //debugSerial <valuestring <valuestring); if (it.isValid()) { if (newValue) { //send set command @@ -548,7 +558,6 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri } } } -} void Input::onAnalogChanged(int newValue) { debugSerial << F("IN:") << (pin) << F("=") << newValue << endl; @@ -569,7 +578,8 @@ void Input::onAnalogChanged(int newValue) { if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); char strVal[16]; itoa(newValue,strVal,10); - mqttClient.publish(addrstr, strVal, true); + if (mqttClient.connected()) + mqttClient.publish(addrstr, strVal, true); } if (item) { @@ -591,7 +601,8 @@ bool Input::publishDataToDomoticz(int pollTimeIncrement, aJsonObject *emit, cons vsnprintf(valstr, sizeof(valstr) - 1, format, args); va_end(args); debugSerial << valstr; - mqttClient.publish(emit->valuestring, valstr); + if (mqttClient.connected()) + mqttClient.publish(emit->valuestring, valstr); if (pollTimeIncrement) setNextPollTime(millis() + pollTimeIncrement); debugSerial << F(" NextPollMillis=") << nextPollTime() << endl; diff --git a/lighthub/item.cpp b/lighthub/item.cpp index b8a2fb0..ed2b954 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -656,8 +656,18 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int subItemN) break; case CH_RELAY: { int k; + short inverse = 0; + + if (iaddr < 0) { + iaddr = -iaddr; + inverse = 1; + } pinMode(iaddr, OUTPUT); - digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? HIGH : LOW)); + + if (inverse) + digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? LOW : HIGH)); + else + digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? HIGH : LOW)); debugSerial< FM_OVERHEAT_CELSIUS && set) { - mqttClient.publish("/alarm/ovrht", itemArr->name); + if (mqttClient.connected()) + mqttClient.publish("/alarm/ovrht", itemArr->name); Ctrl(CMD_OFF); //Shut down } } else debugSerial << F("Modbus polling error=") << _HEX(result); outch = aJson.print(out); - mqttClient.publish(addrstr, outch); + if (mqttClient.connected()) + mqttClient.publish(addrstr, outch); free(outch); aJson.deleteItem(out); modbusBusy = 0; @@ -1283,7 +1296,8 @@ int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) { return -1; } debugSerial<")<valuestring, currentTemp); debugSerial << valstr; - mqttClient.publish(owEmitString, valstr); + if (mqttClient.connected()) + mqttClient.publish(owEmitString, valstr); return; } #endif @@ -766,7 +785,8 @@ void Changed(int i, DeviceAddress addr, float currentTemp) { //strcpy_P(addrstr, outprefix); setTopic(addrstr,sizeof(addrstr),T_OUT); strncat(addrstr, owEmitString, sizeof(addrstr)); - mqttClient.publish(addrstr, valstr); + if (mqttClient.connected()) + mqttClient.publish(addrstr, valstr); } // And translate temp to internal items owItem = aJson.getObjectItem(owObj, "item")->valuestring; @@ -870,8 +890,9 @@ void applyConfig() { if (item->type == aJson_Array && aJson.getArraySize(item)>1) { Item it(item); if (it.isValid()) { + short inverse = 0; int pin=it.getArg(); - if (pin<0) pin=-pin; + if (pin<0) {pin=-pin; inverse = 1;} int cmd = it.getCmd(); switch (it.itemType) { case CH_THERMO: @@ -880,6 +901,9 @@ void applyConfig() { { int k; pinMode(pin, OUTPUT); + if (inverse) + digitalWrite(pin, k = ((cmd == CMD_ON) ? LOW : HIGH)); + else digitalWrite(pin, k = ((cmd == CMD_ON) ? HIGH : LOW)); debugSerial<