How can I decode the value of my node on TTN?

I can send the value as humudity or temperature on TTN.
But the value on TTN Dashboard is HEX.

How can I write the code on payload function to decode the HEX value by myself ?

Thank you

what did you try ? and with what data… show some screenshots too

1 Like

Show your c code how you put the temperature in the data array that you will send too ttn.
With that we can help you convery the hex data to a value.

1 Like

Capture
And why send the HEX value to TTN why not Decimal ?

1 Like

ttn show the raw data in hex if you do not use a payload format. So you application can convert the hex to decimal or use a format.
In your case your payload format must look like this

Blockquote
function Decoder(bytes, port) {
var decoded = {};
if (port === 1)
{
decoded.lightintensity = (bytes[1] << 8) + bytes[0];
}
return decoded;
}

Not sure the endianness is the same so it can be you need to swap the bytes

Blockquote
decoded.lightintensity = (bytes[0] << 8) + bytes[1];

1 Like

So ,Could you explain your format ? Example… Why did you use “bytes[1]<<8” and “+ bytes[0]” ?

It work when I use your format…

Thank you
%E0%B9%85

for example if you had send 0xABCD toward ttn
byte[0] = 0xCD
byte[1] = 0xAB
byte[1] << 8 = 0xAB00

So 0xAB00 + 0xCD = 0xABCD
0xAB00 | 0xCD = 0xABCD

Because we shift 8 position upward both are code will give the same result.

1 Like

One question please…

Why don’t use this code for easy payload format ?

2

And get the payload format like this…

function Decoder(bytes, port) {
var lux = bytes[0];
return {
lux: lux
};
}

Thank you

If a byte is sufficient, you can (you know the precision you need).
This is useful: https://www.thethingsnetwork.org/docs/devices/bytes.html

1 Like

in your code you are using unit16_t which is a 2 byte unsigned interger.
that is why we need 2 bytes. But if you sure that the value of lightMeter.readLightLevel() will fit in a byte
meaning it will never be bigger than 255 than you can use one byte.

1 Like

And your code may have a bug.
I’m not sure what LMIC_setTxData does with your pointer. But I presume it will not copy the data but keep a pointer towards your buffer.
After LMIC_setTxData is called your pointer to your buffer is invalid. The data may not be there when LMIC send the data. I’m not sure if it send the data immediate with the function call or later.

In example code of LMIC they make it a global or in your case you can make it static.

That would be a mistaken assumption. LMIC_setTxData2() does in fact make a copy.

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