SensorTag.discover(function(sensorTag) { console.log('discovered: ' + sensorTag); id = sensorTag.id; sensorTag.on('disconnect', function() { console.log('disconnected!'); process.exit(0); }); async.series([ function(callback) { console.log('connectAndSetUp'); sensorTag.connectAndSetUp(callback); }, function(callback) { setTimeout(callback, 2000); }, function(callback) { sensorTag.readSerialNumber(function(error, serial){ console.log(serial); callback(); }); }, function(callback) { console.log('enableHumidity'); sensorTag.enableHumidity( setInterval(function(){ console.log('readHumidity'); sensorTag.readHumidity(function(error, temperature, humidity) { console.log('\ttemperature = %d °C', temperature.toFixed(1)); console.log('\thumidity = %d %', humidity.toFixed(1)); temp = temperature.toFixed(1); hum = humidity.toFixed(1); }); },20000) ); sensorTag.enableLuxometer( setInterval(function(){ console.log('readLuxometer'); sensorTag.readLuxometer(function(error, lux) { console.log('\tlux = %d lux', lux.toFixed(1)); luxo = lux.toFixed(1); //args = "{'data': { 'Temperature':"+data.Temperature+",'Humidity':"+data.Humidity+",'Lux':"+data.Lux+"},'headers': { 'Content-Type': 'application/json' }}"; var args = '{"data": { "id": "'+id+'", "lux": '+luxo+', "Temperature": '+temp+',"Humidity": '+hum+'},"headers": { "Content-Type": "application/json" }}'; console.log(args); client.post("https://dweet.io:443/dweet/for/stefanssensortag1", JSON.parse(args), function (data, response) {}); }); },20000) ); }, ] ); });
// The main node definition - most things happen in here function sensorTagNode(n) { RED.nodes.createNode(this,n); this.name = n.name; this.topic = n.topic; this.uuid = n.uuid; 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; if (this.uuid === "") { this.uuid = undefined; } //console.log(this.uuid); var node=this; if ( typeof node.stag == "undefined") { //console.log("starting"); SensorTag.discover(function(sensorTag){ node.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 + '/temperature'}; 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(node); }); }); },node.uuid); } else { //console.log("reconfig"); enable(node); } }
SensorTag.discover(function(sensorTag) { sensorTag.on('disconnect', function() { console.log('Tag Disconnected'); process.exit(0); }); // run functions in series async.series([ function(callback) { // read config file if any properties.parse(configFile, { path : true }, function(err, config) { if (err && err.code != 'ENOENT') throw err; if (config) { org = config.org || missing('org'); type = config.type || missing('type'); deviceId = config.id || missing('id'); password = config['auth-token'] || missing('auth-token'); var method = config['auth-method'] || missing('auth-method'); if (method != 'token') { console.log("unexpected auth-method = " + method); process.exit(1); } username = '******'; host = org + reg_domain; tls = true; qs_mode = false; } callback(); }); }, function(callback) { // fill deviceId if (qs_mode && !deviceId) { getmac.getMac(function(err, macAddress) { if (err) throw err; deviceId = macAddress.replace(/:/g, '').toLowerCase(); callback(); }); } else callback(); }, function(callback) { console.log('SensorTag connected'); sensorTag.connect(callback); }, function(callback) { console.log('Discovering services and characteristics'); sensorTag.discoverServicesAndCharacteristics(callback); }, function(callback) { sensorTag.readDeviceName(function(deviceName) { console.log('Device name = ' + deviceName); tagData.d.myName = deviceName; callback(); }); }, function(callback) { sensorTag.readSystemId(function(systemId) { console.log('System id = ' + systemId); callback(); tagData.d.myName += " " + systemId; }); }, function(callback) { sensorTag.readSerialNumber(function(serialNumber) { console.log('Serial number = ' + serialNumber); callback(); }); }, function(callback) { sensorTag.readFirmwareRevision(function(firmwareRevision) { console.log('Firmware revision = ' + firmwareRevision); callback(); }); }, function(callback) { sensorTag.readHardwareRevision(function(hardwareRevision) { console.log('Hardware revision = ' + hardwareRevision); callback(); }); }, function(callback) { sensorTag.readHardwareRevision(function(softwareRevision) { console.log('Software revision = ' + softwareRevision); callback(); }); }, function(callback) { sensorTag.readManufacturerName(function(manufacturerName) { console.log('Manufacturer name = ' + manufacturerName); callback(); }); }, function(callback) { console.log('Enable IR temperature'); sensorTag.enableIrTemperature(callback); }, function(callback) { console.log('Enable accelerometer'); sensorTag.enableAccelerometer(callback); }, function(callback) { sensorTag.setAccelerometerPeriod(1000, callback); }, function(callback) { console.log('Enable humidity sensor'); sensorTag.enableHumidity(callback); }, function(callback) { console.log('Enable magnetometer'); sensorTag.enableMagnetometer(callback); }, function(callback) { sensorTag.setMagnetometerPeriod(1000, callback); }, function(callback) { console.log('Enable barometer'); sensorTag.enableBarometricPressure(callback); }, function(callback) { console.log('Enable gyroscope'); sensorTag.enableGyroscope(callback); }, function(callback) { // connect MQTT client var clientId = "d:" + org + ":" + type + ":" + deviceId; console.log('MQTT clientId = ' + clientId); if (qs_mode) { client = mqtt.createClient(u_port, host, { clientId : clientId, keepalive : 30 }); } else { if (tls) { console.log("TLS connect: " + host + ":" + s_port); client = mqtt.createSecureClient(s_port, host, { clientId : clientId, keepalive : 30, username : username, password : password, rejectUnauthorized: true, ca: caCerts }); } else { console.log("Connect host: " + host + " port " + u_port); client = mqtt.createClient(u_port, host, { clientId : clientId, keepalive : 30, username : username, password : password }); } } client.on('connect', function() { // not reliable since event may fire before handler // installed console.log('MQTT Connected'); console.log("Sending data") if (qs_mode) { console.log('MAC address = ' + deviceId); console.log('Go to the following link to see your device data;'); console.log('http://quickstart.internetofthings.ibmcloud.com/#/device/' + deviceId + '/sensor/') } }); client.on('error', function(err) { console.log('client error' + err); process.exit(1); }); client.on('close', function() { console.log('client closed'); process.exit(1); }); callback(); }, function(callback) { ledBlink(0, 0); // turn off if (!qs_mode) { client.subscribe(sub_topic, { qos: 0 }, function(err, granted) { if (err) throw err; console.log('Subscribed to ' + sub_topic); callback(); }); client.on('message', doCommand); } else { callback(); } }, function(callback) { setTimeout(callback, 1000); setInterval(function(tag) { tag.publish(); }, 1000, tagData); }, function(callback) { sensorTag.on('irTemperatureChange', function(objectTemperature, ambientTemperature) { tagData.d.objectTemp = parseFloat(objectTemperature.toFixed(1)); tagData.d.ambientTemp = parseFloat(ambientTemperature.toFixed(1)); }); sensorTag.notifyIrTemperature(function() { }); callback(); }, function(callback) { sensorTag.on('accelerometerChange', function(x, y, z) { tagData.d.accelX = parseFloat(x.toFixed(1)); tagData.d.accelY = parseFloat(y.toFixed(1)); tagData.d.accelZ = parseFloat(z.toFixed(1)); }); sensorTag.notifyAccelerometer(function() { }); callback(); }, function(callback) { sensorTag.on('humidityChange', function(temperature, humidity) { tagData.d.humidity = parseFloat(humidity.toFixed(1)); tagData.d.temp = parseFloat(temperature.toFixed(1)); }); sensorTag.notifyHumidity(function() { }); callback(); }, function(callback) { sensorTag.on('magnetometerChange', function(x, y, z) { tagData.d.magX = parseFloat(x.toFixed(1)); tagData.d.magY = parseFloat(y.toFixed(1)); tagData.d.magZ = parseFloat(z.toFixed(1)); }); sensorTag.notifyMagnetometer(function() { }); callback(); }, function(callback) { sensorTag.on('barometricPressureChange', function(pressure) { tagData.d.pressure = parseFloat(pressure.toFixed(1)); }); sensorTag.notifyBarometricPressure(function() { }); callback(); }, function(callback) { sensorTag.on('gyroscopeChange', function(x, y, z) { tagData.d.gyroX = parseFloat(x.toFixed(1)); tagData.d.gyroY = parseFloat(y.toFixed(1)); tagData.d.gyroZ = parseFloat(z.toFixed(1)); }); sensorTag.notifyGyroscope(function() { }); callback(); }, function(callback) { sensorTag.on('simpleKeyChange', function(left, right) { console.log('keys left: ' + left + ' right: ' + right); if (left && right) { sensorTag.notifySimpleKey(callback); } }); sensorTag.notifySimpleKey(function() { }); }, function(callback) { console.log('disconnect'); sensorTag.disconnect(callback); } ]); });
SensorTag.discover(function (tag) { tag.on('disconnect', function () { console.log('disconnected!'); process.exit(0); }); function connectAndSetUpTag() { console.log('connectAndSetUp'); tag.connectAndSetUp(enableIrTemp); } function enableIrTemp() { console.log('enableIRTemperatureSensor'); // when you enable the IR Temperature sensor, start notifications: tag.enableIrTemperature(notifyMe); } function notifyMe() { tag.notifyIrTemperature(readTemperature); // start the accelerometer listener } function readTemperature() { tag.on('irTemperatureChange', function(objectTemp, ambientTemp) { console.log('\tObject Temp = %d deg. C', objectTemp.toFixed(1)); console.log('\tAmbient Temp = %d deg. C', ambientTemp.toFixed(1)); var usersRef = ref.child("temperatures"); usersRef.push({ objectTemp: objectTemp.toFixed(1), ambientTemp: ambientTemp.toFixed(1) }); }); } connectAndSetUpTag(); });
deviceClient.on('connect', function() { sensorTag.discover(function(sensorTag) { console.log('discovered: ' + sensorTag); setupMeasure(sensorTag); }); });