From 4691c45320e8231dbe49a801a45a3a760fd51e18 Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Fri, 25 Oct 2013 13:06:29 +0100 Subject: [PATCH] Added node for Ti SensorTag BLE device --- hardware/sensorTag/79-sensorTag.html | 107 ++++++++++++++++++++ hardware/sensorTag/79-sensorTag.js | 146 +++++++++++++++++++++++++++ hardware/sensorTag/README | 5 + 3 files changed, 258 insertions(+) create mode 100644 hardware/sensorTag/79-sensorTag.html create mode 100644 hardware/sensorTag/79-sensorTag.js create mode 100644 hardware/sensorTag/README diff --git a/hardware/sensorTag/79-sensorTag.html b/hardware/sensorTag/79-sensorTag.html new file mode 100644 index 00000000..7568f954 --- /dev/null +++ b/hardware/sensorTag/79-sensorTag.html @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + diff --git a/hardware/sensorTag/79-sensorTag.js b/hardware/sensorTag/79-sensorTag.js new file mode 100644 index 00000000..92555099 --- /dev/null +++ b/hardware/sensorTag/79-sensorTag.js @@ -0,0 +1,146 @@ +/** + * Copyright 2013 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +// Sample Node-RED node file + +// Require main module +var RED = require("../../red/red"); +var SensorTag = require('sensortag'); +var stag; +var node; + +// The main node definition - most things happen in here +function sensorTagNode(n) { + // Create a RED node + RED.nodes.createNode(this,n); + this.name = n.name; + this.topic = n.topic; + this.temperature = n.temperature; + this.pressure = n.pressure; + this.humidity = n.humidity; + this.accelerometer = n.accelerometer; + this.magnetometer = n.magnetometer; + this.gyroscope = n.gyroscope; + this.keys = n.keys; + node=this; + + if ( typeof stag == "undefined") { + //console.log("starting"); + SensorTag.discover(function(sensorTag){ + stag = sensorTag; + sensorTag.connect(function(){ + //console.log("connected"); + sensorTag.discoverServicesAndCharacteristics(function(){ + sensorTag.enableIrTemperature(function(){}); + sensorTag.on('irTemperatureChange', + function(objectTemperature, ambientTemperature){ + var msg = {'topic': node.topic + '/tempature'}; + msg.payload = {'object': objectTemperature.toFixed(1), + 'ambient':ambientTemperature.toFixed(1) + }; + node.send(msg); + }); + sensorTag.enableBarometricPressure(function(){}); + sensorTag.on('barometricPressureChange', function(pressure){ + var msg = {'topic': node.topic + '/pressure'}; + msg.payload = {'pres': pressure.toFixed(1)}; + node.send(msg); + }); + sensorTag.enableHumidity(function(){}); + sensorTag.on('humidityChange', function(temp, humidity) { + var msg = {'topic': node.topic + '/humidity'}; + msg.payload = {'temp': temp.toFixed(1), + 'humidity': humidity.toFixed(1) + }; + node.send(msg); + }); + sensorTag.enableAccelerometer(function(){}); + sensorTag.on('accelerometerChange', function(x,y,z){ + var msg = {'topic': node.topic + '/accelerometer'}; + msg.payload = {'x': x, 'y': y, 'z': z}; + node.send(msg); + }); + sensorTag.enableMagnetometer(function() {}); + sensorTag.on('magnetometerChange', function(x,y,z){ + var msg = {'topic': node.topic + '/magnetometer'}; + msg.payload = {'x': x, 'y': y, 'z': z}; + node.send(msg); + }); + sensorTag.enableGyroscope(function(){}); + sensorTag.on('gyroscopeChange', function(x,y,z){ + var msg = {'topic': node.topic + '/gyroscope'}; + msg.payload = {'x': x, 'y': y, 'z': z}; + node.send(msg); + }); + sensorTag.on('simpleKeyChange', function(left, right){ + var msg = {'topic': node.topic + '/keys'}; + msg.payload = {'left': left, 'right': right}; + node.send(msg); + }); + enable(); + }); + }); + }); + } else { + //console.log("reconfig"); + enable(); + } + + +} + +function enable() { + if (node.temperature) { + stag.notifyIrTemperature(function(){}); + } else { + stag.unnotifyIrTemperature(function(){}); + } + if (node.pressure) { + stag.notifyBarometricPressure(function(){}); + } else { + stag.unnotifyBarometricPressure(function(){}); + } + if (node.humidity) { + stag.notifyHumidity(function() {}); + } else { + stag.unnotifyHumidity(function() {}); + } + if (node.accelometer){ + stag.notifyAccelerometer(function() {}); + } else { + stag.unnotifyAccelerometer(function() {}); + } + if (node.magnetometer) { + stag.notifyMagnetometer(function() {}); + } else { + stag.unnotifyMagnetometer(function() {}); + } + if (node.gyroscope) { + stag.notifyGyroscope(function() {}); + } else { + stag.unnotifyGyroscope(function() {}); + } + if (node.keys) { + stag.notifySimpleKey(function() {}); + } else { + stag.unnotifySimpleKey(function() {}); + } +} + +// Register the node by name. This must be called before overriding any of the +// Node functions. +RED.nodes.registerType("sensorTag",sensorTagNode); + diff --git a/hardware/sensorTag/README b/hardware/sensorTag/README new file mode 100644 index 00000000..b524fc69 --- /dev/null +++ b/hardware/sensorTag/README @@ -0,0 +1,5 @@ +This currently requires a slightly modified sensortag library + +To install use the following command in the Node-Red directory + +npm install sensortag@https://api.github.com/repos/hardillb/node-sensortag/tarball