Maker Faire NY - Sept 26, 2015
Don Coleman - Chariot Solutions
Can be expanded to 128 bit UUIDs
0000FF10-0000-1000-8000-00805F9B34FB
0000FF11-0000-1000-8000-00805F9B34FB
0000FF12-0000-1000-8000-00805F9B34FB
#include <SPI.h>
#include <BLEPeripheral.h>
#define LED_PIN 6
#define BLE_REQ 9
#define BLE_RDY 8
#define BLE_RST UNUSED
BLEPeripheral blePeripheral =
BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEService ledService = BLEService("FF10");
BLECharCharacteristic switchCharacteristic
= BLECharCharacteristic("FF11", BLERead | BLEWrite);
BLEDescriptor switchDescriptor
= BLEDescriptor("2901", "Switch");
BLEUnsignedCharCharacteristic dimmerCharacteristic
= BLEUnsignedCharCharacteristic("FF12",
BLERead | BLEWrite);
BLEDescriptor dimmerDescriptor
= BLEDescriptor("2901", "Dimmer");
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 = BLEService("BBB0");
BLEFloatCharacteristic temperatureCharacteristic
= BLEFloatCharacteristic("BBB1",
BLERead | BLENotify);
BLEDescriptor temperatureDescriptor
= BLEDescriptor("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
Slides don.github.io/slides/
Code github.com/don/mfny2015-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/.../2015-09-26-arduino-ble.