Hi, I have code that works in any vanilla JS compiler however when I use on TTN v3 the decoder doesn’t work, and I get an error probably due to some JS limitation in TTN?
My code:
function decodeUplink(input) {
var data = {};
// Extracting the last byte as the battery byte
var batteryHex = input.bytes[input.bytes.length - 1];
var instantFlow = floatFromHex(input.bytes.slice(6, 14));
// Battery level calculation
var battery_level = ((batteryHex - 1) / 253.0) * 100;
data.battery = battery_level.toFixed();
data.instantflow = instantFlow;
return {
data: data
};
}
// Helper function to convert bytes to floating-point values with specified endianness
function floatFromHex(hex) {
const hexToUint8 = (str) => Uint8Array.from( str.match( /.{1,2}/g ).map( (comp) => parseInt( comp, 16 ) ));
const [ A, B, C, D ] = hexToUint8( hex );
const reordered = new Uint8Array( [ C, D, A, B ] );
const view = new DataView( reordered.buffer ).getFloat32( 0 );
return view;
}
The payload is : 08041831D541E200003FC853D041FB000042A4643B000000DE00007DC02E
the problem is with instantFlow the result should be 28.274332 I can get that answer in all JS compilers but not in TTN, basically Hex string “31 D5 41 E2” gets byte flipped to “41 E2 31 D5” and as a Float - Mid-Little Endian (CDAB) should be = 1.57 any idea what needs to be changed for it to work correctly in TTN?