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

**URL:** <https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621>\
**Category:** Application Development\
**Created:** [April 3, 2019, 1:51pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621 "2019-04-03T13:51:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThassH](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/thassh/32/20104_2.png) [@ThassH](https://www.thethingsnetwork.org/forum/u/ThassH)\
**Post date:** [April 3, 2019, 1:51pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/1 "2019-04-03T13:51:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BoRRoZ](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/borroz/32/44234_2.png) [@BoRRoZ](https://www.thethingsnetwork.org/forum/u/BoRRoZ)\
**Post date:** [April 3, 2019, 2:21pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/2 "2019-04-03T14:21:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gpenzo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gpenzo/32/35697_2.png) [@gpenzo](https://www.thethingsnetwork.org/forum/u/gpenzo)\
**Post date:** [April 3, 2019, 4:32pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/3 "2019-04-03T16:32:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ThassH](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/thassh/32/20104_2.png) [@ThassH](https://www.thethingsnetwork.org/forum/u/ThassH)\
**Post date:** [April 3, 2019, 5:13pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/4 "2019-04-03T17:13:24Z")

</div>

![Capture](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/3/9/392c9b372d7ff8f78679ae93b1eb462bbdfba898.png)  
And why send the HEX value to TTN why not Decimal ?

---

<div class="post-metadata">

**Author:** ![gpenzo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gpenzo/32/35697_2.png) [@gpenzo](https://www.thethingsnetwork.org/forum/u/gpenzo)\
**Post date:** [April 3, 2019, 6:05pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/5 "2019-04-03T18:05:51Z")

</div>

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];

---

<div class="post-metadata">

**Author:** ![ThassH](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/thassh/32/20104_2.png) [@ThassH](https://www.thethingsnetwork.org/forum/u/ThassH)\
**Post date:** [April 3, 2019, 6:23pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/6 "2019-04-03T18:23:18Z")

</div>

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](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/5/5/5597d7f2674545d2426097286001375fdfc74262.png)

---

<div class="post-metadata">

**Author:** ![gpenzo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gpenzo/32/35697_2.png) [@gpenzo](https://www.thethingsnetwork.org/forum/u/gpenzo)\
**Post date:** [April 3, 2019, 6:46pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/7 "2019-04-03T18:46:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ThassH](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/thassh/32/20104_2.png) [@ThassH](https://www.thethingsnetwork.org/forum/u/ThassH)\
**Post date:** [April 3, 2019, 6:59pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/8 "2019-04-03T18:59:42Z")

</div>

One question please…

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

![2](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/5/5/55278e8a648f220c614d7c6e17b6d6999365a61b.png)

And get the payload format like this…

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

Thank you

---

<div class="post-metadata">

**Author:** ![UdLoRa](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/udlora/32/45330_2.png) [@UdLoRa](https://www.thethingsnetwork.org/forum/u/UdLoRa)\
**Post date:** [April 3, 2019, 7:04pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/9 "2019-04-03T19:04:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gpenzo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gpenzo/32/35697_2.png) [@gpenzo](https://www.thethingsnetwork.org/forum/u/gpenzo)\
**Post date:** [April 3, 2019, 7:05pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/10 "2019-04-03T19:05:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gpenzo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gpenzo/32/35697_2.png) [@gpenzo](https://www.thethingsnetwork.org/forum/u/gpenzo)\
**Post date:** [April 3, 2019, 7:09pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/11 "2019-04-03T19:09:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cslorabox](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cslorabox/32/35697_2.png) [@cslorabox](https://www.thethingsnetwork.org/forum/u/cslorabox)\
**Post date:** [April 3, 2019, 8:53pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/12 "2019-04-03T20:53:04Z")

</div>

> [@gpenzo](#):
>
> 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.

That would be a mistaken assumption. **LMIC\_setTxData2()** does in fact make a copy.

---

<div class="post-metadata">

**Author:** ![system](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/6/2/6250ea44fd2b0d6259489e1a92748df681e1f970.png) [@system](https://www.thethingsnetwork.org/forum/u/system)\
**Post date:** [June 2, 2019, 8:54pm UTC](https://www.thethingsnetwork.org/forum/t/how-can-i-decode-the-value-of-my-node-on-ttn/24621/13 "2019-06-02T20:54:16Z")

</div>

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