Created
March 1, 2016 22:00
-
-
Save ryanjbaxter/7fa9d4f32034e98d4564 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
| var host = "quickstart.messaging.internetofthings.ibmcloud.com"; | |
| var port = 1883; | |
| var SensorTag = require('sensortag'); | |
| var mqtt = require('mqtt'); | |
| var topic = 'iot-2/evt/lux/fmt/json'; | |
| var clientId = 'd:quickstart:sensortag:ryanssensortag'; | |
| console.log('running'); | |
| var luxListeners = []; | |
| SensorTag.discover(function(sensorTag) { | |
| console.log('found sensor tag'); | |
| sensorTag.on('disconnect', function() { | |
| console.log('disconnected!'); | |
| process.exit(0); | |
| }); | |
| sensorTag.connectAndSetUp(function() { | |
| sensorTag.enableLuxometer(); | |
| sensorTag.setLuxometerPeriod(500, function(error) { | |
| console.log('notifyLuxometer'); | |
| sensorTag.notifyLuxometer(function(error) { | |
| }); | |
| sensorTag.on('luxometerChange', function(lux) { | |
| for(var i = 0; i < luxListeners.length; i++) { | |
| luxListeners[i](lux); | |
| } | |
| }); | |
| }); | |
| }); | |
| }); | |
| var client = mqtt.connect({ | |
| host: host, | |
| port: port, | |
| clientId: "d:quickstart:sensortag:ryanssensortag" | |
| }); | |
| client.on('connect', function() { | |
| console.log('connected to iotf'); | |
| luxListeners.push(function(lux) { | |
| console.log('\tlux = %d', lux.toFixed(1)); | |
| client.publish(topic, JSON.stringify({"d":{"lux":Math.round(lux)}})); | |
| }); | |
| }); | |
| client.on('close', function() { | |
| console.log('connection closed'); | |
| }); | |
| client.on('error', function() { | |
| console.log('error'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment