Aplications. Custom JavaScript Decode


#1

Hi,

I can not decode payload from lora device. I have tried same javascript code in TTN and it works.

My device is Lobaro Wmbus over LoraWan Bridge. Can anybody help me ?

Thanks!

function readVersion(bytes, i) {
    if (bytes.length < 3) {
        return null;
    }
    return "v" + bytes[i] + "." + bytes[i + 1] + "." + bytes[i + 2];
}

function Decode(fPort, bytes) {
    // Decode an uplink message from a buffer
    // (array) of bytes to an object of fields.
    var decoded = {};

    // example decoder for status packet by lobaro
    if (port === 1 && bytes.length == 7) {
        decoded.FirmwareVersion = readVersion(bytes, 0); // byte 0-2
        decoded.Vbat = (bytes[3] | bytes[4] << 8) / 1000.0; // originally in mV

        var tempRaw16 = (bytes[4] | bytes[5] << 8);
        if( (tempRaw16 & 1<<15) > 0){ // temp is negative (16bit 2's complement)
          tempRaw16 = ((~tempRaw16)& 0xffff)+1; // invert 16bits & add 1 => now positive value 
          tempRaw16=tempRaw16*-1;
        }
        decoded.Temp = tempRaw16 / 10.0; // originally in 10th degree C
        decoded.msg = "Firmware Version: " + decoded.FirmwareVersion + " Battery: " + decoded.Vbat + "V Temperature: " + decoded.Temp + "°C";
    }

    return decoded;
}

Result:

* adr:false
* applicationID:"4"
* applicationName:"app-lora"
* data:"JVWiXgs="
* devEUI:"-------"
* deviceName:"lobaro-01"
* fCnt:26232
* fPort:1
object:{} 4 keys
  * FirmwareVersion:"%U¢^"
  * Temp:0
  * Vbat:0
  * msg:"Firmware Version: v%U¢^ Battery: 0V Temperature: 0°C"

#2

did you try base64 decode function ?