Skip to content

Instantly share code, notes, and snippets.

@jpmens
Last active January 19, 2018 21:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmens/af90e8f87254226d0dd5 to your computer and use it in GitHub Desktop.
Save jpmens/af90e8f87254226d0dd5 to your computer and use it in GitHub Desktop.
TI SensorTag2 (CC2650) to MQTT with Node.js (http://jpmens.net/2015/11/23/yet-another-fine-thing-sensortag-cc2650/)
{
"version": 1,
"allow_edit": true,
"plugins": [],
"panes": [
{
"title": "Lux",
"width": 1,
"row": {
"3": 1
},
"col": {
"3": 3
},
"col_width": 1,
"widgets": [
{
"type": "text_widget",
"settings": {
"title": "",
"size": "regular",
"value": "datasources[\"mqtt\"][\"msg\"][\"lux\"]",
"sparkline": true,
"animate": false
}
}
]
},
{
"title": "Buttons",
"width": 1,
"row": {
"3": 1
},
"col": {
"3": 2
},
"col_width": 1,
"widgets": [
{
"type": "indicator",
"settings": {
"title": "Left",
"value": "datasources[\"mqtt\"][\"msg\"][\"left\"]"
}
},
{
"type": "indicator",
"settings": {
"title": "Right",
"value": "datasources[\"mqtt\"][\"msg\"][\"right\"]"
}
},
{
"type": "indicator",
"settings": {
"title": "Reed",
"value": "datasources[\"mqtt\"][\"msg\"][\"reed\"]"
}
}
]
},
{
"title": "Weather",
"width": 1,
"row": {
"3": 1
},
"col": {
"3": 1
},
"col_width": 1,
"widgets": [
{
"type": "gauge",
"settings": {
"title": "Humidity",
"value": "datasources[\"mqtt\"][\"msg\"][\"humidity\"]",
"units": "%",
"min_value": 0,
"max_value": 100
}
},
{
"type": "gauge",
"settings": {
"title": "Pressure",
"value": "datasources[\"mqtt\"][\"msg\"][\"millibars\"]",
"units": "millibar",
"min_value": "100",
"max_value": "1200"
}
}
]
},
{
"title": "Temperature",
"width": 1,
"row": {
"3": 7
},
"col": {
"3": 3
},
"col_width": 1,
"widgets": [
{
"type": "gauge",
"settings": {
"title": "Ambient",
"value": "datasources[\"mqtt\"][\"msg\"][\"ambtemp\"]",
"units": "°C",
"min_value": "-10",
"max_value": "35"
}
},
{
"type": "gauge",
"settings": {
"title": "Object (IR)",
"value": "datasources[\"mqtt\"][\"msg\"][\"objtemp\"]",
"units": "°C",
"min_value": "-10",
"max_value": "35"
}
}
]
},
{
"width": 1,
"row": {
"3": 9
},
"col": {
"3": 2
},
"col_width": 1,
"widgets": [
{
"type": "picture",
"settings": {
"src": "http://www.ti.com/ww/en/wireless_connectivity/sensortag2015/images/sensorTag-main-visual.png",
"refresh": 0
}
}
]
},
{
"width": 1,
"row": {
"3": 15
},
"col": {
"3": 1
},
"col_width": 1,
"widgets": [
{
"type": "text_widget",
"settings": {
"title": "ID (MacAddr)",
"size": "regular",
"value": "datasources[\"mqtt\"][\"msg\"][\"_id\"]",
"animate": true
}
},
{
"type": "text_widget",
"settings": {
"title": "Firmware",
"size": "regular",
"value": "datasources[\"mqttextras\"][\"msg\"]",
"animate": true
}
}
]
}
],
"datasources": [
{
"name": "mqtt",
"type": "paho_mqtt",
"settings": {
"server": "localhost",
"port": 9008,
"use_ssl": false,
"client_id": "fff888888888",
"username": "",
"password": "",
"topic": "jpmens/cc2650",
"json_data": true,
"name": "mqtt"
}
},
{
"name": "mqttextras",
"type": "paho_mqtt",
"settings": {
"server": "localhost",
"port": 9008,
"use_ssl": false,
"client_id": "",
"username": "",
"password": "",
"topic": "jpmens/cc2650/firmwarerev",
"json_data": false
}
}
],
"columns": 3
}
/*
* sensortag2mqtt by Jan-Piet Mens (C) November 2015
* read SensorTag CC2650 and publish over MQTT
* Credits to Tom Igoe for example at https://github.com/tigoe/BluetoothLE-Examples
*
* Fedora23: # dnf install npm nodejs
* # npm install sensortag // https://github.com/sandeepmistry/node-sensortag
* # npm install mqtt // https://github.com/mqttjs/MQTT.js
*
* Launch this program with
* $ node sensortag2mqtt.js
* and click the left button on the SensorTag (the small, round one)
* Every notification change from the SensorTag will result in an
* MQTT message being published.
*
*/
var SensorTag = require('sensortag');
var mqtt = require('mqtt').connect(process.env.BROKERURL || 'mqtt://localhost:1883');
var topic = 'jpmens/cc2650';
var data = {
ambtemp: 0,
lux: 0,
millibars: 0,
humidity: 0,
gyroX: 0,
gyroY: 0,
gyroZ: 0,
reed: false,
left: false,
right: false,
};
function fix(num, dec) {
var s = num.toFixed(dec);
return parseFloat(s);
}
// listen for tags:
SensorTag.discover(function(tag) {
tag.on('disconnect', function() {
console.log('disconnected!');
process.exit(0);
});
function publishData() {
var payload = JSON.stringify(data);
console.log(payload);
mqtt.publish(topic, payload);
}
function mod(key, val) {
if (data[key] != val) {
console.log(key + " changed to " + val);
data[key] = val;
publishData();
}
}
function connectAndSetUpMe() {
console.log('connectAndSetUp');
data._id = tag.id; // xxxxxxxxxxxx
data._tagtype = tag.type; // cc2650
tag.connectAndSetUp(enableSensors);
}
function enableSensors() {
console.log('enableSensors');
// when sensors are enabled, start notifications
tag.enableBarometricPressure(n_baro);
// tag.enableGyroscope(n_gyro);
tag.enableHumidity(n_humid);
tag.enableLuxometer(n_lux);
tag.enableIrTemperature(n_irtemp);
tag.enableAccelerometer(n_accel);
// tag.enableMagnetometer(n_magnet);
tag.notifySimpleKey(function() {
tag.on('simpleKeyChange', function(left, right, reed) {
mod('reed', reed);
mod('left', left);
mod('right', right);
if (left && right) {
// tag.disconnect();
}
});
});
tag.readFirmwareRevision(function(err, firmwarerev) {
if (err) {
console.log(err);
} else {
mqtt.publish(topic + "/firmwarerev", firmwarerev);
}
});
}
function n_baro() {
tag.notifyBarometricPressure(function() {
tag.on('barometricPressureChange', function(pressure) {
mod('millibars', fix(pressure, 0));
});
});
}
function n_gyro() {
tag.notifyGyroscope(function() {
tag.on('gyroscopeChange', function(x,y,z) {
mod('gyroX', fix(x, 3));
mod('gyroY', fix(y, 3));
mod('gyroZ', fix(z, 3));
});
});
}
function n_humid() {
tag.notifyHumidity(function() {
tag.on('humidityChange', function(ambtemp, humidity) {
mod('ambtemp', fix(ambtemp, 2));
mod('humidity', fix(humidity, 2));
});
});
}
function n_lux() {
tag.notifyLuxometer(function() {
tag.on('luxometerChange', function(lux) {
mod('lux', lux);
});
});
}
function n_irtemp() {
tag.notifyIrTemperature(function() {
tag.on('irTemperatureChange', function(objtemp, ambtemp) {
mod('objtemp', fix(objtemp, 2));
});
});
}
function n_accel() {
tag.notifyAccelerometer(function() {
tag.on('accelerometerChange', function(x, y, z) {
mod('accX', fix(x, 2));
mod('accY', fix(y, 2));
mod('accZ', fix(z, 2));
});
});
}
function n_magnet() {
tag.notifyMagnetometer(function() {
tag.on('magnetometerChange', function(x, y, z) {
mod('magX', fix(x, 2));
mod('magY', fix(y, 2));
mod('magZ', fix(z, 2));
mod('magAll', [x, y, z]);
});
});
}
// Now that we've defined all the functions, start the process
connectAndSetUpMe();
});
@ishuverma
Copy link

Very very useful script for SensorTag. Thanks.
The mqtt message broker I want to connect to requires user and pwd. Where do I put the user and pwd info?
I tried it adding it to line 19 but it doesn't connect successfully.
Thanks.

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