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
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