forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.ino
71 lines (56 loc) · 1.97 KB
/
led.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>
// define pins (varies per shield/board)
#define BLE_REQ 10
#define BLE_RDY 2
#define BLE_RST 9
// LED pin
#define LED_PIN 3
// create peripheral instance, see pinouts above
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
// create service
BLEService ledService = BLEService("19b10000e8f2537e4f6cd104768a1214");
// create switch characteristic
BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
#if defined (__AVR_ATmega32U4__)
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board
#endif
// set LED pin to output mode
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);
// begin initialization
blePeripheral.begin();
Serial.println(F("BLE LED Peripheral"));
}
void loop() {
BLECentral central = blePeripheral.central();
if (central) {
// central connected to peripheral
Serial.print(F("Connected to central: "));
Serial.println(central.address());
while (central.connected()) {
// central still connected to peripheral
if (switchCharacteristic.written()) {
// central wrote new value to characteristic, update LED
if (switchCharacteristic.value()) {
Serial.println(F("LED on"));
digitalWrite(LED_PIN, HIGH);
} else {
Serial.println(F("LED off"));
digitalWrite(LED_PIN, LOW);
}
}
}
// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}