Decoding Sensor Data

Share

Every sensor has it’s own kind off codification
The message from the device is like this one:

{"time": "1551285775",
"deviceType": "deviceType",
"device": "xyzabcd",
"duplicate": "false",
"snr": "30.00",
"rssi": "-128.00",
"avgSnr": "19.39",
"station": "300C",
"lat": "-20.0",
"lng": "-44.0",
"seqNumber": "497",
"data": "1a24183c3801" }

The measurement are encoded on data string, a sequence of 6 bytes.
I read the sensor manual and the temperature are usin the last 4 bytes

To get the temperature value i wrote the following:

var dados = msg.payload.data //get the data value “1a24183c3801”
var v1 = dados.substring(10,11)+dados.substring(11,12)+dados.substring(8,9)+dados.substring(9,10) //get the 4 values as in the device manual
For example the value will be “0138”
The temperature will be temp = parseInt(v1,16)/100
Using the value 0138 the result is 3,12 Degrees Celsius
But if the temperatue is negative you need to test if the first bit of the meesage is 1 and use

temp = parseInt((~parseInt(v1,16) +1 >>> 0).toString(2).substring(16,32),2)/100 * -1

The formula code on the line above use twos complement format, get the last 16 bits and then convert to a decimal value