Created
December 23, 2015 13:11
-
-
Save ties/527da7f71080bcf08d2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * This PoC script uses the [node-sensortag](https://github.com/sandeepmistry/node-sensortag) | |
| * library | |
| * | |
| * Status: "it works, but drains the sensortag in a month" | |
| */ | |
| var MAGNETOMETER_PERIOD = 1000; | |
| var SensorTag = require('sensortag'); | |
| var winston = require('winston'); | |
| var Q = require('q'); | |
| var csv = require('csv'); | |
| var fs = require('fs'); | |
| var StatsD = require('node-statsd'), | |
| var moment = require('moment'); | |
| client = new StatsD({'host': 'antigua.tiesdekock.nl'}); | |
| var logger = new (winston.Logger)({ | |
| transports: [ | |
| new (winston.transports.Console)(), | |
| new (winston.transports.File)({ filename: 'sensor.log' }) | |
| ] | |
| }); | |
| logger.info("starting discovery"); | |
| var i = 0; | |
| var logMagnetometer = function(x, y, z) { | |
| stringifier.write({'t': moment().format("X").toString(), 'x': x, 'y': y, 'z': z}); | |
| i++ | |
| if (i % 60 == 0) { | |
| logger.info(i/60); | |
| } | |
| } | |
| SensorTag.discover(function(tag) { | |
| tag.on('disconnect', function() { | |
| logger.info("disconnected from %s", tag.uuid); | |
| stringifier.end(); | |
| out_csv.end(); | |
| process.exit(0); | |
| }) | |
| logger.info("discovered %s", tag.uuid); | |
| var enableHumidity = function() { | |
| var deferred = Q.defer(); | |
| tag.enableHumidity(function (cb) { | |
| logger.info("Humidity enabled"); | |
| deferred.resolve(cb); | |
| }); | |
| return deferred.promise; | |
| } | |
| var enableTemperatureLogging = function() { | |
| logger.info("Adding humidity listener."); | |
| tag.on('humidityChange', function(temperature, humidity) { | |
| client.gauge('room.temperature', temperature.toFixed(2)); | |
| client.gauge('room.humidity', humidity.toFixed(2)); | |
| }); | |
| tag.notifyHumidity(function(cb){}); | |
| } | |
| var detect = function(val) { | |
| var deferred = Q.defer(); | |
| logger.info("starting service scan"); | |
| tag.discoverServicesAndCharacteristics(function (cb) { | |
| logger.info("Services loaded."); | |
| deferred.resolve(cb); | |
| }); | |
| return deferred.promise; | |
| } | |
| var connect = function(val) { | |
| var deferred = Q.defer(); | |
| tag.connect(function (cb) { | |
| deferred.resolve(cb); | |
| }); | |
| return deferred.promise; | |
| } | |
| connect() | |
| .then(detect) | |
| .then(enableHumidity) | |
| .then(enableTemperatureLogging); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment