Help to decode the payload-parseInt error

Hi,
Im trying to decode an Object Locator Payload, but i having issues to decode it in Lora App Server.
I already decode it in Node-Red, but i have this error when i put the code in Lora-App-Server.


I dont know why dont recognize the parseInt().Here, the code im using.

// The function must return an object, e.g. {“temperature”: 22.5}
function base64tohex(bytes){
return bytes.map(function(byte) {
return (“00” + (byte & 0xFF).toString(16)).slice(-2)
}).join(‘’)
}
function estadisticas(stat){
var hexstat=stat;
var estadist=“00000000”+(parseInt(hexstat,16).toString(2)).substr(-8);
var i=(estadist.length)-4;
if(estadist[i]===“1”){
return 0;
}else{
return 1;
}
}
function conversorbateria(bateria){
var hexbat=bateria;
var bat=(parseInt(hexbat,16).toString(2));//valor en binario del campo bateria
var volbat=0;
var porcentbat=0;
var v=“”;
var m=0;
var k=“”;
while((bat.length)<8){
bat=“0”+bat;
}
for(i=0;i<=bat.length;i++){
if(m<=3){//los primeros 3 bits son para el auxiliar del voltaje de la bateria
k=k+(bat[i]);
}
if((m>3)&&(m<8)){//los siguientes bits son para el auxiliar del porcentaje de bateria
v=v+(bat[i]);
}
m=m+1;
}
v=Number.parseInt(v,2);
k=Number.parseInt(k,2);
voltbat=(v+25)/10;//voltaje de bateria
porcentbat=100*(k/15);//porcentaje de bateria
var arraybat=[voltbat,porcentbat];
return arraybat;
}
function temperatura(temperature){
var hextemp=temperature;
var tempbin=(parseInt(hextemp,16).toString(2));//valor en binario de la temperatura
var temp=0;
var t=“”;
while((tempbin.length)<8){//longitud de la cadena debe ser 8
tempbin=“0”+tempbin;
}
for(i=1;i<tempbin.length;i++){//los primeros 7 bits son para el auxiliar de la temperatura
t=t+(tempbin[i]);
}
temp=Number.parseInt(t,2);
temp=(temp-32);//temperatura en Celsius
return temp;
}
function conversorcoordenadas(latitude,longitude){
function invertir(valor){
var i=0;
var j=0;
var val1=6;
var val2=8;
var correcto=“”;
for(i=0;i<4;i++){//intercambia lsb y msb
correcto=correcto+valor.substring(val1,val2);
val1=val1-2;
val2=val2-2;
}
return correcto;
}
function convertbinario(hexvalue){
var binario=“”;
binario=(parseInt(hexvalue,16).toString(2));
while((binario.length)<32){//longitud de la cadena debe ser 32
binario=“0”+binario;
}
return binario;
}
function divisor(latwsg){
var aux;
aux=latwsg/1000000;
return aux;
}
function intercambiador(valorbin){
var cont=0;
var entero;
var signo;
var newcadena=“”;
if(valorbin.charAt(0)===“1”){//si el primer valor es 1 se hace el intercambio de ceros y unos
signo=-1;
for(i=((valorbin.length)-1);i>=0;i–){//halla la posicion del primer uno
newcadena=valorbin[i]+newcadena;
if(valorbin[i]===“1”){
cont=i;
break;
}
}
for(i=(cont-1);i>=0;i–){
if(valorbin.charAt(i)===“0”){
newcadena=“1”+newcadena;
}
if(valorbin.charAt(i)===“1”){
newcadena=“0”+newcadena;
}
}
}
if(valorbin[0]===“0”){
signo=1;
newcadena=valorbin;
}
entero=signo*Number.parseInt(newcadena,2);//la cadena se pasa a entero
return entero;
}
var lendianlat=latitude;
var lendianlong=longitude;
var hexlat=“”;
var hexlong=“”;
var lat;
var longi;
var aux=“”;
var cont=0;
var i=0;
hexlong=invertir(lendianlong);
hexlat=invertir(lendianlat);
latbin=convertbinario(hexlat);
longbin=convertbinario(hexlong);
lat=latbin.substring(4,32);//extrae el resto de los valores que son la latitud
aux=longbin.substring(0,3);
longi=longbin.substring(3,32);
lat=intercambiador(lat);//se envia la cadena binaria y se recibe entero
longi=intercambiador(long);
lat=divisor(lat);
longi=divisor(longi);
exactitud=Number.parseInt(aux,2);
exactitud=Math.pow(2,(exactitud+2));
var arraycoordenadas=[lat,longi.exactitud];
return arraycoordenadas;
}
function Decode(fPort, bytes) {
hexvalue=base64tohex(bytes);//retorna una cadena hexadecimal
var i=0;
var lat=“”;
var longi=“”;
var stat=“”;
var temp=“”;
var bat=“”;
var carga=hexvalue;
for (i=0;i<carga.length;i++){
if((i===0)||(i===1)){
stat=stat+carga[i];
}
if((i===2)||(i===3)){
bat=bat+carga[i];
}
if((i===4)||(i===5)){
temp=temp+carga[i];
}
if((i>5)&&(i<=13)){
lat=lat+carga[i];
}
if((i>13)){
longi=longi+carga[i];
}
}
var parametros=new Object();
parametros.gnssfix=estadisticas(stat);
var auxbat=conversorbateria(bat);
parametros.voltbat=auxbat[0];
parametros.porcentbat=auxbat[1];
parametros.temp=temperatura(temp);
var auxcoordenadas=conversorlongitud(lat,longi);
parametros.lat=auxcoordenadas[0];
parametros.longi=auxcoordenadas[1];
parametros.exact=auxcoordenadas[2];
return parametros;
}

