Skip to content

Instantly share code, notes, and snippets.

@don
Created October 3, 2015 18:51
Show Gist options
  • Save don/669f30ff71c21d0d268f to your computer and use it in GitHub Desktop.
Save don/669f30ff71c21d0d268f to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <BLEPeripheral.h>
#define LED_PIN 3
// define pins (varies per shield/board)
#define BLE_REQ 10
#define BLE_RDY 2
#define BLE_RST 9
// create peripheral instance, see pinouts above
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEService ledService = BLEService("ff10");
BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("ff11", BLERead | BLEWrite);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
// set advertised local name and service UUID
blePeripheral.setLocalName("LED");
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
// add service and characteristic
blePeripheral.addAttribute(ledService);
blePeripheral.addAttribute(switchCharacteristic);
// assign event handlers for characteristic
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
blePeripheral.begin();
Serial.println(F("BLE LED Peripheral"));
}
void loop() {
blePeripheral.poll();
}
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
// central wrote new value to characteristic, update LED
Serial.print(F("Characteristic event, writen: "));
if (switchCharacteristic.value()) {
Serial.println(F("LED on"));
digitalWrite(LED_PIN, HIGH);
} else {
Serial.println(F("LED off"));
digitalWrite(LED_PIN, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment