Arduino 101 CurieBLECallbackLED

With this tutorial you use the Arduino 101 Bluetooth® Low Energy capabilities to turn on and of the LED connected to Pin 13 from a smartphone or tablet.

With this tutorial you use the Arduino 101's onboard Bluetooth® Low Energy capabilities to turn on and of the LED connected to Pin 13 from a smartphone or tablet. You create a LED service that polls the central and creates Bluetooth® Low Energy events that are managed with callbacks. The values are sent using nRF Master Control Panel(Bluetooth® Low Energy) app, available for Android and iOS.

Hardware Required

Software Required

  • nRF Master Control Panel(Bluetooth® Low Energy) for Android and iOS

The Circuit

genuino101fzz

image developed using Fritzing.

Software Essentials

Libraries

CurieBLE.h is the library that gives access to all the parameters, features and functions of the Bluetooth® Low Energy module of the 101 board. With Bluetooth® Low Energy it is possible to connect to and communicate with smartphones, tablets and peripherals that support this standard. In this tutorial it is used to establish a connection with a control application on the smartphone and get the value used to turn on or off a LED.

Functions

The following functions are callbacks driven by the Bluetooth® Low Energy events.

blePeripheralConnectHandler() - called when Bluetooth® Low Energy central connects to the 101 board. It prints a connect message with Bluetooth® Low Energy central address on the Arduino Software (IDE) Serial Monitor.

blePeripheralDisconnectHandler () - called when Bluetooth® Low Energy central disconnects from the 101 board. It prints a disconnect message with Bluetooth® Low Energy central address on the Arduino Software (IDE) Serial Monitor.

switchCharacteristicWritten () - called when Bluetooth® Low Energy central writes the switch characteristic. It switches LED on or off depending on the value written to that characteristic by the user on the nRF Master Control Panel.

On the Smartphone

To drive the onboard LED of Arduino 101, you need the nRF Master Control Panel(Bluetooth® Low Energy) for Android and iOS. Launch it and do a SCAN. You should find the LEDCB tab with a connect button

CallBackLED 1

Tap on connect to open the following screen, where you find the description of our Bluetooth® Low Energy service offered by the 101 board. The unknown service has a UUID 19B10000-E8F2-537E-4F6C-D104768A1214 and it is set by the

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");
statement at the beginning of the sketch.

CallBackLED 2

Tap the Unknown Service to open up its characteristic, as shown below. It includes properties that can be written with a Write Request. you also find two icons on the right of the Unknown Characteristic. The arrow pointing down means read, the other means write.

CallBackLED 3

Tap the arrow pointing up to open the Write value popup, then set the data type to UINT 8 and tap on the line to write your chosen value (either "0" or "1"). As soon as you tap on send the value is sent to the 101 board and the LED instantly changes accordingly.

CallBackLED 4

Code

1/*
2
3 * Copyright (c) 2016 Intel Corporation. All rights reserved.
4
5 * See the bottom of this file for the license terms.
6
7 */
8
9#include <CurieBLE.h>
10
11const int ledPin = 13; // set ledPin to use on-board LED
12
13BLEPeripheral blePeripheral; // create peripheral instance
14
15BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
16
17// create switch characteristic and allow remote device to read and write
18
19BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
20
21void setup() {
22
23 Serial.begin(9600);
24
25 pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
26
27 // set the local name peripheral advertises
28
29 blePeripheral.setLocalName("LEDCB");
30
31 // set the UUID for the service this peripheral advertises
32
33 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
34
35 // add service and characteristic
36
37 blePeripheral.addAttribute(ledService);
38
39 blePeripheral.addAttribute(switchChar);
40
41 // assign event handlers for connected, disconnected to peripheral
42
43 blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
44
45 blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
46
47 // assign event handlers for characteristic
48
49 switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
50// set an initial value for the characteristic
51
52 switchChar.setValue(0);
53
54 // advertise the service
55
56 blePeripheral.begin();
57
58 Serial.println(("Bluetooth® device active, waiting for connections..."));
59}
60
61void loop() {
62
63 // poll peripheral
64
65 blePeripheral.poll();
66}
67
68void blePeripheralConnectHandler(BLECentral& central) {
69
70 // central connected event handler
71
72 Serial.print("Connected event, central: ");
73
74 Serial.println(central.address());
75}
76
77void blePeripheralDisconnectHandler(BLECentral& central) {
78
79 // central disconnected event handler
80
81 Serial.print("Disconnected event, central: ");
82
83 Serial.println(central.address());
84}
85
86void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
87
88 // central wrote new value to characteristic, update LED
89
90 Serial.print("Characteristic event, written: ");
91
92 if (switchChar.value()) {
93
94 Serial.println("LED on");
95
96 digitalWrite(ledPin, HIGH);
97
98 } else {
99
100 Serial.println("LED off");
101
102 digitalWrite(ledPin, LOW);
103
104 }
105}
106
107/*
108
109 Copyright (c) 2016 Intel Corporation. All rights reserved.
110
111 This library is free software; you can redistribute it and/or
112
113 modify it under the terms of the GNU Lesser General Public
114
115 License as published by the Free Software Foundation; either
116
117 version 2.1 of the License, or (at your option) any later version.
118
119 This library is distributed in the hope that it will be useful,
120
121 but WITHOUT ANY WARRANTY; without even the implied warranty of
122
123 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
124
125 Lesser General Public License for more details.
126
127 You should have received a copy of the GNU Lesser General Public
128
129 License along with this library; if not, write to the Free Software
130
131 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
132
133 1301 USA
134
135*/

Last revision 2016/04/05 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.