How to decode payload data on Chirpstack?

I am using the example code, LoraWan_HDC1080, which is successfully sending data to Chirpstack. The payload is on base64: data:“pHDFQc3Mg0IQmQ==”

unsigned char *puc;

puc = (unsigned char *)(&temperature);
appDataSize = 10;
appData[0] = puc[0];
appData[1] = puc[1];
appData[2] = puc[2];
appData[3] = puc[3];

puc = (unsigned char *)(&humidity);
appData[4] = puc[0];
appData[5] = puc[1];
appData[6] = puc[2];
appData[7] = puc[3];

appData[8] = (uint8_t)(batteryVoltage>>8);
appData[9] = (uint8_t)batteryVoltage;

Can anyone give some advice on what kind of encoding this is or how I would decode the data?

You can refer to this decoding method. https://heltec-automation-docs.readthedocs.io/en/latest/general/decode_payload.html

Yes I saw it but for chirpstack the image is cropped so I can’t see all the code.

I don’t understand what you mean, take a screenshot.

I am following this steps:


but I think that the picture miss other part.

True, there’s something missing, but that’s not very important. The main thing is to know the setting position of the decoding. The decoded code needs to correspond to the code of the node by itself.

1 Like

go through this and you will understand it…

Jay

1 Like

Thank you I will read that.

// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// - variables contains the device variables e.g. {“calibration”: “3.5”} (both the key / value are of type string)
// The function must return an object, e.g. {“temperature”: 22.5}
//function Decode(fPort, bytes, variables) {
// return {};
//}
function bytesToFloat(bytes) {
//LSB Format (least significant byte first).
var bits = bytes[3]<<24 | bytes[2]<<16 | bytes[1]<<8 | bytes[0];
var sign = (bits>>>31 === 0) ? 1.0 : -1.0;
var e = bits>>>23 & 0xff;
var m = (e === 0) ? (bits & 0x7fffff)<<1 : (bits & 0x7fffff) | 0x800000;
var f = sign * m * Math.pow(2, e - 150);
return f;
}
function bytesToFloathum(bytes) {
//LSB Format (least significant byte first).
var bits1 = bytes[7]<<24 | bytes[6]<<16 | bytes[5]<<8 | bytes[4];
var sign1 = (bits1>>>31 === 0) ? 1.0 : -1.0;
var e1 = bits1>>>23 & 0xff;
var m1 = (e1 === 0) ? (bits1 & 0x7fffff)<<1 : (bits1 & 0x7fffff) | 0x800000;
var f1 = sign1 * m1 * Math.pow(2, e1 - 150);
return f1;
}

function Decode(fPort, bytes, variables) {
var data = {};
data.temperature = bytesToFloat(bytes.slice(0, 12));
data.temp0 = bytes[0];
data.temp1 = bytes[1];
data.temp2 = bytes[2];
data.temp3 = bytes[3];
data.humidity = bytesToFloathum(bytes.slice(0, 12));
data.hum0 = bytes[4];
data.hum1 = bytes[5];
data.hum2 = bytes[6];
data.hum3 = bytes[7];
return data;
}

the above makes no sense as you are reading the same bytes over and over…
please take a look at the example and explanation i shared here: