Skip to content

Classic API: Sensor

Gyeonghwan Hong edited this page Dec 21, 2020 · 4 revisions

Parent Document: ANT APIs

Sensor API is the set of APIs for acquiring the sensor data on the device.

You need to load sensor API module before you use its API as following.

var remoteUiApi = require(process.env.ANT_BIN_DIR + 'ant').sensor();

GetSensorlist()

Object GetSensorlist();

GetSensorlist() returns the object containing the number of sensors, and those names.

You can get the number of sensors with the member, 'sensorNum', and the names of sensors with 'sensorList'.

The example shows how to print all the sensor names. You can use those to get the sensor value with variable.

Example

var sensorApi = require(process.env.ANT_BIN_DIR + '/api/sensor-api');
var sensorList = sensorApi.GetSensorlist();

// Get # of sensors
console.log('sensor num: ' + sensorList.sensorNum);

// Print sensor list
var sensorNum = sensorList.sensorNum;
var i = 0;
for(i=0; i<sensorNum; i++){
  var sensorIndex = 'sensor' + (i+1);
  console.log('sensor' + (i+1) + ': ' +  sensorList[sensorIndex]);
}

Get()

Object Get(String sensor_name);

Get() acquires the value of the target sensor, of which name(sensor_name) is given as a parameter.

And it returns the object of sensor values, which contains the sensor values as a member.

(This is because, some sensor has several sensor values such as accelerometer)

Example

var sensorApi = require(process.env.ANT_BIN_DIR + '/api/sensor-api');
var value = sensorApi.Get("TOUCH").TOUCH;

On()

Integer On(String sensor_name, String method_type, Integer interval, Function callback(Object sensor_object));

On() is an advanced Get() with additional features: Periodic way, and Event-driven way

The method_type can be Periodic(periodic way) or Notify(event-driven way).

Literally, Periodic On() gets the sensor value of target sensor every interval period.

Event-driven On() gets the sensor value when the value changes.

The period is minimum interval detecting the change.

Example

var sensorApi = require(process.env.ANT_BIN_DIR + '/api/sensor-api');
sensorApi.On("TOUCH", "PERIODIC", 5000, function(touch) {
  console.log("Callback came!");
  console.log("Touch value is " + touch.TOUCH);
});
sensorApi.On("TOUCH", "NOTIFY", 5000, function(touch) {
  console.log("Touch value changed");
  console.log("Touch value is " + touch.TOUCH);
});