-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
88 lines (78 loc) · 3.12 KB
/
example.js
File metadata and controls
88 lines (78 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
SensorTag IR Temperature sensor example
This example uses Sandeep Mistry's sensortag library for node.js to
read data from a TI sensorTag.
The sensortag library functions are all asynchronous and there is a
sequence that must be followed to connect and enable sensors.
Step 1: Connect
1) discover the tag
2) connect to and set up the tag
Step 2: Activate sensors
3) turn on the sensor you want to use (in this case, IR temp)
4) turn on notifications for the sensor
Step 3: Register listeners
5) listen for changes from the sensortag
Step 4 (optional): Configure sensor update interval
*/
var SensorTag = require('sensortag');
var log = function(text) {
if(text) {
console.log(text);
}
}
//==============================================================================
// Step 1: Connect to sensortag device.
//------------------------------------------------------------------------------
// It's address is printed on the inside of the red sleeve
// (replace the one below).
var ADDRESS = "b0:b4:48:c9:74:80";
var connected = new Promise((resolve, reject) => SensorTag.discoverByAddress(ADDRESS, (tag) => resolve(tag)))
.then((tag) => new Promise((resolve, reject) => tag.connectAndSetup(() => resolve(tag))));
//==============================================================================
// Step 2: Enable the sensors you need.
//------------------------------------------------------------------------------
// For a list of available sensors, and other functions,
// see https://github.com/sandeepmistry/node-sensortag.
// For each sensor enable it and activate notifications.
// Remember that the tag object must be returned to be able to call then on the
// sensor and register listeners.
var sensor = connected.then(function(tag) {
log("connected");
tag.enableIrTemperature(log);
tag.notifyIrTemperature(log);
tag.enableHumidity(log);
tag.notifyHumidity(log);
tag.enableGyroscope(log);
tag.notifyGyroscope(log);
return tag;
});
//==============================================================================
// Step 3: Register listeners on the sensor.
//------------------------------------------------------------------------------
// You can register multiple listeners per sensor.
//
// A simple example of an act on the humidity sensor.
var prev = 0;
sensor.then(function(tag) {
tag.on("humidityChange", function(temp, humidity){
if(prev < 35 && humidity > 35) {
log("Don't slobber all over the SensorTag please...");
}
prev = humidity;
});
});
// A simple example of an act on the irTemperature sensor.
sensor.then(function(tag) {
tag.on("irTemperatureChange", function(objectTemp, ambientTemp) {
if(objectTemp > 25) {
log("You're so hot");
}
})
});
//==============================================================================
// Step 4 (optional): Configure periods for sensor reads.
//------------------------------------------------------------------------------
// The registered listeners will be invoked with the specified interval.
sensor.then(function(tag) {
tag.setIrTemperaturePeriod(3000, log);
});