Maker Faire Bay Area - May 21/22, 2016
Don Coleman - Chariot Solutions - @doncoleman
Sandeep Mistry - Arduino - @sandeepmistry
0000FF10-0000-1000-8000-00805F9B34FB
0000FF11-0000-1000-8000-00805F9B34FB
0000FF12-0000-1000-8000-00805F9B34FB
Provides services
Uses services on a peripheral
#include <BLEPeripheral.h>
#include <CurieBLE.h>
BLEPeripheral blePeripheral;
// Can also override default pins for
// nRF8001 based boards:
//
// blePeripheral(req, rdy, rst);
BLEService ledService("FF10");
BLECharCharacteristic
switchCharacteristic("FF11", BLERead | BLEWrite);
BLEDescriptor switchDescriptor("2901", "Switch");
BLEUnsignedCharCharacteristic
dimmerCharacteristic("FF12", BLERead | BLEWrite);
BLEDescriptor dimmerDescriptor("2901", "Dimmer");
const int LED_PIN = 6;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
blePeripheral.addAttribute(ledService);
blePeripheral.addAttribute(switchCharacteristic);
blePeripheral.addAttribute(switchDescriptor);
blePeripheral.addAttribute(dimmerCharacteristic);
blePeripheral.addAttribute(dimmerDescriptor);
// ...
}
void setup() {
// ...
switchCharacteristic.setEventHandler(
BLEWritten, switchCharacteristicWritten);
dimmerCharacteristic.setEventHandler(
BLEWritten, dimmerCharacteristicWritten);
blePeripheral.setAdvertisedServiceUuid(
ledService.uuid());
blePeripheral.setLocalName("LED");
blePeripheral.begin();
}
void loop() {
blePeripheral.poll();
}
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);
}
}
void dimmerCharacteristicWritten(
BLECentral& central,
BLECharacteristic& characteristic) {
Serial.print(F("Dimmer set to: "));
Serial.println(dimmerCharacteristic.value());
analogWrite(LED_PIN, dimmerCharacteristic.value());
}
BLEService thermometerService("BBB0");
BLEFloatCharacteristic
temperatureCharacteristic("BBB1",
BLERead | BLENotify);
BLEDescriptor temperatureDescriptor("2901",
"degrees C");
void loop()
{
blePeripheral.poll();
if(millis() - previousMillis > interval) {
pollTemperatureSensor();
previousMillis = millis();
}
}
void pollTemperatureSensor()
{
float temp = calculateTemperature();
if (tempCharac.value() != temp) {
tempCharac.setValue(temp);
Serial.println(temp);
}
}
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;
}
Don Coleman -
@doncoleman
don@chariotsolutions.com
Sandeep Mistry -
@sandeepmistry
s.mistry@arduino.cc
Slides don.github.io/slides
Code github.com/don/mfba2016-arduino-ble
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/.../2016-05-21-arduino-ble.