Bluetooth Low Energy

with Raspberry Pi

Maker Faire NY - Sept 27, 2015

Don Coleman - Chariot Solutions

@doncoleman


Raspberry Pi 2 Model B
Installing Node.js

Lightbulb Service

  • Light Switch
  • Dimmer Setting

Lightbulb Service - FF10

16 bit UUIDs

Can be expanded to 128 bit UUIDs

0000FF10-0000-1000-8000-00805F9B34FB

0000FF11-0000-1000-8000-00805F9B34FB

0000FF12-0000-1000-8000-00805F9B34FB

Lightbulb Service - FF10

bleno

A Node.js module for implementing BLE peripherals.

github.com/sandeepmistry/bleno

  $ cd path/to/project
  $ npm install bleno
            	

led.js

var Gpio = require('onoff').Gpio,
    led = new Gpio(18, 'out');

var bleno = require('bleno');
var util = require('util');
	

Create Characteristic

var SwitchCharc = function() {
  SwitchCharc.super_.call(this, {
     uuid: 'ff11',
     properties: ['read', 'write']
  });
};
util.inherits(SwitchCharc, Characteristic);
	

Handle Read Requests

SwitchCharc.prototype.onReadRequest 
	  = function(offset, callback) {

  var data = new Buffer(1);
  data[0] = led.readSync();
  callback(this.RESULT_SUCCESS, data);
};
	

Handle Write Requests

SwitchChar.prototype.onWriteRequest 
	  = function(data, offset, 
	    withoutResponse, callback) {

  led.writeSync(data[0]);
  callback(this.RESULT_SUCCESS);
}
	

Create Service

var lightService = new PrimaryService({
  uuid: 'ff10',
  characteristics: [
    new SwitchCharc()
  ]
});
	

Advertise Service

bleno.on('stateChange', function(state) {

  if (state === 'poweredOn') {
    bleno.startAdvertising('LED', ['FF10']);
  } else {
    bleno.stopAdvertising();
  }
});
	

Add Service

bleno.on('advertisingStart', function(err) {
  if (!err) {
    bleno.setServices([lightService]);
  }
});
	

Demo

Thermometer Service - BBB0


Digital Temperature Sensor DS18B20
https://www.npmjs.com/package/ds18b20
OneWire Setup

Temperature Characteristic

Characteristic onSubscribe

Characteristic onUnsubscribe

noble

A Node.js BLE central module.

github.com/sandeepmistry/noble

  $ cd path/to/project
  $ npm install noble
             
Be sure to check out the Getting Started guide.
Downgrade Kernel to 4.0 :(
sudo rpi-update \
  46d179597370c5145c7452796acbee0f1ff93392
More info see rpi-update & rpi-firmware

button.js

var Gpio = require('onoff').Gpio,
    button = new Gpio(23, 'in', 'both');

var noble = require('noble');
	

Start Scanning

noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning(['ff10']);
  } else {
    noble.stopScanning();
  }
});

On Discover

noble.on('discover', function(peripheral) {
    console.log(peripheral);
    connectAndSetUp(peripheral);
});
	

Connect and Setup

function connectAndSetUp(peripheral) {
  peripheral.connect(function(error) {
    var serviceUUIDs = ['ff10'];
    var characteristicUUIDs = ['ff11'];
    peripheral.discoverSomeServicesAndCharacteristics(
      serviceUUIDs, characteristicUUIDs, 
      onDiscovered);
  });
}	 
	

On Discovered

function onDiscovered(err, services, characteristics) {
  function sendData(byte) {
      var buffer = new Buffer(1);
      buffer[0] = byte;
      characteristics[0].write(buffer);
  }

  button.watch(function(err, value) {
      console.log("button " + value);
      sendData(value);
  });
}

Demo


node-robosmart

iBeacons

github.com/sandeepmistry/node-bleacon

Eddystone Beacons

github.com/don/node-eddystone-beacon

pre-order on Amazon

Thank You

Don Coleman

@doncoleman

don@chariotsolutions.com

Slides don.github.io/slides/

Code github.com/don/mfny2015-rpi-ble

 

Creative Commons License
Bluetooth Low Energy with Raspberry Pi by Don Coleman
is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at https://github.com/don/.../2015-09-27-rpi-ble.