Created
February 22, 2020 15:21
-
-
Save dwblair/0cea1f9013825b58053d1844da479f06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] | |
// The function must return an object, e.g. {"temperature": 22.5} | |
function bin2String(array) { | |
return String.fromCharCode.apply(String, array); | |
} | |
function bin2HexStr(arr) | |
{ | |
var str = ""; | |
for(var i=0; i<arr.length; i++) | |
{ | |
var tmp = arr[i].toString(16); | |
if(tmp.length == 1) | |
{ | |
tmp = "0" + tmp; | |
} | |
str += tmp; | |
} | |
return str; | |
} | |
function Decode(fPort, bytes) | |
{ | |
// Decode an uplink message from a buffer | |
// (array) of bytes to an object of fields. | |
var value=bytes[0]<<8 | bytes[1]; | |
var batV=value/1000;//Battery,units:V | |
value=bytes[2]<<8 | bytes[3]; | |
if(bytes[2] & 0xFC) | |
{value |= 0xFFFF0000;} | |
var tempc=(value/10).toFixed(2);//DS18B20,PB3,units:℃ | |
var adc_ch0=(bytes[4]<<8 | bytes[5])/1000;//PA0,ADC Channel 0,units:V | |
var digital_IS=(bytes[6] & 0x02)? "H":"L";//PA12,Digital Input Status | |
var exti_trigger=(bytes[6] & 0x01)? "TRUE":"FALSE";//PB14,GPIO_MODE_IT_FALLING | |
value=bytes[7]<<8 | bytes[8]; | |
if(bytes[7] & 0xFC) | |
{value |= 0xFFFF0000;} | |
var temp_SHT=(value/10).toFixed(2);//SHT20,temperature,units:℃ | |
value=bytes[9]<<8 | bytes[10]; | |
var hum_SHT=(value/10).toFixed(1);//SHT20,Humidity,units:% | |
return { | |
BatV:batV, | |
TempC:tempc, | |
ADC_CH0V:adc_ch0, | |
Digital_IStatus:digital_IS, | |
EXTI_Trigger:exti_trigger, | |
//TempC_SHT:temp_SHT, | |
//Hum_SHT:hum_SHT | |
}; | |
//var myObj = {"DecodeDataString":"", "DecodeDataHex":""}; | |
//myObj.DecodeDataString = tostring; | |
//myObj.DecodeDataHex = toHextring; | |
//return myObj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment