Creating Bluetooth LE

Peripherals with Arduino

National Maker Faire - June 18, 2016

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

Compatible Hardware

Compatible Hardware List

Arduino 101

Arduino 101 - CurieBLE


https://www.arduino.cc/en/Reference/CurieBLE

Lightbulb Service

  • Light Switch
  • Dimmer Setting

Lightbulb Service - FF10

16 bit UUIDs

0000FF10-0000-1000-8000-00805F9B34FB

0000FF11-0000-1000-8000-00805F9B34FB

0000FF12-0000-1000-8000-00805F9B34FB

Lightbulb Service - FF10

LED.ino (BLEPeripheral library)


#include <BLEPeripheral.h>
  

LED.ino (Arduino 101)


#include <CurieBLE.h>
  

Create BLE Peripheral


BLEPeripheral blePeripheral;
  

Create Service


BLEService ledService("FF10");
  

Characteristics & Descriptors


BLECharCharacteristic
    switchCharacteristic("FF11", BLERead | BLEWrite);

BLEDescriptor switchDescriptor("2901", "Switch");
  

Characteristics & Descriptors


BLEUnsignedCharCharacteristic
    dimmerCharacteristic("FF12", BLERead | BLEWrite);

BLEDescriptor dimmerDescriptor("2901", "Dimmer");
  

LED pin


const int LED_PIN = 6;
  

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("BBB0");

BLEFloatCharacteristic
     temperatureCharacteristic("BBB1",
                               BLERead | BLENotify);

BLEDescriptor temperatureDescriptor("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 and Arduino 101
  • Create Eddystone Beacons *
  • Create HID devices (mouse, keyboard) *


* Not available in CurieBLE yet


makebluetooth.com

Thank You

Don Coleman

@doncoleman

don@chariotsolutions.com

Slides don.github.io/slides

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

 

Creative Commons License
Creating Bluetooth LE Peripherals with Arduino by Don Coleman is licensed under a Creative Commons Attribution 4.0 International License.