Skip to content

Instantly share code, notes, and snippets.

@hardillb
Last active December 26, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hardillb/7126753 to your computer and use it in GitHub Desktop.
Save hardillb/7126753 to your computer and use it in GitHub Desktop.
Ti SensorTag Node-red node, it depends on the sensortag and noble nodes run "npm install sensortag@https://api.github.com/repos/hardillb/node-sensortag/tarball'' in the node red directory Put these 2 files in nodes/hardware directory. You will need to apt-get libbluetooth-dev to get noble to build during the npm install
<!--
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.
-->
<!-- First, the content of the edit dialog is defined. -->
<script type="text/x-red" data-template-name="sensorTag">
<!-- data-template-name identifies the node type this is for -->
<!-- Each of the following divs creates a field in the edit dialog. -->
<!-- Generally, there should be an input for each property of the node. -->
<!-- The for and id attributes identify the corresponding property -->
<!-- (with the 'node-input-' prefix). -->
<!-- The available icon classes are defined in Twitter Bootstrap -->
<!-- By convention, most nodes have a 'name' property. The following div -->
<!-- provides the necessary field. -->
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="Topic">
</div>
</script>
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="sensorTag">
<!-- data-help-name identifies the node type this help is for -->
<!-- This content appears in the Info sidebar when a node is selected -->
<!-- The first <p> is used as the pop-up tool tip when hovering over a -->
<!-- node in the palette. -->
<p>Simple outputput node to drive digispark RGB</p>
<p>Requires msg.payload to be of the form 'r,g,b'</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<!-- The example below shows a small subset of the properties that can be set-->
<script type="text/javascript">
RED.nodes.registerType('sensorTag',{
category: 'input', // the palette category
color:"GoldenRod",
defaults: { // defines the editable properties of the node
name: {value:"sensorTag"}, // along with default values.
topic: {value:"sensorTag"}
},
inputs:0, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
icon: "bluetooth.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name||this.topic||"sample";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
}
});
</script>
/**
* 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('node-sensortag');
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;
node=this;
SensorTag.discover(function(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);
});
sensorTag.notifyBarometricPressure(function(){});
sensorTag.notifyIrTemperature(function(){});
sensorTag.notifyHumidity(function() {});
sensorTag.notifyAccelerometer(function() {});
sensorTag.notifyMagnetometer(function() {});
sensorTag.notifyGyroscope(function() {});
});
});
});
node.on('close',function() {
if (node.st) {
node.st.disconnect(function(){});
delete node.st;
}
});
}
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("sensorTag",sensorTagNode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment