Hello again @aargollo
I am guessing english is not your 1st language & that may be the reason for some confusion here.
At first you say "working to get temperature from a smart thermometer"
Then you say "But my buffer sent it in wrong position, as you can see in the log below"
It is my guess that you need to read values from a Buffer object that arrives in msg.payload
And only now you post a relevant piece of the puzzle...
it is my suspicion, the protocol is fully documented & its where where you got that piece
of info is from.
OK, so I will invent/guess the 1st 28 bytes of your reply and I will attempt to show you something that SHOULD get you moving towards a solution.
IMPORTANT
Many assumptions have been made! As I don't have the full protocol specification, many assumptions have been made. The below code is an attempt to help you help yourself...
I strongly recommend you understand what all the other values mean. For example, I am certain byte[0] (0x1) is SOH and the last byte (0x3) is ETX REF. Many of the other BYTEs and INTs in the Buffer will be relevant but since I dont have that information, I will work backwards from the last byte as your examples indicate the temperature is in the last 2 bytes (minus the ETX)
Playtime (guesswork)...
//fake the msg.payload (simulate serial reply)
var msg = {};
/* 0 1 2 3 4 5 6 7 8 9 */
var serialData = [ 0x01, 0x81, 0x02, 0x12, 0x02, 0x10, 0x02, 0x1f, 0xde, 0x92,
0xd5, 0x72, 0x02, 0x1a, 0x02, 0x14, 0x02, 0x12, 0x02, 0x10,
0x02, 0x10, 0x02, 0x10, 0x29, 0x02, 0x10, 0x02, 0x12, 0x02,
0x1b, 0x72, 0x39, 0x3]
msg.payload = new Buffer.from(serialData);
//Loop through and decode any encoded values...
var decodeNext = false;
var decodedData = [];
for (var i = 0; i < msg.payload.length; i++) {
//get this byte
let b = msg.payload[i];
//if last was 0x2, deduct 0x10 and store it
if (decodeNext && b >= 0x10) {
decodedData.push(b - 0x10);
decodeNext = false;//reset flag
continue;//next loop
}
if (b === 0x2) {
decodeNext = true;//set flag & continue (dont store this one)
continue;//next loop
}
//if we reach here - simply store it
decodedData.push(b);
}
console.log(decodedData);
//make a buffer from new decoded data (not necessary but the read functions work nicely)
var decodedBuffer = new Buffer.from(decodedData);
//decode the status record
var statusRecord = {};
var lastByte = decodedBuffer.length - 1;
statusRecord.attribute = decodedBuffer.readInt16BE(lastByte - 7);
statusRecord.success = decodedBuffer.readInt8(lastByte - 5);
statusRecord.dataType = decodedBuffer.readInt8(lastByte - 4);
statusRecord.measuredValue = decodedBuffer.readInt16BE(lastByte - 3);
//todo: Check the success flag is 0
//todo: Check dataType before calling the next buffer.readXXXX
// e.g if dataType is byte then call readInt8() to get measuredValue
if (statusRecord.success === 0) {
console.log(`Read was successful :)`);
console.log(statusRecord);
console.log(`Measured Value: ${statusRecord.measuredValue / 100.0} [°C]`);
} else {
console.log(`Read was unsuccessful! :(`);
}
output...
[1, 129, 2, 0, 15, 222, 146, 213, 114, 10, 4, 2, 0, 0, 0, 41, 0, 2, 11, 114, 57, 3]
Read was successful :)
{attribute: 41, success: 0, dataType: 2, measuredValue: 2930}
Measured Value: 29.3 [°C]