Creating Bluetooth LE

Peripherals with Arduino

Maker Faire NY - Sept 26, 2015

Don Coleman - Chariot Solutions

@doncoleman

Arduino BLE Peripheral


https://github.com/sandeepmistry/arduino-BLEPeripheral

Adafruit Bluefruit LE nRF8001 Breakout

RedBearLab BLE Shield 2.1

Other RedBearLab boards
RFduino

Compatible Hardware

Compatible Hardware List

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

LED.ino


  #include <SPI.h>
  #include <BLEPeripheral.h>

  #define LED_PIN  6
	

Create BLE Peripheral


#define BLE_REQ  9
#define BLE_RDY  8
#define BLE_RST  UNUSED

BLEPeripheral blePeripheral = 
   BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
	

Create Service


BLEService ledService = BLEService("FF10");
	

Characteristics & Descriptors


BLECharCharacteristic switchCharacteristic 
  = BLECharCharacteristic("FF11", BLERead | BLEWrite);
BLEDescriptor switchDescriptor 
  = BLEDescriptor("2901", "Switch");
	

Characteristics & Descriptors


BLEUnsignedCharCharacteristic dimmerCharacteristic 
 = BLEUnsignedCharCharacteristic("FF12",
                                 BLERead | BLEWrite);
BLEDescriptor dimmerDescriptor 
 = BLEDescriptor("2901", "Dimmer");
	

Setup

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);

  blePeripheral.addAttribute(ledService);
  blePeripheral.addAttribute(switchCharacteristic);
  blePeripheral.addAttribute(switchDescriptor);
  blePeripheral.addAttribute(dimmerCharacteristic);
  blePeripheral.addAttribute(dimmerDescriptor);

  // ...
}	 	 
	

Setup

void setup() {
  // ...
  switchCharacteristic.setEventHandler(
              BLEWritten, switchCharacteristicWritten);
  dimmerCharacteristic.setEventHandler(
              BLEWritten, dimmerCharacteristicWritten);

  blePeripheral.setAdvertisedServiceUuid(
                                    ledService.uuid());
  blePeripheral.setLocalName("LED");
  blePeripheral.begin();
}	 	 
	

Loop


  void loop() {
    blePeripheral.poll();  
  }	 
	

Switch Characteristic Written

void switchCharacteristicWritten(
	  BLECentral& central,
	  BLECharacteristic& characteristic) {

  if (switchCharacteristic.value()) {
    Serial.println(F("LED on"));
    digitalWrite(LED_PIN, HIGH);
  } else {
    Serial.println(F("LED off"));
    digitalWrite(LED_PIN, LOW);
  }
}
	

Dimmer Characteristic Written

void dimmerCharacteristicWritten(
	  BLECentral& central, 
	  BLECharacteristic& characteristic) {
  Serial.print(F("Dimmer set to: "));
  Serial.println(dimmerCharacteristic.value());

  analogWrite(LED_PIN, dimmerCharacteristic.value());
}	 
	

Demo

Thermometer Service - BBB0


Analog Temperature Sensor TMP36

Service, Characteristic, & Descriptor


  BLEService thermometerService = BLEService("BBB0");
  BLEFloatCharacteristic temperatureCharacteristic 
      = BLEFloatCharacteristic("BBB1",
                               BLERead | BLENotify);
  BLEDescriptor temperatureDescriptor 
      = BLEDescriptor("2901", "degrees C");
            	

Loop

void loop()
{
  blePeripheral.poll();

  if(millis() - previousMillis > interval) {
    pollTemperatureSensor();
    previousMillis = millis();
  }
}

Poll Temperature Sensor

void pollTemperatureSensor()
{
  float temp = calculateTemperature();

  if (tempCharac.value() != temp) {
    tempCharac.setValue(temp);
    Serial.println(temp);
  }
}

Calculate Temperature

float calculateTemperature()
{
  // read the sensor value
  int sensorValue = analogRead(TEMPERATURE_PIN);

  float voltage = sensorValue * 5.0;
  voltage /= 1024.0;
  // 100 degrees per volt with 0.5 volt offset
  float temperature = (voltage - 0.5) * 100; 

  return temperature;
}

Demo

BLE Peripheral can also

  • Create iBeacons on nRF51822
  • Create Eddystone Beacons
  • Create HID devices (mouse, keyboard)

pre-order on Amazon

Thank You

Don Coleman

@doncoleman

don@chariotsolutions.com

Slides don.github.io/slides/

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

 

Creative Commons License
Creating Bluetooth Low Energy Peripherals with Arduino 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-26-arduino-ble.