Hi all,
I am discovering TTN and IoT with sensors. At the moment I have my own sensor working and it sends my data (payload) well to the TTN console.
This sensor measures the position based on the magnetometer and returns the X, Y and Z coordinates.
Now I want to decipher my payload to “real” values. I do manage to see values of the Accelorometer and read the values of the compass.
But now I have to convert these values of the compass (X, Y, Z) to “something”. On the internet I have found that it is possible with “Math.atan2”. Only I try this I get this message:
Error ("json: unsupported value: NaN")
What I’ve done so far:
function Decoder(bytes, port) {
var accel_x = (bytes[16] << 8) * 2/32768 ;
var accel_y = (bytes[17] << 8) * 2/32768 ;
var accel_z = (bytes[18] << 8) * 2/32768 ;
var compass_x = (bytes[19]) | bytes[20] ;
var compass_y = (bytes[21]) | bytes[22] ;
var compass_z = (bytes[23]) | bytes[24] ;
var heading = Math.atan2(compass_y, compass_x) * 180 / Math.Pi;
return {
accel_x:accel_x
accel_y:accel_y
accel_z:accel_z
compass_x:compass_x
compass_y:compass_y
compass_z:compass_z
heading:heading
}
}
But when I return this “heading”, I get that error. The vendor of the sensor gave me this PHP example for decoding the compass.
$compassX = convertToReal(hexdec(substr($payload,38,4)));
$compassY = convertToReal(hexdec(substr($payload,42,4)));
$compassZ = convertToReal(hexdec(substr($payload,46,4)));
But after having these values in my code, I’ve to do more with this to decode these numbers to a “real” value.
Found this and this article and much more about converting the three magnetometer axis.
Does anyone have experience deciphering a magnetometer? Or someone who can push me in the right direction.