MQTT To ASCII over TCP/IP

This is a function prior to the "buffer parser" node. It sets the values in the proper object. You can see I am setting a value in a variable and using the variable in the object. It gets the payload which is a buffer type, sets the object value and passes this with the original payload untouched.

image

var buffer=msg.payload;
var bufferlen = buffer.length;
var mymsg= {
    options: {
        resultType: "object", 
        msgProperty: "payload"},
    items: [
        {
            name: "unit1",
            type: "hex",
            offset: 0,
            length: bufferlen
        }
	]
}
msg.spec=mymsg;
return msg;

@tree-frog

If the purpose of the dynamic spec (above) is to convert the full input buffer to a HEX string, you can achieve this in the UI also. Just specify the length as -1

From the built in doc...

  • length (Number|optional) The quantity of items to be returned. e.g. 6 bools or 12 floats or 34 int32s. NOTE: setting length to -1 will attempt to read all bytes from offset to the end. NOTE: If source data does not have enough bytes, the operation will fail.`

The salient detail being "setting length to -1 will attempt to read all bytes from offset to the end"

PS: Just ignore this if I have misinterpreted

Steve,
To be honest, I missed that particular entry and went for the spec to get how to modify it on the fly. Being handy to be able to set the object correctly and manage it.

Yet, I am amazed at my inability to grasp these details when i scan over, instead of read...
Thanks.

1 Like

Where do you put the code ? in the buffer-parser

I did try using the dynamic spec but not getting the right value
I the case in the image ambient temp is 10, But not getting that result.

Can you copy the payload that goes into the buffer parser (use the copy value but not appears under your mouse when you have over the debug message)

Paste it into a reply.

Also explain which bytes refer to this temperature value.

This is the Values coming from TCP device
[67,72,78,44,49,58,65,77,66,84,69,77,80,44,49,48,48,13,10]

Looks like a string to me. Try feeding into a function.
msg.payload = msg.payload.toString();
return msg;

So here is the result when I set the buffer parser to ASCII

How would I set it so the payload is always the value part of the string ? in the case for reading ambient temp it it 10, see image

Interesting when I try a function node I get (see image)
Now I just need to set it so I get just 100 and then drop off the last zero so its 10

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.