forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 5
/
led_switch.ino
73 lines (57 loc) · 2.4 KB
/
led_switch.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
72
73
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>
// LED and button pin
#define LED_PIN 3
#define BUTTON_PIN 4
//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST)
BLEPeripheral blePeripheral = BLEPeripheral();
// create service
BLEService ledService = BLEService("19b10010e8f2537e4f6cd104768a1214");
// create switch and button characteristic
BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("19b10011e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
BLECharCharacteristic buttonCharacteristic = BLECharCharacteristic("19b10012e8f2537e4f6cd104768a1214", BLERead | BLENotify);
void setup() {
Serial.begin(115200);
#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, button pin to input mode
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
// set advertised local name and service UUID
blePeripheral.setLocalName("LED Switch");
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
// add service and characteristics
blePeripheral.addAttribute(ledService);
blePeripheral.addAttribute(switchCharacteristic);
blePeripheral.addAttribute(buttonCharacteristic);
// begin initialization
blePeripheral.begin();
Serial.println(F("BLE LED Switch Peripheral"));
}
void loop() {
// poll peripheral
blePeripheral.poll();
// read the current button pin state
char buttonValue = digitalRead(BUTTON_PIN);
// has the value changed since the last read
bool buttonChanged = (buttonCharacteristic.value() != buttonValue);
if (buttonChanged) {
// button state changed, update characteristics
switchCharacteristic.setValue(buttonValue);
buttonCharacteristic.setValue(buttonValue);
}
if (switchCharacteristic.written() || buttonChanged) {
// update LED, either central has written to characteristic or button state has changed
if (switchCharacteristic.value()) {
Serial.println(F("LED on"));
digitalWrite(LED_PIN, HIGH);
} else {
Serial.println(F("LED off"));
digitalWrite(LED_PIN, LOW);
}
}
}