TTN Payload Decoding wrong?

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:

    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:

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:

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:

{
  "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

Remove the -1 and try again.

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

dg

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