diff --git a/lighthub/abstractch.cpp b/lighthub/abstractch.cpp index fdccd18..26b9f0e 100644 --- a/lighthub/abstractch.cpp +++ b/lighthub/abstractch.cpp @@ -10,7 +10,7 @@ extern PubSubClient mqttClient; extern int8_t ethernetIdleCount; int abstractCh::publishTopic(const char* topic, long value, const char* subtopic) -{ +{ char valstr[16]; printUlongValueToStr(valstr, value); return publishTopic(topic, valstr,subtopic); @@ -19,7 +19,7 @@ int abstractCh::publishTopic(const char* topic, long value, const char* subtopic int abstractCh::publishTopic(const char* topic, float value, const char* subtopic) { char valstr[16]; - printFloatValueToStr(value, valstr); + printFloatValueToStr(valstr, value); return publishTopic(topic, valstr,subtopic); }; diff --git a/lighthub/abstractin.cpp b/lighthub/abstractin.cpp index 4624c1e..a6a9b21 100644 --- a/lighthub/abstractin.cpp +++ b/lighthub/abstractin.cpp @@ -20,7 +20,7 @@ int abstractIn::publish(long value, const char* subtopic) int abstractIn::publish(float value, const char* subtopic) { char valstr[16]; - printFloatValueToStr(value, valstr); + printFloatValueToStr(valstr, value); return publish(valstr,subtopic); }; diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 5a70aad..4778bd6 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -503,11 +503,11 @@ debugSerial << F("IN:") << pin << F(" DHT22 type. T=") << temp << F("°C H=") << strncpy(addrstr, emit->valuestring, sizeof(addrstr)); if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring); strcat(addrstr, "T"); - printFloatValueToStr(temp, valstr); + printFloatValueToStr(valstr, temp); if (mqttClient.connected() && !ethernetIdleCount) mqttClient.publish(addrstr, valstr); addrstr[strlen(addrstr) - 1] = 'H'; - printFloatValueToStr(humidity, valstr); + printFloatValueToStr(valstr, humidity); if (mqttClient.connected() && !ethernetIdleCount) mqttClient.publish(addrstr, valstr); diff --git a/lighthub/itemCmd.cpp b/lighthub/itemCmd.cpp index acea3c8..4423db1 100644 --- a/lighthub/itemCmd.cpp +++ b/lighthub/itemCmd.cpp @@ -1340,7 +1340,10 @@ char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags, bool scale100 snprintf(argPtr, bufLen, "%ld", param.asInt32); break; case ST_TENS: - snprintf(argPtr, bufLen, "%ld.%0"QUOTE(TENS_FRACT_LEN)"d", param.asInt32/TENS_BASE, abs(param.asInt32 % TENS_BASE)); + if (param.asInt32<0) + snprintf(argPtr, bufLen, "-%ld.%0"QUOTE(TENS_FRACT_LEN)"d", abs(param.asInt32)/TENS_BASE, abs(param.asInt32 % TENS_BASE)); + else + snprintf(argPtr, bufLen, "%ld.%0"QUOTE(TENS_FRACT_LEN)"d", param.asInt32/TENS_BASE, abs(param.asInt32 % TENS_BASE)); break; case ST_HSV255: colorTemp=getColorTemp(); diff --git a/lighthub/modules/out_counter.cpp b/lighthub/modules/out_counter.cpp index 021ef80..ffaa563 100644 --- a/lighthub/modules/out_counter.cpp +++ b/lighthub/modules/out_counter.cpp @@ -14,7 +14,8 @@ void out_counter::getConfig() { if (!item) return; impulse = item->getFloatArg(0); - period = item->getFloatArg(1)*1000.0; + period = item->getFloatArg(1)*1000.0; + //debugSerial<<"CTR: imp:"<getExt(); if (timer && period && isTimeOver(timer,millis(),period)) { item->setExt(millisNZ()); + itemCmd st; st.loadItem(item,SEND_PARAMETERS|SEND_COMMAND); float val = st.getFloat(); //short cmd = st.getCmd(); + debugSerial<<"CTR: tick val:"<SendStatus(SEND_PARAMETERS); } return 0; @@ -82,13 +88,11 @@ case S_SET: if (!item->getExt()) { item->setExt(millisNZ()); - //relay(true); } } else { item->setExt(0); - } } return 1; @@ -105,13 +109,11 @@ case S_CMD: if (!item->getExt()) { item->setExt(millisNZ()); - //relay(true); } return 1; case CMD_OFF: item->setExt(0); - return 1; default: diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index 93fe93f..86edfa0 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -15,6 +15,7 @@ extern aJsonObject *modbusObj; extern ModbusMaster node; extern short modbusBusy; extern void modbusIdle(void) ; +static uint32_t mbusSlenceTimer = 0; struct reg_t { @@ -116,13 +117,13 @@ bool out_Modbus::getConfig() aJsonObject * templateIdObj = aJson.getArrayItem(item->itemArg, 1); if (templateIdObj->type != aJson_String || !templateIdObj->valuestring) { - errorSerial<valuestring); if (! templateObj) { - errorSerial<valuestring<valuestring<timestamp=millisNZ(); if (getConfig()) { - infoSerial<itemArr->name<itemArr->name<driverStatus = CST_INITIALIZED; return 1; } else - { errorSerial<driverStatus = CST_FAILED; return 0; } @@ -187,7 +188,7 @@ else int out_Modbus::Stop() { -debugSerial.println("Modbus De-Init"); +debugSerial.println("MBUS: De-Init"); delete store; item->setPersistent(NULL); @@ -221,8 +222,9 @@ switch (regType) { result = node.readInputRegisters(reg, count); break; default: - debugSerial<name<name<type==aJson_Array || mapObj->type==aJson_Object)) { mappedParam=mappedParam.doReverseMapping(mapObj); - debugSerial << F("Mapped:")<type ==aJson_Object) @@ -328,7 +330,7 @@ int out_Modbus::findRegister(int registerNum, int posInBuffer, int regType) } else //No container to store value yet { - debugSerial<name<name<type==aJson_Int && (settedValue->valueint == param)) { - debugSerial<baud, (store->serialParam)); #endif - debugSerial<< store->baud << F("---")<< store->serialParam<baud << F("---")<< store->serialParam<getArg(0), modbusSerial); } @@ -458,7 +460,8 @@ int out_Modbus::sendModbus(char * paramName, int32_t value, uint8_t regType) res = node.writeSingleRegister(regObj->valueint,(value & 0xFFFF)>> 8); break; } - debugSerial<valueint<valueint<itemArg, 2); if (itemParametersObj && itemParametersObj->type ==aJson_Object) @@ -486,18 +489,22 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object) lineInitialized=true; initLine(); } + debugSerial<<"MBUS: SEND "<itemArr->name<<" "; switch (sendModbus(execObj->name,outValue->valueint,outValue->subtype)) { case 1: //success execObj->subtype&=~ MB_NEED_SEND; + ///return 1; //relax + break; case 0: //fault execObj->subtype |= MB_SEND_ERROR; - errorSerial<name<name<subtype & 3) != MB_SEND_ATTEMPTS) execObj->subtype++; + errorSerial<<"Attempt: "<< (execObj->subtype & 3) <name<name<subtype&=~ MB_NEED_SEND; } } @@ -508,7 +515,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object) } -if (isTimeOver(store->timestamp,millis(),store->pollingInterval)) +if (isTimeOver(store->timestamp,millis(),store->pollingInterval) && ( !mbusSlenceTimer || isTimeOver(mbusSlenceTimer,millis(),100))) { // Clean_up SEND_ERROR flag @@ -518,7 +525,14 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object) while (execObj && execObj->type == aJson_Object) { if (execObj->subtype & MB_SEND_ERROR) execObj->subtype&=~ MB_SEND_ERROR; - if ((execObj->subtype & 0x3) >= MB_SEND_ATTEMPTS) execObj->subtype&=~ MB_NEED_SEND; + if ((execObj->subtype & 0x3) >= MB_SEND_ATTEMPTS) + { + //execObj->subtype&=~ MB_NEED_SEND; + //Clean ERROR, NEED, Attempts + errorSerial<<"MBUS: send failed "<itemArr->name<subtype = 0; + } + execObj=execObj->next; } } @@ -526,7 +540,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object) // if some polling configured if (store->pollingRegisters || store->pollingIrs) { - debugSerial<itemArr->name << endl; + debugSerial<itemArr->name << endl; modbusBusy=1; if (!lineInitialized) @@ -537,7 +551,7 @@ if (store->pollingRegisters || store->pollingIrs) pollModbus(store->pollingRegisters,MODBUS_HOLDING_REG_TYPE); pollModbus(store->pollingIrs,MODBUS_INPUT_REG_TYPE); - debugSerial<itemArr->name << endl; + debugSerial<itemArr->name << endl; //Non blocking waiting to release line uint32_t time = millis(); @@ -599,7 +613,7 @@ aJsonObject * mapObj = aJson.getObjectItem(templateParamObj, "map"); Value=cmdValue.getTens_raw()*(100/TENS_BASE); } -debugSerial<itemArg, 2); if (itemParametersObj && itemParametersObj->type ==aJson_Object) { @@ -680,7 +694,7 @@ else } templateParamObj=templateParamObj->next; } - if (!suffixFinded) errorSerial<setCmd(CMD_ENABLE); - item->SendStatus(SEND_COMMAND); + item->SendStatus(SEND_COMMAND); + //executeCommand(oCmd,-1,value); return 1; case CMD_DISABLE: item->setCmd(CMD_DISABLE); item->SendStatus(SEND_COMMAND); + //executeCommand(oCmd,-1,value); return 1; /* case CMD_OFF: diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index 832e806..cc6156d 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -124,9 +124,14 @@ itemCmd getNumber(char **chan) { long fractnumbers = 0; short fractlen = 0; short intlen = 0; + bool negative = false; char * intptr = * chan; - if (*intptr == '-') intptr ++; + if (*intptr == '-') + { + negative=true; + intptr ++; + } while (isDigit(*(intptr+intlen))) intlen++; char * fractptr = strchr(*chan,'.'); @@ -146,7 +151,7 @@ itemCmd getNumber(char **chan) { else if (fractlen<=TENS_FRACT_LEN && intlen+TENS_FRACT_LEN<=9) { long intpart = atol(*chan); - val.Tens_raw(intpart*TENS_BASE+((intpart>=0)?fractnumbers:-fractnumbers)); + val.Tens_raw(intpart*TENS_BASE+((negative)?-fractnumbers:fractnumbers)); } else val.Float(atof(*chan)); @@ -243,7 +248,7 @@ void parseBytes(const char *str, char separator, byte *bytes, int maxBytes, int } -void printFloatValueToStr(float value, char *valstr) { +void printFloatValueToStr(char *valstr, float value) { #if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32) sprintf(valstr, "%2.1f", value); #endif @@ -499,6 +504,8 @@ return buf; void printUlongValueToStr(char *valstr, unsigned long value) { + ultoa(value,valstr,10); + /* char buf[11]; int i=0; for(;value>0;i++){ @@ -510,7 +517,7 @@ void printUlongValueToStr(char *valstr, unsigned long value) { for(int n=0;n<=i;n++){ valstr[n]=buf[i-n-1]; } - valstr[i]='\0'; + valstr[i]='\0';*/ } @@ -771,7 +778,7 @@ const serial_st serialModes_P[] PROGMEM = #define serialModesNum sizeof(serialModes_P)/sizeof(serial_st) serialParamType str2SerialParam(char * str) -{ debugSerial<"); +{ //debugSerial<"); for(uint8_t i=0; i (SERIAL_8N1); } diff --git a/lighthub/utils.h b/lighthub/utils.h index 577f5ba..962c545 100644 --- a/lighthub/utils.h +++ b/lighthub/utils.h @@ -58,7 +58,7 @@ itemCmd getNumber(char ** chan); unsigned long freeRam (); void parseBytes(const char* str, char separator, byte* bytes, int maxBytes, int base); int log(const char *str, ...); -void printFloatValueToStr(float value, char *valstr); +void printFloatValueToStr(char *valstr, float value); void ReadUniqueID( uint32_t * pdwUniqueID ); int _inet_aton(const char* aIPAddrString, IPAddress& aResult); char *_inet_ntoa_r(IPAddress addr, char *buf, int buflen);