Javascript programming in TheThingsNetwork


Joined
May 11, 2022
Messages
1
Reaction score
0
Hi, I am currently doing an internship in a University.

We have to collect data such as voltage, average, power and energy using an SDM-120M and transmit this to a server using a LoraWan converter.

This sends all the data to a temporary server, the thing is, that the data is quite difficult to read, it resembles to this : 01436100003E26E97941DE7AE23DB43958
Where :

01 = slave address
43610000 = voltage
3E26E979 = amperage
41DE7AE2 = power
3DB43958 = energy

The string I showed is coded in IEEE-754 (link to a converter: https://www.h-schmidt.net/FloatConverter/IEEE754.html).
What we have to do is to come up with some code that would read the string, then concatenate It to spare all the variables and then display it in a quite neat way for example : {Payload : Voltage = XX, Amperage = XX, Power = XX, Energy = XX }. All this is coded in Javascript in a website named : https://www.thethingsnetwork.org

It is coded in the payload formatters section.

With my teammate, we came to a code looking like shown below :


function Decoder(bytes, port) {
var myVal = {};
var decode = {};
{
decode.Tension=(bytes[1] = (myVal & 0xFF00) >> 8);
decode.Amperage= (bytes[2]<<8 | bytes[4]);
decode.Power=(bytes[2]<<8 | bytes[6]);
decode.Energy=(bytes[2]<<8 | bytes[4]);

}
const Float32ToHex = (decode) => {
const getHex = i => ('00' + i.toString(16)).slice(-2);
var view = new DataView(new ArrayBuffer(4))
view.setFloat32(0, decode);
return Array.apply(null, { length: 4 }).map((_, i) => getHex(view.getUint8(i))).join('');
}

const Float32ToBin = (decode) => {
const HexToBin = hex => (parseInt(hex, 16).toString(2)).padStart(32, '0');
const getHex = i => ('00' + i.toString(16)).slice(-2);
var view = new DataView(new ArrayBuffer(4))
view.setFloat32(0, decode);
return HexToBin(Array.apply(null, { length: 4 }).map((_, i) => getHex(view.getUint8(i))).join(''));
}

const HexToFloat32 = (str) => {
var int = parseInt(str, 16);
if (int > 0 || int < 0) {
var sign = (int >>> 31) ? -1 : 1;
var exp = (int >>> 23 & 0xff) - 127;
var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
var decode = 0
for (i = 0; i < mantissa.length; i += 1) { decode += parseInt(mantissa) ? Math.pow(2, exp) : 0; exp-- }
return decode * sign;
} else return 0
}

const BinToFloat32 = (str) => {
var int = parseInt(str, 2);
if (int > 0 || int < 0) {
var sign = (int >>> 31) ? -1 : 1;
var exp = (int >>> 23 & 0xff) - 127;
var mantissa = ((int & 0x7fffff) + 0x800000).toString(2);
var decode = 0
for (i = 0; i < mantissa.length; i += 1) { decode += parseInt(mantissa) ? Math.pow(2, exp) : 0; exp-- }
return decode* sign;
} else return 0
}
{
return decode;
}
}


Thanks for your help :)
 
Ad

Advertisements

Joined
Mar 11, 2022
Messages
63
Reaction score
8
Sorry, have'nt read the whole thing. And i have no idea about this converter.

Summon:
For any reason this is returned as a string. So you have to treat it like a string. substring should be your choice.

This should work
Code:
function chunker(t){
    var obj={};
    obj.slaveAdress=t.substr(0,2);
    obj.voltage=t.substring(2,10);
    obj.amperage=t.substring(10,18);
    obj.power=t.substring(18,26);
    obj.energy=t.substring(26,44);
    return obj;  
}
console.log(chunker('01436100003E26E97941DE7AE23DB43958'));

Now you can try to use this converter tool to deconvert each chunck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top