Uplink Decoder "bytes" undefined

Hi,

I’m trying to implement a custom decoder for a Dragino LHT65 Temperature Sensor. They provided the code below on their website. Note: I changed the function from “Decoder” to “decodeUplink” as this seems to be the recent nomenclature.

function decodeUplink(bytes, fPort){
var data = {  
       //External sensor
       Ext_sensor:
       {
         "0":"No external sensor",
         "1":"Temperature Sensor",
         "4":"Interrupt Sensor send",
         "5":"Illumination Sensor",
         "6":"ADC Sensor",
         "7":"Interrupt Sensor count",
       }[bytes[6]&0x7F],
       
       //Battery,units:V
       BatV:((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000,
       
       //SHT20,temperature,units:
       TempC_SHT:((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2),
       
       //SHT20,Humidity,units:%
       Hum_SHT:((bytes[4]<<8 | bytes[5])/10).toFixed(1),
       
       //DS18B20,temperature,units:
       TempC_DS:
       {
         "1":((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2),
       }[bytes[6]&0xFF],       
       
       //Exti pin level,PA4
       Exti_pin_level:
       {
         "4":bytes[7] ? "High":"Low",
       }[bytes[6]&0x7F], 
       
       //Exit pin status,PA4
       Exti_status:
       {
         "4":bytes[8] ? "True":"False",
       }[bytes[6]&0x7F],    
       
       //BH1750,illumination,units:lux
       ILL_lux:
       {
         "5":bytes[7]<<8 | bytes[8],
       }[bytes[6]&0x7F],  

        //ADC,PA4,units:V
        ADC_V:
       {
         "6":(bytes[7]<<8 | bytes[8])/1000,
       }[bytes[6]&0x7F],  
       
        //Exti count,PA4,units:times
        Exit_count:
        {
          "7":bytes[7]<<8 | bytes[8],
        }[bytes[6]&0x7F],  
        
        //Applicable to working mode 4567,and working mode 467 requires short circuit PA9 and PA10
        No_connect:
        {
          "1":"Sensor no connection",
        }[(bytes[6]&0x80)>>7],  
  };
	return data;
}

However, this decoder produces the following error:

"Exception generated by quickjs: ‘bytes’ is not defined at decodeUplink (eval_script:13) at (eval_script:69) "

As per the documentation, “bytes” is being passed directly to the decoder from Chirpstack, so I don’t know why it would be undefined.

Not just that, but you can simply use this while keeping your original function:

// v3 to v4 compatibility wrapper
function decodeUplink(input) {
        return { 
            data: Decode(input.fPort, input.bytes, input.variables)
        };   
}
2 Likes

Thanks for the quick reply!

This gives me the error:
"Exception generated by quickjs: ‘Decode’ is not defined at decodeUplink (eval_script:4) at (eval_script:8) "

Did you rename your original function back to Decode?

When I do that it throws no error, but all values are set to 0 or null in the output.

image

This the complete code:

function decodeUplink(input) {
        return { 
            data: Decode(input.fPort, input.bytes, input.variables)
        };   
}

function Decode(bytes, fPort) {
//Payload Formats of LHT65 Deveice
  return {
    
       //External sensor
       Ext_sensor:
       {
         "0":"No external sensor",
         "1":"Temperature Sensor",
         "4":"Interrupt Sensor send",
         "5":"Illumination Sensor",
         "6":"ADC Sensor",
         "7":"Interrupt Sensor count",
       }[bytes[6]&0x7F],
       
       //Battery,units:V
       BatV:((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000,
       
       //SHT20,temperature,units:℃
       TempC_SHT:((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2),
       
       //SHT20,Humidity,units:%
       Hum_SHT:((bytes[4]<<8 | bytes[5])/10).toFixed(1),
       
       //DS18B20,temperature,units:℃
       TempC_DS:
       {
         "1":((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2),
       }[bytes[6]&0xFF],       
       
       //Exti pin level,PA4
       Exti_pin_level:
       {
         "4":bytes[7] ? "High":"Low",
       }[bytes[6]&0x7F], 
       
       //Exit pin status,PA4
       Exti_status:
       {
         "4":bytes[8] ? "True":"False",
       }[bytes[6]&0x7F],    
       
       //BH1750,illumination,units:lux
       ILL_lx:
       {
         "5":bytes[7]<<8 | bytes[8],
       }[bytes[6]&0x7F],  

        //ADC,PA4,units:V
        ADC_V:
       {
         "6":(bytes[7]<<8 | bytes[8])/1000,
       }[bytes[6]&0x7F],  
       
        //Exti count,PA4,units:times
        Exit_count:
        {
          "7":bytes[7]<<8 | bytes[8],
        }[bytes[6]&0x7F],  
        
        //Applicable to working mode 4567,and working mode 467 requires short circuit PA9 and PA10
        No_connect:
        {
          "1":"Sensor no connection",
        }[(bytes[6]&0x80)>>7],  
  };
}

Lets change this: function Decode(bytes, fPort)
to this: function Decode(fPort, bytes, variables)

1 Like

That did it. Do you know why variables is a necessary parameter? There are none defined for the device.

In any case, thanks so much!

Not necessary, just another option you could use as the function Decode(fPort, bytes, variables) should be latest default v3 format. Seems you don’t even use the fPort, so in your case you could also do this:

function decodeUplink(input) {
        return { 
            data: Decode(input.bytes)
        };   
}

function Decode(bytes) {
//Payload Formats of LHT65 Deveice
  return {
    // bytes decoding here ...
  };
}
1 Like

I must’ve changed something else simultaneously then. This works perfectly now, thank you.

Nice wrapper. Thanks a lot.

You’re welcome, but I’ve just copied it from chirpstack-v3-to-v4 tool’s output :slight_smile:

1 Like

Nice to know.

I was asking myself, why my Adeunis Field Tester has stopped working with the same decoder on CS4…