# TTN Payload Decoding wrong?

**URL:** <https://www.thethingsnetwork.org/forum/t/ttn-payload-decoding-wrong/55313>\
**Category:** TTN Network and Services\
**Created:** [February 18, 2022, 1:06pm UTC](https://www.thethingsnetwork.org/forum/t/ttn-payload-decoding-wrong/55313 "2022-02-18T13:06:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidgs](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/davidgs/32/46693_2.png) [@davidgs](https://www.thethingsnetwork.org/forum/u/davidgs)\
**Post date:** [February 18, 2022, 1:06pm UTC](https://www.thethingsnetwork.org/forum/t/ttn-payload-decoding-wrong/55313/1 "2022-02-18T13:06:36Z")

</div>

I am having trouble with a packet decoder in TTN right now that I cannot figure out. I’m sure I’m doing something wrong, but any help would be appreciated. My data is put into a 16-byte message on a LoRaWAN Arduino device. They are all 16-bit `uint16_t` except for one `uint32_t` and I encode them like so:

```auto
    dataSender[0] = ft >> 8;
    dataSender[1] = ft & 0xFF;
    dataSender[2] = fh >> 8;
    dataSender[3] = fh & 0xFF;
    dataSender[4] = co & 0xFF;
    dataSender[5] = (co >> 8) & 0xFF;
    dataSender[6] = (co >> 16) & 0xFF;
    dataSender[7] = (co >> 24) & 0xFF;
    dataSender[8] = pm10 >> 8;
    dataSender[9] = pm10 & 0xFF;
    dataSender[10] = pm25 >> 8;
    dataSender[11] = pm25 & 0xFF;
    dataSender[12] = pm100 >> 8;
    dataSender[13] = pm100 & 0xFF;
    dataSender[14] = batt >> 8;
    dataSender[15] = batt & 0xFF;

```

I then have a decoder on TTN that is:

```auto
function Decoder(bytes, port) {
  var decoded = {};

  decoded.temp_c = ((bytes[0] << 8) | bytes[1]) / 100; // temperature ºC
  decoded.humidity = ((bytes[2] << 8) | bytes[3]) / 100; // humidity %
  decoded.co2 = ((bytes[4]) + // co2 is a 4-byte value
  ((bytes[5]) << 8)
    + ((bytes[6]) << 16)
    + ((bytes[7]) << 24) );
  decoded.pm10 = ((bytes[8] << 8) | bytes[9]);
  decoded.pm25 = (bytes[10] << 8) | bytes[11];
  decoded.pm100 = (bytes[12] << 8) | bytes[13];
  decoded.batt = (bytes[14] << 8) | bytes[15];
  return decoded;
}

```

Which _should_ work, right? It doesn’t. Everything works ok except that the `decoded.batt` is incorrect.

I even went to far as to add this debug to the Arduino:

```auto
SerialUSB.print("Decoded temp: ");
    SerialUSB.println(((dataSender[0] << 8) | dataSender[1]) / 100);
    SerialUSB.print("Decoded hum: ");
    SerialUSB.println(((dataSender[2] << 8) | dataSender[3]) / 100);
    SerialUSB.print("Decoded CO2: ");
    SerialUSB.println((dataSender[4]) + ((dataSender[5]) << 8) + ((dataSender[6]) << 16) + ((dataSender[7]) << 24) );
    SerialUSB.print("Decoded pm10: ");
    SerialUSB.println(((dataSender[8] << 8) | dataSender[9]));
    SerialUSB.print("Decoded pm25: ");
    SerialUSB.println(((dataSender[10] << 8) | dataSender[11]));
    SerialUSB.print("Decoded pm100: ");
    SerialUSB.println(((dataSender[12] << 8) | dataSender[13]));
    SerialUSB.print("Decoded Batt: ");
    SerialUSB.println(((dataSender[14] << 8) | dataSender[15]));
    // Prepare upstream data transmission at the next possible time.
    LMIC_setTxData2(1, dataSender, sizeof(dataSender) - 1, 0);

```

And lo and behold that prints all the correct values.

But when I copy the payload `0A1F0E050107000000030003000302` out and enter it into the Uplink Payload Test I get:

```auto
{
  "batt": 512,
  "co2": 1793,
  "humidity": 35.89,
  "pm10": 3,
  "pm100": 3,
  "pm25": 3,
  "temp_c": 25.91
}

```

But `batt` is actually 623 on the uplink side. In fact, no matter what the payload is, the `batt` value is _always_ 512.

So, please, point out my bone-headed move here please?

Thanks!  
dg

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [February 18, 2022, 1:32pm UTC](https://www.thethingsnetwork.org/forum/t/ttn-payload-decoding-wrong/55313/2 "2022-02-18T13:32:07Z")

</div>

> [@davidgs](#):
>
> `sizeof(dataSender) - 1`

Remove the -1 and try again.

---

<div class="post-metadata">

**Author:** ![davidgs](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/davidgs/32/46693_2.png) [@davidgs](https://www.thethingsnetwork.org/forum/u/davidgs)\
**Post date:** [February 18, 2022, 3:07pm UTC](https://www.thethingsnetwork.org/forum/t/ttn-payload-decoding-wrong/55313/3 "2022-02-18T15:07:44Z")

</div>

I _knew_ it was just me being dumb somewhere! Thanks, it’s all set now!

dg

---

<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:** [February 28, 2022, 10:17pm UTC](https://www.thethingsnetwork.org/forum/t/ttn-payload-decoding-wrong/55313/4 "2022-02-28T22:17:45Z")

</div>

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