Extract values then convert hex to decimal

2,886 views
Skip to first unread message

Matthew Tati

unread,
Sep 19, 2016, 6:42:42 PM9/19/16
to Node-RED
I've got a device connected by MQTT that outputs sensor data in raw packets.
I need to extract out the values and convert them from hex to decimal do I can read them.

For example:
02010612FF590080BC4A0101A10A3F00FFFFFFFFFFFF
Where the battery voltage remaining is: 0x014A (3.3V) and the temperature is 0x0AA1 (27.21).

I've been working through this trying to use a function to split the message with the intent of recombining then converting to decimal, but I'm guessing there is a much better way.?

Dave C-J

unread,
Sep 20, 2016, 3:41:27 AM9/20/16
to node...@googlegroups.com
If they are a constant length (IE the position doesn't change) then a simple substr to split, concatenate in the correct order, then parseInt( foo, 16), to convert from hex to integer and divide by 100.

so not a much better way :-)

Matthew Tati

unread,
Sep 21, 2016, 7:13:09 AM9/21/16
to Node-RED
Yes, all constant length. I will try what you've suggested. Thanks!

Matt T

unread,
Sep 21, 2016, 8:57:56 AM9/21/16
to Node-RED
I have concatenated the data OK by doing a manual join and outputting a string but I can't seem to get the parseInt working. 

How do I do that? 

And does it matter if the strong sometimes starts with a 0? Some of the references I've read suggest that the leading 0 will mean parseInt assumes the radiux is 8 not 16?

Dave C-J

unread,
Sep 21, 2016, 9:54:08 AM9/21/16
to node...@googlegroups.com
that is why you need to specify , 16   to force it to hex...
when you say not working... what are you doing ? some examples ?

Matt T

unread,
Sep 21, 2016, 4:41:10 PM9/21/16
to Node-RED
The attachment 'flow1' shows this bit of my flow.

 

The first function on the left extracts the payload. This outputs the following:

 

02010612FF590080BC39010020064C00FFFFFFFFFFFF

 

The next function “temperature_hex_part1” extracts the first part of the payload:

 

var temper_hex_part1 = msg.payload.substr(24, 2);

msg.payload = temper_hex_part1;

return msg;

 

The one below it in the flow, “temperature_hex_part2” grabs the 2nd part of the payload I need

 

Finally, the join combines the output of these two in the correct order to produce a string. For example:

 

0620

 

This is a hex value that I need to convert that to decimal and, as you say, /100. This will be the temperature reading from my sensor in degrees Celsius, it should be:

 

15.68

 

I've got to do the same several times over to extract battery voltage and device ID.

Flow1.JPG

Dave C-J

unread,
Sep 21, 2016, 4:54:15 PM9/21/16
to node...@googlegroups.com
based on your original example I would be tempted to do it all in one function...

[{"id":"dda59d1c.fd7a1","type":"debug","z":"fb0ce1dd.3f7e4","name":"","active":true,"console":"false","complete":"true","x":465.5,"y":173,"wires":[]},{"id":"15bfeb4d.cfe0f5","type":"function","z":"fb0ce1dd.3f7e4","name":"","func":"// eg 2010612FF590080BC39010020064C00FFFFFFFFFFFF\n\nvar b1 = msg.payload.substr(18, 2);\nvar b2 = msg.payload.substr(20, 2);\nvar t1 = msg.payload.substr(24, 2);\nvar t2 = msg.payload.substr(25, 2);\nmsg.b = parseInt(b2+b1 ,16)/100;\nmsg.t = parseInt(t2+t1 ,16)/100;\n\nreturn msg;","outputs":1,"noerr":0,"x":280.5,"y":172,"wires":[["dda59d1c.fd7a1"]]},{"id":"7b822c95.7ea814","type":"inject","z":"fb0ce1dd.3f7e4","name":"","topic":"","payload":"02010612FF590080BC4A0101A10A3F00FFFFFFFFFFFF","payloadType":"str","repeat":"","crontab":"","once":false,"x":93,"y":170,"wires":[["15bfeb4d.cfe0f5"]]}]

This is assigning the parts to new msg.properties... which you can manipulate further down the flow as you wish. (Or we could re-combine them) but I don;t know what you are heading for so...

Matt T

unread,
Sep 24, 2016, 6:40:10 AM9/24/16
to Node-RED
Thanks heaps Dave.That is much cleaner and does the job very nicely!
My plan for this is to simply save the readings in a database and at the same time, output them to JSON so I can display on a simply web page where I can monitor temperature of my house; figure it's a nice project to get started with NodeRed!
Reply all
Reply to author
Forward
0 new messages