Demonstration code showing how to control an LED over Bluetooth Low Energy using the UARTService

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

This programme demonstrates the use of the UARTService to send command strings over Bluetooth Low Energy (BLE) to control devices attached to the nRF51822 board.

In this example you can use a BLE connection from smart phone to turn on and off LED2 on the board.

Sending the character string "led2 on" will turn the LED on and "led2 off" will turn it off.

The command that is received is also echoed back via BLE to the sending device.

You can use the nRF UART App on and Android or iOS smart phone with BLE to test this out https://www.nordicsemi.com/eng/Products/nRFready-Demo-APPS

I have also ported the source code for the Android app into Android studio which should be a good starting point for developing your own custom App https://github.com/bennthomsen/nRF_UART. If you want to start using this within Android Studio create for own fork in GitHub and then from within Android Studio VCS>Checkout from Version Control>GitHub

Committer:
djmannion
Date:
Fri Jan 02 15:15:54 2015 +0000
Revision:
15:2a9916011a2a
Parent:
14:8ff3bfb6b19d
Line 85: was edited to include:
; ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED| GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
;
; This allowed the UART app to connect to the board successfully.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:e910d9bb040f 1 /* mbed Microcontroller Library
yihui 0:e910d9bb040f 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:e910d9bb040f 3 *
yihui 0:e910d9bb040f 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:e910d9bb040f 5 * you may not use this file except in compliance with the License.
yihui 0:e910d9bb040f 6 * You may obtain a copy of the License at
yihui 0:e910d9bb040f 7 *
yihui 0:e910d9bb040f 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:e910d9bb040f 9 *
yihui 0:e910d9bb040f 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:e910d9bb040f 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:e910d9bb040f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:e910d9bb040f 13 * See the License for the specific language governing permissions and
yihui 0:e910d9bb040f 14 * limitations under the License.
yihui 0:e910d9bb040f 15 */
yihui 0:e910d9bb040f 16
yihui 0:e910d9bb040f 17 #include "mbed.h"
Rohit Grover 2:e060367b9024 18 #include "BLEDevice.h"
yihui 0:e910d9bb040f 19
rgrover1 6:e0fc9072e853 20 #include "UARTService.h"
yihui 0:e910d9bb040f 21
bthomsen 14:8ff3bfb6b19d 22 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
Rohit Grover 2:e060367b9024 23 * it will have an impact on code-size and power consumption. */
yihui 0:e910d9bb040f 24
Rohit Grover 2:e060367b9024 25 #if NEED_CONSOLE_OUTPUT
rgrover1 6:e0fc9072e853 26 #define DEBUG(...) { printf(__VA_ARGS__); }
yihui 0:e910d9bb040f 27 #else
Rohit Grover 2:e060367b9024 28 #define DEBUG(...) /* nothing */
Rohit Grover 2:e060367b9024 29 #endif /* #if NEED_CONSOLE_OUTPUT */
yihui 0:e910d9bb040f 30
bthomsen 13:291a8f177400 31 #define LED2OFF "led2 off"
bthomsen 13:291a8f177400 32 #define LED2ON "led2 on"
bthomsen 13:291a8f177400 33
bthomsen 13:291a8f177400 34
bthomsen 13:291a8f177400 35 //char rxPayload[CMD_SIZE];
bthomsen 13:291a8f177400 36
bthomsen 13:291a8f177400 37 BLEDevice ble; // Create Bluetooth object
bthomsen 13:291a8f177400 38 DigitalOut led1(LED1); // Set the pin attached to LED1 as an output
bthomsen 13:291a8f177400 39 DigitalOut led2(LED2); // Set the pin attached to LED2 as an output
yihui 0:e910d9bb040f 40
rgrover1 6:e0fc9072e853 41 UARTService *uartServicePtr;
yihui 0:e910d9bb040f 42
bthomsen 13:291a8f177400 43 /* BLE disconnected callback */
rgrover1 5:4bc41267a03a 44 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yihui 0:e910d9bb040f 45 {
Rohit Grover 2:e060367b9024 46 DEBUG("Disconnected!\n\r");
Rohit Grover 2:e060367b9024 47 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 2:e060367b9024 48 ble.startAdvertising();
Rohit Grover 2:e060367b9024 49 }
bthomsen 13:291a8f177400 50 /* BLE UART data received callback */
rgrover1 6:e0fc9072e853 51 void onDataWritten(const GattCharacteristicWriteCBParams *params)
yihui 0:e910d9bb040f 52 {
bthomsen 13:291a8f177400 53 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { //If characters received over BLE
rgrover1 5:4bc41267a03a 54 uint16_t bytesRead = params->len;
rgrover1 5:4bc41267a03a 55 DEBUG("received %u bytes\n\r", bytesRead);
bthomsen 13:291a8f177400 56 DEBUG("Received string: '");
bthomsen 13:291a8f177400 57 DEBUG((const char *)params->data); //Note the size of data expands to the largest string received. Need to use bytesRead to resize.
bthomsen 13:291a8f177400 58 DEBUG("'\n\r");
bthomsen 13:291a8f177400 59 if (!strncmp(LED2ON,(const char *)params->data,bytesRead)) led2 = 1; // If the received and command string are equal turn led2 on. Note strcmp returns 0 if strings are equal
bthomsen 13:291a8f177400 60 if (!strncmp(LED2OFF,(const char *)params->data,bytesRead)) led2 = 0; // If the received and command string are equal turn led2 off.
bthomsen 13:291a8f177400 61 //Toggle LED2 when data received
bthomsen 13:291a8f177400 62 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); // Echo received characters back over BLE
yihui 0:e910d9bb040f 63 }
Rohit Grover 2:e060367b9024 64 }
yihui 0:e910d9bb040f 65
bthomsen 12:36a9f01cfa0a 66 /* Periodic Ticker callback */
Rohit Grover 2:e060367b9024 67 void periodicCallback(void)
Rohit Grover 2:e060367b9024 68 {
bthomsen 12:36a9f01cfa0a 69 led1 = !led1; // Toggle LED 1
Rohit Grover 2:e060367b9024 70 }
yihui 0:e910d9bb040f 71
yihui 0:e910d9bb040f 72 int main(void)
yihui 0:e910d9bb040f 73 {
Rohit Grover 2:e060367b9024 74 led1 = 1;
bthomsen 13:291a8f177400 75 led2 = 0;
bthomsen 12:36a9f01cfa0a 76 Ticker ticker; // Create period timer
bthomsen 12:36a9f01cfa0a 77 ticker.attach(periodicCallback, 1); // Attach ticker callback function with a period of 1 second
yihui 0:e910d9bb040f 78
Rohit Grover 2:e060367b9024 79 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 2:e060367b9024 80 ble.init();
bthomsen 13:291a8f177400 81 ble.onDisconnection(disconnectionCallback); // Define callback function for BLE disconnection event
bthomsen 13:291a8f177400 82 ble.onDataWritten(onDataWritten); // Define callback function for BLE Data received event
yihui 0:e910d9bb040f 83
Rohit Grover 2:e060367b9024 84 /* setup advertising */
djmannion 15:2a9916011a2a 85 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED| GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // Indicate that Legacy Bluetooth in not supported
Rohit Grover 2:e060367b9024 86 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 87 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 6:e0fc9072e853 88 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
Rohit Grover 2:e060367b9024 89 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 90 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
yihui 0:e910d9bb040f 91
bthomsen 13:291a8f177400 92 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); // Set advertising interval to 1 second
bthomsen 13:291a8f177400 93 ble.startAdvertising(); // Start advertising
yihui 0:e910d9bb040f 94
bthomsen 13:291a8f177400 95 UARTService uartService(ble); // Create BLE UART service
bthomsen 13:291a8f177400 96 uartServicePtr = &uartService; // Initalise pointer to point to UART Service
yihui 0:e910d9bb040f 97
Rohit Grover 2:e060367b9024 98 while (true) {
bthomsen 13:291a8f177400 99 ble.waitForEvent(); // Wait for BLE events
yihui 0:e910d9bb040f 100 }
yihui 0:e910d9bb040f 101 }