Skip to content

Commit 3d3f973

Browse files
hbisbysandeepmistry
authored andcommitted
ported examples from BLEPeripheral buttonLED and callbackLED
1 parent 2581cff commit 3d3f973

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright (c) 2015 Intel Corporation. All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
17+
1301 USA
18+
*/
19+
20+
21+
#include <CurieBle.h>
22+
23+
const int ledPin = 13; // set ledPin to on-board LED
24+
const int buttonPin = 4; // set buttonPin to digital pin 4
25+
26+
BlePeripheral blePeripheral; // create peripheral instance
27+
BleService ledService("19b10010e8f2537e4f6cd104768a1214"); // create service
28+
29+
30+
// create switch characteristic and allow remote device to read and write
31+
BleCharCharacteristic switchCharacteristic("19b10011e8f2537e4f6cd104768a1214", BleRead | BleWrite);
32+
// create button characteristic and allow remote device to get notifications
33+
BleCharCharacteristic buttonCharacteristic("19b10012e8f2537e4f6cd104768a1214", BleRead | BleNotify); // allows remote device to get notifications
34+
35+
void setup() {
36+
Serial.begin(9600);
37+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
38+
pinMode(buttonPin, INPUT); // use button pin 4 as an input
39+
40+
// set a name for the BLE peripheral. Max length is 20 characters:
41+
blePeripheral.setLocalName("Curie LED_Switch Sketch");
42+
// set the UUID for the service this peripheral advertises:
43+
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
44+
45+
// add service and characteristics
46+
blePeripheral.addAttribute(ledService);
47+
blePeripheral.addAttribute(switchCharacteristic);
48+
blePeripheral.addAttribute(buttonCharacteristic);
49+
50+
// advertise the service
51+
blePeripheral.begin();
52+
53+
Serial.println("Bluetooth device active, waiting for connections...");
54+
}
55+
56+
void loop() {
57+
// poll peripheral
58+
blePeripheral.poll();
59+
60+
// read the current button pin state
61+
char buttonValue = digitalRead(buttonPin);
62+
63+
// has the value changed since the last read
64+
boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);
65+
66+
if (buttonChanged) {
67+
// button state changed, update characteristics
68+
switchCharacteristic.setValue(buttonValue);
69+
buttonCharacteristic.setValue(buttonValue);
70+
}
71+
72+
if (switchCharacteristic.written() || buttonChanged) {
73+
// update LED, either central has written to characteristic or button state has changed
74+
if (switchCharacteristic.value()) {
75+
Serial.println("LED on");
76+
digitalWrite(ledPin, HIGH);
77+
} else {
78+
Serial.println("LED off");
79+
digitalWrite(ledPin, LOW);
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright (c) 2015 Intel Corporation. All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
17+
1301 USA
18+
*/
19+
20+
21+
#include <CurieBle.h>
22+
23+
const int ledPin = 13; // set ledPin to use on-board LED
24+
BlePeripheral blePeripheral; // create peripheral instance
25+
26+
BleService ledService("19b10000e8f2537e4f6cd104768a1214"); // create service
27+
28+
// create switch characteristic and allow remote device to read and write
29+
BleCharCharacteristic switchChar("0x2015", BleRead | BleWrite);
30+
31+
void setup() {
32+
Serial.begin(9600);
33+
34+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
35+
36+
// set a name for the local peripheral
37+
blePeripheral.setLocalName("Curie LED Sketch");
38+
// set the UUID for the service this peripheral advertises
39+
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
40+
41+
// add service and characteristic
42+
blePeripheral.addAttribute(ledService);
43+
blePeripheral.addAttribute(switchChar);
44+
45+
// assign event handlers for connected, disconnected to peripheral
46+
blePeripheral.setEventHandler(BleConnected, blePeripheralConnectHandler);
47+
blePeripheral.setEventHandler(BleDisconnected, blePeripheralDisconnectHandler);
48+
49+
// assign event handlers for characteristic
50+
switchChar.setEventHandler(BleWritten, switchCharacteristicWritten);
51+
52+
53+
// advertise the service
54+
blePeripheral.begin();
55+
56+
Serial.println(("Bluetooth device active, waiting for connections..."));
57+
}
58+
59+
60+
61+
void loop() {
62+
// poll peripheral
63+
blePeripheral.poll();
64+
}
65+
66+
void blePeripheralConnectHandler(BleCentral& central) {
67+
// central connected event handler
68+
Serial.print("Connected event, central: ");
69+
Serial.println(central.address());
70+
}
71+
72+
void blePeripheralDisconnectHandler(BleCentral& central) {
73+
// central disconnected event handler
74+
Serial.print("Disconnected event, central: ");
75+
Serial.println(central.address());
76+
}
77+
78+
void switchCharacteristicWritten(BleCentral& central, BleCharacteristic& characteristic) {
79+
// central wrote new value to characteristic, update LED
80+
Serial.print("Characteristic event, writen: ");
81+
82+
if (switchChar.value()) {
83+
Serial.println("LED on");
84+
digitalWrite(ledPin, HIGH);
85+
} else {
86+
Serial.println("LED off");
87+
digitalWrite(ledPin, LOW);
88+
}
89+
}

0 commit comments

Comments
 (0)