ITP | Week 1 | February 5, 2016
Don Coleman
0000FF10-0000-1000-8000-00805F9B34FB
0000FF11-0000-1000-8000-00805F9B34FB
0000FF12-0000-1000-8000-00805F9B34FB
Provides services
Exercise 1
#define LED_PIN 6
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH); // turn the LED on
delay(1000); // wait one second
digitalWrite(LED_PIN, LOW); // turn the LED off
delay(1000); // wait one second
}
Exercise 2
#include <SPI.h>
#include <BLEPeripheral.h>
#define LED_PIN 6
#define BLE_REQ 9
#define BLE_RDY 8
#define BLE_RST 5
BLEPeripheral blePeripheral =
BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEService ledService = BLEService("FF10");
BLECharCharacteristic switchCharacteristic
= BLECharCharacteristic("FF11", BLERead | BLEWrite);
BLEDescriptor switchDescriptor
= BLEDescriptor("2901", "Switch");
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
blePeripheral.addAttribute(ledService);
blePeripheral.addAttribute(switchCharacteristic);
blePeripheral.addAttribute(switchDescriptor);
// ...
}
void setup() {
// ...
switchCharacteristic.setEventHandler(
BLEWritten, switchCharacteristicWritten);
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);
}
}
Exercise 3
BLEUnsignedCharCharacteristic dimmerCharacteristic
= BLEUnsignedCharCharacteristic("FF12",
BLERead | BLEWrite);
BLEDescriptor dimmerDescriptor
= BLEDescriptor("2901", "Dimmer");
void setup() {
// ...
blePeripheral.addAttribute(dimmerCharacteristic);
blePeripheral.addAttribute(dimmerDescriptor);
// ...
dimmerCharacteristic.setEventHandler(
BLEWritten, dimmerCharacteristicWritten);
// ...
}
void dimmerCharacteristicWritten(
BLECentral& central,
BLECharacteristic& characteristic) {
Serial.print(F("Dimmer set to: "));
Serial.println(dimmerCharacteristic.value());
analogWrite(LED_PIN, dimmerCharacteristic.value());
}
Exercise 4
BLEService buttonService = BLEService("FFE0");
BLECharCharacteristic buttonCharacteristic =
BLECharCharacteristic("FFE1", BLENotify);
BLEDescriptor buttonDescriptor =
BLEDescriptor("2901", "Button State");
void loop() {
blePeripheral.poll();
char buttonValue = digitalRead(BUTTON_PIN);
if (buttonCharacteristic.value() != buttonValue) {
Serial.print("Button ");
Serial.println(buttonValue, HEX);
buttonCharacteristic.setValue(buttonValue);
}
}
void loop() {
blePeripheral.poll();
// limit how often we read the button
if (millis() - lastReadTime > readInterval) {
readButton();
lastReadTime = millis();
}
}
void readButton() {
char buttonValue = digitalRead(BUTTON_PIN);
// has the value changed since the last read?
if (buttonCharacteristic.value() != buttonValue) {
Serial.print("Button ");
Serial.println(buttonValue, HEX);
buttonCharacteristic.setValue(buttonValue);
}
}
Exercise 5
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
dc159@nyu.eduhttp://don.github.io/slides/2016-02-05-itp-bluetoothle
Bluetooth LE ITP Week 1 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-02-05-itp-bluetoothle.