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