The device is a Tabs Object Locator, as you can see, i use several times the function parseInt, Can someone help me with this?
Thanks for your help

how you encode your data in your sensor befor send it ?

Hi,
I dont encode the data, the node is made by Tabs company.
I found a PDF, here is explained how the data is encoded.
image .
I made the data decoder by myself, it works in node-red but not in Lora-App-Server.
I hope this help.
Thanks

Depending if your bytes are saved on little or big endian, you have 11 bytes to decode, and the first byte is status so you must code something like that :

 function Decode(fPort, bytes) {
   var status    = bytes[0]   ;
   var battery       = bytes[1]   ;
   var temp      = bytes[2]    ;
   var lat  = bytes[3]  << 24 | bytes[4] << 32 | bytes[5]  << 40 | bytes[6]  ;
  .
  .
  .
   return {
              "status": status ,
              "battery": battery ,
              "tension_bat": tension_bat ,
              "temp": temp ,
              "lat": lat ,
              ...
};

}

It’s really important to know how your node encode data, and how it’s send it ? look on the code of your node.

1 Like

Thanks for your help jawad_didouh
I will do that.
Best Regards

Hi,
have you succeeded in decoding? I have some TABS as well (Temperature and Motion sensor).
I have problems to decode it and to have it correctly joined the lorawan. TBOL has joined correctly but the temperature and motion sensor do not activate although join accept has been sent.
I am really interested in your experience!
Yours A

Hello Andreas
Sorry for the late reply,
Yes, already decode the payload of the TABS Object Locator and the home and health sensor.
Now, for your problem, if the Application Key or DevEUI is not set correctly in the Loraserver, you will not receive the Join Accept message. But if you receive the Join Accept, but not the uplink from you node, then maybe your Loraserver may not be configured correctly.
I also had the problem of Join Accep Loop and Join Request and I solved it by configuring the Loraserver uplink channels, Im actually using the ISM Band AU915 and after enabled the channels from 8 to 15, and restart the Node, i start to receive the Uplinks.
I hope it works for you.
Best Regards

Hi,
i managed as well. It works fine…
Thanks.
Andreas.

here is the decoding we use for the TBOL in the chirpstack server

 function Decode(fPort, byte) {
	function sliceBinary(a,b,c){var d="";if(c<b)for(i=b;i>c-1;i--)d+=a[i];else for(i=b;i<c+1;i++)d+=a[i];return d} /// slice (1001,3,2) of binary out 100
	function reverseString(a){return a.split("").reverse().join("");}  /// string in 'abcd' out 'dcba'
	function dec2bin(n){return(n>>>0).toString(2);}
	function padStart(s){var l=s.length;if (l<8){var t='00000000';s=t.slice(0,8-l)+s}return s;}
    function flipbits(a){for(var b="",c=0;c<a.length;c++)b+="0"==a.charAt(c)?"1":"0";return b}
  	r={};
  	r["RAWDATA"]=byte;
    r["RAWDATA_STRING"]=byte.join(',');
    bytearray=[];
  	for(i=0;i<byte.length;i++){var tmp=dec2bin(byte[i]);bytearray[i]=padStart(tmp)}
  	r["RAWDATA_BINARIES"]=bytearray.join(' ');
    //----------------
    t1=reverseString(bytearray[0]);
	t11=sliceBinary(t1,0,0);t11=parseInt(t11,2);
	t12=sliceBinary(t1,3,3);t12=parseInt(t12,2);
    if (t12==1) {t12='GNNS NOT OK';} else {t12='GNNS OK';}
    r["status"]={}
    r["status"]["GNNS"]=t12;
  	r["status"]["button"]=t11;
    //----------------
    t1=reverseString(bytearray[1]);
	t11=sliceBinary(t1,3,0);t11=parseInt(t11,2);t11=(t11+25)/10;
	t12=sliceBinary(t1,7,4);t12=parseInt(t12,2);t12=(t12/15)*100;
    r["battery"]={};
    r["battery"]["voltage"]=t11;
    r["battery"]["capacity"]=t12;
    //----------------
    t1=reverseString(bytearray[2]);
	t11=sliceBinary(t1,6,0);t11=parseInt(t11,2);t11=t11-32;
  	r["temperature"]={}
    r["temperature"]["chip"]=t11;
    //----------------
    r["position"]={}
  	t1=reverseString(bytearray[6]+bytearray[5]+bytearray[4]+bytearray[3]);
	t11=sliceBinary(t1,27,0);
  	t11=~~parseInt(t11,2);
    r["position"]["latitude"]=t11/1000000;
  	//----------------
  	t1=reverseString(bytearray[10]+bytearray[9]+bytearray[8]+bytearray[7]);
	t11=sliceBinary(t1,27,0);
  	t11=~~parseInt(t11,2);
    r["position"]["longitude"]=t11/1000000;
  	//----------------
  	t11=sliceBinary(t1,31,29);
  	t11=~~parseInt(t11,2);
  	r["position"]["accuracykategory"]=t11;
    r["position"]["accuracy"]=Math.pow(2,(t11+2));
  	return r;
}
1 Like