Skip to content

Instantly share code, notes, and snippets.

@xiaodongliang
Created November 25, 2016 16:59
Show Gist options
  • Select an option

  • Save xiaodongliang/67cad4fda231b4e190393cff8e73d33a to your computer and use it in GitHub Desktop.

Select an option

Save xiaodongliang/67cad4fda231b4e190393cff8e73d33a to your computer and use it in GitHub Desktop.
var socket = io('http://mydummp.com');
//tick / untick to nable or diable gyro sensor monitor
$('#EmmitGyroData').click(function (evt) {
if($(this)[0].checked)
{
//Monitor Device Orientation
if(window.DeviceOrientationEvent){
window.addEventListener("deviceorientation", orientation, false);
}else{
alert("DeviceOrientationEvent is not supported");
}
}
else {
//undelegate Device Orientation
window.removeEventListener("deviceorientation",orientation);
socket.disconnect();
$('#gyrobig').text('Check [Emmit Gyro Data] to Start');
}
});
//emit data by socket.
function orientation(event){
//just for demo, rephrase the data
var x = parseInt(event.alpha);
var y = parseInt(event.beta) ;
var z = parseInt(event.gamma) ;
// display the value
var thistext = x + ', '
+ y + ', '
+ z;
$('#gyrobig').text(thistext);
//avoid frequent emmit
var tol = 2;
if(Math.abs(r-lastr ) >tol|| Math.abs(g-lastg ) >tol || Math.abs(b-lastb ) >tol)
{
//element ID has not been used by this demo yet. reserve for future
var IoTJson = {elementID:'183911',GyroData:{alpha:x,beta:y,gamma:z}};
socket.emit('au_Gyro',JSON.stringify(IoTJson));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment