I need to write a codec for a MKRWAN 1310. I am using an LHT65N as an example and am trying to understand the relationship between the received LoRaWAN frames and its codec. I am not being successful. Any clues on where to start?
Here is the codec I use for my LHT65
Bob
// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes){
var data = {
//External sensor
Ext_sensor:
{
"0":"No external sensor",
"1":"Temperature Sensor LHT65",
"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),
TempF_SHT:(((bytes[2]<<24>>16 | bytes[3])/100) * 1.8 + 32).toFixed(2),
//SHT20,Humidity,units:%
Hum_SHT:((bytes[4]<<8 | bytes[5])/10).toFixed(1),
//DS18B20,temperature,units:
//TempC_DS:
TempF_DS:
{
// "1":((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2),
"1":(((bytes[7]<<24>>16 | bytes[8])/100) * 1.8 + 32).toFixed(2),
// Change to "0" to eliminate DS probe
}[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;
}
If you want to write your own codec, you may want to see the guide below.
Generally, the uplink payload will be decoded byte by byte. And you need to read the manual to understand the bytes.
Do not panic, it is easier than you think.
Most of the commercial LoRaWAN vendor will provide you the codec in their websites…
You may find the modified codec for Dragino LHT65N below.