How to decode payload data?

Sorry for the newb question, but how do I decode the example payload data.

I am using the example code, LoraWan_BH1750, which is successfully sending data to TTN. The payload on TTN is: 0FE4

unsigned char *puc;
puc = (unsigned char *)(&lux);
AppDataSize = 6;//AppDataSize max value is 64
AppData[0] = puc[0];
AppData[1] = puc[1];
AppData[2] = puc[2];
AppData[3] = puc[3];
AppData[4] = (uint8_t)(BatteryVoltage>>8);
AppData[5] = (uint8_t)BatteryVoltage;

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

try this as the decoder in TTN:

function bytesToFloat(by) {
   var bits = by[3]<<24 | by[2]<<16 | by[1]<<8 | by[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 Decoder(bytes, port) {

   var decoded = {};
   i = 0;

  decoded.data = bytesToFloat(bytes.slice(i,i+=4));
  decoded.battery = ((bytes[i++] << 8) | bytes[i++]);

  return decoded;
}

Thank you for your help, it is much appreciated.

The decoder is returning:
{
“battery”: 0,
“data”: 8.18120082426758e-41
}

However, I don’t think that is correct. If I look at the terminal, I see:
unconfirmed uplink sending …
Scan for OneWire Sensors …
No OneWire Sensors found.
BatteryVoltage: 4200
unconfirmed uplink sending …

I don’t currently have any sensors, but I would expect to see battery voltage.

Thoughts?

Try this:

Your payload has 2 bytes but your sourcecode should send 6 bytes, 4 for data and 2 for battery.

There must be an issue with your sourcecode

You wrote you use the example code for the BH1750 but in your debug output i see a OneWire Scan.

What example are you using? The BH1750 example has no OneWire implementation

@Andreas that is exactly the same i wrote a post before, but mssing the parts for defining the function bytesToFloat.

yes I’am aware of that now… sorry :frowning:

Dont have go be sorry just wanted the topic author to know.
To help further the author needs to answer my questions.

Thanks for the replies, much appreciated. I am really looking forward to testing this unit out!

I don’t have any sensors connected to the CubeCell at the moment. My goal was just to get the device to transmit battery voltage once every 15 minutes. Ultimately, I want to connect an accelerometer and have movement trigger an interrupt to wake up the device. However, I thought starting with a timer might be easier.

I am using the example code that was provided when I installed the board into Arduino IDE.

thats a good info, no sensor connected.

the Payload 0FE4 is just the battery voltage.

TTN Decoder for only battery voltage is super easy:

function Decoder(bytes, port) {
    var decoded = {};
    i = 0;
    decoded.battery = ((bytes[i++] << 8) | bytes[i++]);
    return decoded;
}

you can only see what you decode. if you only transmit the battery (2 bytes) you can only deocde them.
if you try my first decoder if decodes the 2 bytes as the first two bytes for the float value.

there are more than one example code provided.
in your debug output i can see a OneWire Scan so you have not used the LoraWan_BH1750 example.

2 Likes

Perfect, that worked great! I do appreciate the explanation as well.

I must have gotten confused as to which sketch I applied. I have been going through all of them trying to get “consistent” results.

Thanks again

is there a different decoder for sending data with the MPU9250?

as explained above you need the proper decoder for what you send.
how are you sending the mpu9250 data?

if you share the source code i can have a look how to write the decoder.

I modified it to send the roll pitch and yaw. Here is the code.
Added eX, eY, and eZ as floats

  unsigned char *puc;
  
  puc = (unsigned char *)(&eX);
  appDataSize = 14;
  appData[0] = puc[0];
  appData[1] = puc[1];
  appData[2] = puc[2];
  appData[3] = puc[3];
  
  puc = (unsigned char *)(&eY);
  appData[4] = puc[0];
  appData[5] = puc[1];
  appData[6] = puc[2];
  appData[7] = puc[3];
  
  puc = (unsigned char *)(&eZ);
  appData[8] = puc[0];
  appData[9] = puc[1];
  appData[10] = puc[2];
  appData[11] = puc[3];
  
  appData[12] = (uint8_t)(batteryVoltage>>8);
  appData[13] = (uint8_t)batteryVoltage;

}

That should decode your payload if you send only eX, eY, eZ, battery:

function bytesToFloat(by) {
   var bits = by[3]<<24 | by[2]<<16 | by[1]<<8 | by[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 Decoder(bytes, port) {
  var decoded = {};
  i = 0;
  
  decoded.eX= bytesToFloat(bytes.slice(i,i+=4));
  decoded.eY= bytesToFloat(bytes.slice(i,i+=4));
  decoded.eZ= bytesToFloat(bytes.slice(i,i+=4));
  decoded.battery = ((bytes[i++] << 8) | bytes[i++]);

  return decoded;
}

I am not sure my data is going through like it is supposed to… I get “Payload input could not be validly parsed” when I try to run the payload through it. The payload looks like this ““payload”: “1qaawQ==”,
“payload_size”: 4,”

if I comment out the appDataSize = 14 it will send what I posted. If I leave the appDataSize = 14 in it will not send any data to the gateway.

ok I am trying to decode…I have made a few changes but should be the same.

  Heading = (roundf ( dH * 100.00));
  Pitch = (roundf (dP * 100.00));
  Roll = (roundf (dR * 100.00));

  unsigned char *puc;

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

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

  puc = (unsigned char *)(&Roll);
  appData[8] = puc[0];
  appData[9] = puc[1];
  appData[10] = puc[2];
  appData[11] = puc[3];

  appData[12] = (uint8_t)(batteryVoltage >> 8);
  appData[13] = (uint8_t)batteryVoltage;

And using the decoder as

function bytesToFloat(by) {
   var bits = by[3]<<24 | by[2]<<16 | by[1]<<8 | by[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 Decoder(bytes, port) {
  var decoded = {};
  i = 0;
  
  decoded.Heading= bytesToFloat(bytes.slice(i,i+=4))/100;
  decoded.Pitch= bytesToFloat(bytes.slice(i,i+=4))/100;
  decoded.Roll= bytesToFloat(bytes.slice(i,i+=4))/100;
  decoded.battery = ((bytes[i++] << 8) | bytes[i++]);

  return decoded;
}

i get

"payload": "AACcxACgucQAoLlEEHw=",
 "payload_size": 14,

as a paylod but the output is “Payload input could not be validly parsed” when I try to decode.