From c92cbacbfad65f5ffde40688c7ad1970e9bc07fa Mon Sep 17 00:00:00 2001 From: Simon Vogl Date: Mon, 28 Oct 2019 13:53:33 +0100 Subject: [PATCH] made the sensor data to read a little more more general - set the environment variable CHANNEL to the type of data you want to read. --- examples/cache-gatt-discovery.js | 28 ++++++++++++++++++++-------- examples/cache-gatt-reconnect.js | 32 +++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/examples/cache-gatt-discovery.js b/examples/cache-gatt-discovery.js index 553a9bc49..ae146682b 100644 --- a/examples/cache-gatt-discovery.js +++ b/examples/cache-gatt-discovery.js @@ -8,7 +8,13 @@ var noble = require('../index'); const fs = require('fs'); +// the sensor value to scan for, number of bits and factor for displaying it +const CHANNEL = process.env['CHANNEL'] ? process.env['CHANNEL'] : 'Temperature' +const BITS = process.env['BITS'] ? 1 * process.env['BITS'] : 16 +const FACTOR = process.env['FACTOR'] ? 1. * process.env['FACTOR'] : .1 + const EXT='.dump' + noble.on('stateChange', function(state) { if (state === 'poweredOn') { noble.startScanning(); @@ -88,7 +94,7 @@ let findServices = function (noble, peripheral) { peripheral.discoverServices([], (error, services) => { - let temperatureCharacteristic = undefined + let sensorCharacteristic = undefined servicesToRead = services.length // we found the list of services, now trigger characteristics lookup for each of them: @@ -112,9 +118,9 @@ let findServices = function (noble, peripheral) { let ch = characteristics[j] console.log('\t' + service.uuid + ' chara.\t ' + ' ' + j + ' ' + ch) - if ( ch.name === 'Temperature') { - console.log('found temperature characteristic!') - temperatureCharacteristic = ch + if ( ch.name === CHANNEL) { + console.log('found ' + CHANNEL + ' characteristic!') + sensorCharacteristic = ch } } @@ -130,15 +136,21 @@ let findServices = function (noble, peripheral) { console.log("The data was saved to " , meta.uuid + EXT); }); - if (temperatureCharacteristic) { + if (sensorCharacteristic) { console.log('Listening for temperature data...') tRead = Date.now() - temperatureCharacteristic.on('data', (data) => { - console.log(' new temperature ' + (data.readUInt16LE()/100.) + ' C' ) + sensorCharacteristic.on('data', (data) => { + if (BITS === 16 ) { + console.log(' new ' + CHANNEL + ' ' + (data.readUInt16LE() * FACTOR) ) + } else if (BITS === 32) { + console.log(' new ' + CHANNEL + ' ' + (data.readUInt32LE() * FACTOR) ) + } else { + console.log(' Cannot cope with BITS value '+ BITS) + } }) - temperatureCharacteristic.read() + sensorCharacteristic.read() } console.log('Timespan from discovery to connected: ' + (tConn -tDisco) + ' ms') diff --git a/examples/cache-gatt-reconnect.js b/examples/cache-gatt-reconnect.js index 2ad0b910e..fcdcfa5b1 100644 --- a/examples/cache-gatt-reconnect.js +++ b/examples/cache-gatt-reconnect.js @@ -8,6 +8,11 @@ var noble = require('../index'); const fs = require('fs'); +// the sensor value to scan for, number of bits and factor for displaying it +const CHANNEL = process.env['CHANNEL'] ? process.env['CHANNEL'] : 'Temperature' +const BITS = process.env['BITS'] ? 1 * process.env['BITS'] : 16 +const FACTOR = process.env['FACTOR'] ? 1. * process.env['FACTOR'] : .1 + const EXT='.dump' noble.on('stateChange', function(state) { @@ -69,23 +74,28 @@ let quickConnect = function (peripheral) { meta = loadData(peripheral) // initialize the service and charateristics objects in Noble; return a temperature characteristic, if found - let temperatureCharacteristic = setData(peripheral, meta) + let sensorCharacteristic = setData(peripheral, meta) - if (!temperatureCharacteristic) { + if (!sensorCharacteristic) { console.log('Warning - no temperature characteristic found.') } else { console.log('Listening for temperature data...') tRead = Date.now() - temperatureCharacteristic.on('data', (data) => { - console.log(' new temperature ' + (data.readUInt16LE()/100.) + ' C' ) + sensorCharacteristic.on('data', (data) => { + if (BITS === 16 ) { + console.log(' new ' + CHANNEL + ' ' + (data.readUInt16LE() * FACTOR) ) + } else if (BITS === 32) { + console.log(' new ' + CHANNEL + ' ' + (data.readUInt32LE() * FACTOR) ) + } else { + console.log(' Cannot cope with BITS value '+ BITS) + } }) - temperatureCharacteristic.read() + sensorCharacteristic.read() console.log('Timespan from discovery to connected: ' + (tConn -tDisco) + ' ms') console.log('Timespan from connected to reading : ' + (tRead -tConn) + ' ms') - } }) } @@ -112,7 +122,7 @@ let setData = function(peripheral, meta) { } console.log() - let temperatureCharacteristic = undefined + let sensorCharacteristic = undefined console.log('initializing characteristics... ') // now, for each service, set the characteristics: @@ -126,11 +136,11 @@ let setData = function(peripheral, meta) { for (let j in characteristics) { let characteristic = characteristics[j] console.log('\t\tcharac ' + service.uuid + ' ' + j + ' ' + characteristic + ' ' + characteristic.rawProps) - if (characteristic.name === 'Temperature') { - console.log('\t\t\t-->found temperature characteristic!') - temperatureCharacteristic = characteristic + if (characteristic.name === CHANNEL) { + console.log('\t\t\t-->found ' + CHANNEL + ' characteristic!') + sensorCharacteristic = characteristic } } } - return temperatureCharacteristic + return sensorCharacteristic }