Skip to content

Commit

Permalink
made the sensor data to read a little more more general - set the env…
Browse files Browse the repository at this point in the history
…ironment

variable CHANNEL to the type of data you want to read.
  • Loading branch information
svogl committed Oct 28, 2019
1 parent fb59099 commit c92cbac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
28 changes: 20 additions & 8 deletions examples/cache-gatt-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
}

Expand All @@ -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')
Expand Down
32 changes: 21 additions & 11 deletions examples/cache-gatt-reconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')

}
})
}
Expand All @@ -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:
Expand All @@ -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
}

0 comments on commit c92cbac

Please sign in to comment.