Maker Faire NY - Oct 1, 2016
Don Coleman - Chariot Solutions
0000FF10-0000-1000-8000-00805F9B34FB
0000FF11-0000-1000-8000-00805F9B34FB
0000FF12-0000-1000-8000-00805F9B34FB
#include <BLEPeripheral.h>
#include <CurieBLE.h>
BLEPeripheral blePeripheral;
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()) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
void dimmerCharacteristicWritten(
BLECentral& central,
BLECharacteristic& characteristic) {
analogWrite(LED_PIN, dimmerCharacteristic.value());
}
BLEService thermometerService("BBB0");
BLEFloatCharacteristic tempCharacteristic("BBB1",
BLERead | BLENotify);
BLEDescriptor temperatureDescriptor("2901", "degrees C");
void loop()
{
blePeripheral.poll();
if(millis() - previousMillis > interval) {
readTemperatureSensor();
previousMillis = millis();
}
}
void readTemperatureSensor()
{
float temp = tempSensor.readTempC();
if (tempCharacteristic.value() != temp) {
tempCharacteristic.setValue(temp);
}
}
Don Coleman
Slides don.github.io/slides
Code github.com/don/mfny2016-arduino-ble
Creating Bluetooth LE Peripherals with Arduino by Don Coleman is licensed under a Creative Commons Attribution 4.0 International License.