National Maker Faire - June 18, 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()) {
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
Slides don.github.io/slides
Code github.com/don/mfba2016-arduino-ble
Creating Bluetooth LE Peripherals with Arduino by Don Coleman is licensed under a Creative Commons Attribution 4.0 International License.