Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active January 19, 2018 23:20
Show Gist options
  • Save ma2shita/586b28233e1e4943f4818f17da257c1f to your computer and use it in GitHub Desktop.
Save ma2shita/586b28233e1e4943f4818f17da257c1f to your computer and use it in GitHub Desktop.
Example code of read sensor data of multiple SensorTag (CC2541DK-SENSOR, CC2650STK) at same time by using node-sensortag
/*
* Example code of read sensor data of multiple SensorTag (CC2541DK-SENSOR, CC2650STK) at same time by using node-sensortag
*
* Node.js 0.10.36 or higher (4.4.2 ready)
*
* Usage;
* $ npm install sensortag async
* $ node multiple_sensortag.js
*
* e.g.)
```
$ node multiple_sensortag.js
start
5cXXXXXXXXXX discovered
5cXXXXXXXXXX ready
{"Info":{"id":"5cXXXXXXXXXX","type":"cc2540","rssi":-48},"Humidity":{"temperature":25.576584472656243,"humidity":58.1937255859375}}
{"Info":{"id":"5cXXXXXXXXXX","type":"cc2540","rssi":-47},"Humidity":{"temperature":25.579265747070316,"humidity":58.36920166015625}}
b0XXXXXXXXXX discovered
{"Info":{"id":"5cXXXXXXXXXX","type":"cc2540","rssi":-47},"Humidity":{"temperature":25.576584472656243,"humidity":58.43023681640625}}
{"Info":{"id":"5cXXXXXXXXXX","type":"cc2540","rssi":-49},"Humidity":{"temperature":25.576584472656243,"humidity":58.43023681640625}}
b0XXXXXXXXXX ready
{"Info":{"id":"5cXXXXXXXXXX","type":"cc2540","rssi":-51},"Humidity":{"temperature":25.579265747070316,"humidity":58.43023681640625}}
{"Info":{"id":"b0XXXXXXXXXX","type":"cc2650","rssi":-48},"Humidity":{"temperature":25.137939453125,"humidity":73.468017578125},"Luxometer":{"lux":32.19}}
{"Info":{"id":"5cXXXXXXXXXX"},"SimpleKey":{"left":true,"right":false}}
{"Info":{"id":"5cXXXXXXXXXX","type":"cc2540","rssi":-63},"Humidity":{"temperature":25.576584472656243,"humidity":58.43023681640625}}
{"Info":{"id":"5cXXXXXXXXXX"},"SimpleKey":{"left":false,"right":false}}
{"Info":{"id":"b0XXXXXXXXXX","type":"cc2650","rssi":-42},"Humidity":{"temperature":25.137939453125,"humidity":73.468017578125},"Luxometer":{"lux":100.44}}
^C
```
*/
var polling_interval = 1000; //ms | NOTE: Interval for polling in periodic
var SensorTag = require('sensortag');
var async = require('async');
var device_timers = {}; // NOTE: Storage for setinterval objects
var onDiscover = function(sensorTag) {
sensorTag.once('disconnect', function() {
clearInterval(device_timers[sensorTag.id]);
delete(device_timers[sensorTag.id]);
console.info(sensorTag.id, 'disconnected');
});
async.series({
connectAndSetUp: function(next) {
console.info(sensorTag.id, 'discovered');
sensorTag.connectAndSetUp(function() {
SensorTag.discover(onDiscover); // NOTE: resume for discover other devices
next();
});
},
enableSensors: function(next) {
sensorTag.enableHumidity();
sensorTag.notifySimpleKey();
try {
sensorTag.enableLuxometer();
} catch(ex) {
// NOTE: Ignored because not supported
}
console.info(sensorTag.id, 'ready');
next();
},
}, function() {
// NOTE: In case of polling in periodic
device_timers[sensorTag.id] = setInterval(function() {
async.parallel({
Info: function(next) {
var info = {id: sensorTag.id, type: sensorTag.type};
sensorTag._peripheral.updateRssi(function(error, rssi) {
info.rssi = rssi;
});
next(null, info);
},
Humidity: function(next) {
sensorTag.readHumidity(function(error, temperature, humidity) {
next(null, {temperature: temperature, humidity: humidity});
});
},
Luxometer: function(next) {
try {
sensorTag.readLuxometer(function(error, lux) {
next(null, {lux: lux});
});
} catch(ex) {
next(); // NOTE: Ignored because not supported
};
}
}, function(err, data) {
console.log(JSON.stringify(data));
});
}, polling_interval);
// NOTE: In case of listening for notification
sensorTag.on('simpleKeyChange', function(left, right) {
var data = {Info: {id: sensorTag.id}, SimpleKey: {left: left, right: right}};
console.log(JSON.stringify(data));
});
}
);
};
console.info('start');
SensorTag.discover(onDiscover);
@ma2shita
Copy link
Author

Description of code is here (in Japanese)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment