Convert Buffer to proper object

Hello,

i'm getting buffer in msg.payload like this. (from mqtt)

[1,3,80,0,0,3,232,0,0,5,93,0,0,0,0,5,93,42,224,26,207,37,138,107,57,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0]

i want it like this

{
    "Device_ID": "CC50E3EBD81D",
    "Time": "2022-11-28T10:58:45.425Z",
    "PrdWt": 1200,
    "nOff": 0,
    "nPChg": 2853,
    "nCChg": 0,
    "nOvrL": 0,
    "nEOff": 2853,
    "ovrTl": 22824,
    "winTl": 14266,
    "blwTl": 19971,
    "tPrdOK": 57061,
    "DPvalue": 2
}

how to convert it?

i'm newbie in node-red. please help me to get proper data. thanks!!

Do you mean that the numbers in the buffer are just the values that you wish to populate the object with, or do you think that whole object, including field names is contained in that buffer? It isn't long enough for that.

If the former, then how does the data in the buffer map onto the object?

buffer contains just values and need to populate with object.

thanks!!

12 number of data sending from IOT module to MQTT broker. like this

That doesn't seem to make sense. The buffer starts with

I don't see how that can contain the string "CC50E3EBD81D"

Similarly tProOK should be 57061, but the end of the buffer is all zeros.

if i use msg.payload = msg.payload.toString(); return msg;

getting like this.

P

How is your mqtt in node configured?

The document does not tell me how you configured your mqtt in node. I was looking to see what the output is configured to.
P.S. The data looks compressed when outputed as a string from the mqtt in node.

okay, how can i do this calculation for bytes?

PrdWt = byte 6 *256 + byte 7
nOff = byte 8 * 256 + byte

because packet constructed like that only.

it's possible in buffer-parser node?

According to the manual you linked, data starts at byte 6, and every value is (256*byte-n) + (byte-n1)

The mqtt format will be a packet of 40 hex bytes. Each key value in the json(e.g PrdWt), is constructed as 256*byte0+byte1.
The offset of the 1st value in the json (“PrdWt”) is the 6th byte offset of the byte stream. So,
PrdWt = byte 6 *256 + byte 7
nOff = byte 8 * 256 + byte 9 and so on

Essentially, the data is a byte reversed array of 16bit values with data starting at byte 6.

Following that specification, using the buffer-parser node like this

...with your buffer...

produces this...
image

I have no idea if that is correct or not (I suspect the buffer data you provided does NOT match the result in your first post)

Demo flow...

[{"id":"2df7243189d4a33a","type":"inject","z":"1b5ed5b363954ae7","name":"inject your buffer","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"CC50E3EBD81D/event","payload":"[1,3,80,0,0,3,232,0,0,5,93,0,0,0,0,5,93,42,224,26,207,37,138,107,57,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0]","payloadType":"bin","x":320,"y":820,"wires":[["86da72d0ea660d34"]]},{"id":"1fb0717a014c6dd5","type":"debug","z":"1b5ed5b363954ae7","name":"Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":880,"wires":[]},{"id":"86da72d0ea660d34","type":"buffer-parser","z":"1b5ed5b363954ae7","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint16be","name":"PrdWt","offset":6,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nOff","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nPChg","offset":10,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nCChg","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nOvrL","offset":13,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nEOff","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"ovrTl","offset":18,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"winTl","offset":20,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"blwTl","offset":22,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"tPrdOK","offset":24,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"DPvalue","offset":26,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":510,"y":820,"wires":[["f53d51a2895034b0"]]},{"id":"f53d51a2895034b0","type":"function","z":"1b5ed5b363954ae7","name":"Populate Time and Device_ID","func":"msg.payload.Device_ID = msg.topic.split('/')[0]\nmsg.payload.Time = new Date().toISOString()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":820,"wires":[["1fb0717a014c6dd5"]]}]

NOTES:

  • This demo requires you to install node-red-contrib-buffer-parser
  • You may need to UN-swap the data and use the LE (little endian) versions e.g. UINT16LE instead of UINT16BE
  • Remember, that manual COULD BE incorrect (it isn't exactly high quality!)
1 Like

Thank you so much !!.

Getting the data, i"l cross verify the values with device.

once again thank you.!! closing.

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