This program demonstrate how to apply BLE_nRF8001 library on mbed platform working with RedBearLab BLE Shield v2.1 or above.

Dependencies:   BLE_nRF8001 mbed

The application works with the BLEController iOS/Android App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal. You have to change the belowing constructs manually corresponding to the platform you are using.

SPI spi(PTD2, PTD3, PTD1);

DigitalInOut BLE_RDY(PTA12);

DigitalInOut BLE_REQ(PTA2);

Committer:
RedBearLab
Date:
Wed Oct 22 04:25:26 2014 +0000
Revision:
0:c9d37cf1551c
Firt commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 0:c9d37cf1551c 1 /*
RedBearLab 0:c9d37cf1551c 2
RedBearLab 0:c9d37cf1551c 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 0:c9d37cf1551c 4
RedBearLab 0:c9d37cf1551c 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 0:c9d37cf1551c 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 0:c9d37cf1551c 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 0:c9d37cf1551c 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 0:c9d37cf1551c 9 subject to the following conditions:
RedBearLab 0:c9d37cf1551c 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 0:c9d37cf1551c 11
RedBearLab 0:c9d37cf1551c 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 0:c9d37cf1551c 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 0:c9d37cf1551c 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 0:c9d37cf1551c 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 0:c9d37cf1551c 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 0:c9d37cf1551c 17
RedBearLab 0:c9d37cf1551c 18 */
RedBearLab 0:c9d37cf1551c 19
RedBearLab 0:c9d37cf1551c 20 /*
RedBearLab 0:c9d37cf1551c 21 * The application works with the BLEController iOS/Android App.
RedBearLab 0:c9d37cf1551c 22 * Type something from the Terminal to send
RedBearLab 0:c9d37cf1551c 23 * to the BLEController App or vice verse.
RedBearLab 0:c9d37cf1551c 24 * Characteristics received from App will print on Terminal.
RedBearLab 0:c9d37cf1551c 25 */
RedBearLab 0:c9d37cf1551c 26
RedBearLab 0:c9d37cf1551c 27 // Import libraries
RedBearLab 0:c9d37cf1551c 28 #include "Arduino.h"
RedBearLab 0:c9d37cf1551c 29 #include "BLEPeripheral.h"
RedBearLab 0:c9d37cf1551c 30
RedBearLab 0:c9d37cf1551c 31 Serial serial(USBTX, USBRX);
RedBearLab 0:c9d37cf1551c 32
RedBearLab 0:c9d37cf1551c 33 // The SPI construct, REQN and RDYN IO construct should be modified manually
RedBearLab 0:c9d37cf1551c 34 // It depend on the board you are using and the REQN&RDYN configuration on BLE Shield
RedBearLab 0:c9d37cf1551c 35 SPI spi(PTD2, PTD3, PTD1);
RedBearLab 0:c9d37cf1551c 36 DigitalInOut BLE_RDY(PTA12);
RedBearLab 0:c9d37cf1551c 37 DigitalInOut BLE_REQ(PTA2);
RedBearLab 0:c9d37cf1551c 38
RedBearLab 0:c9d37cf1551c 39 unsigned char txbuf[16] = {0};
RedBearLab 0:c9d37cf1551c 40 unsigned char txlen = 0;
RedBearLab 0:c9d37cf1551c 41
RedBearLab 0:c9d37cf1551c 42 /*----- BLE Utility -------------------------------------------------------------------------*/
RedBearLab 0:c9d37cf1551c 43 // create peripheral instance, see pinouts above
RedBearLab 0:c9d37cf1551c 44 BLEPeripheral blePeripheral = BLEPeripheral(&BLE_REQ, &BLE_RDY, NULL);
RedBearLab 0:c9d37cf1551c 45
RedBearLab 0:c9d37cf1551c 46 // create service
RedBearLab 0:c9d37cf1551c 47 BLEService uartService = BLEService("713d0000503e4c75ba943148f18d941e");
RedBearLab 0:c9d37cf1551c 48
RedBearLab 0:c9d37cf1551c 49 // create characteristic
RedBearLab 0:c9d37cf1551c 50 BLECharacteristic txCharacteristic = BLECharacteristic("713d0002503e4c75ba943148f18d941e", BLENotify, 20);
RedBearLab 0:c9d37cf1551c 51 BLECharacteristic rxCharacteristic = BLECharacteristic("713d0003503e4c75ba943148f18d941e", BLEWriteWithoutResponse, 20);
RedBearLab 0:c9d37cf1551c 52 /*--------------------------------------------------------------------------------------------*/
RedBearLab 0:c9d37cf1551c 53
RedBearLab 0:c9d37cf1551c 54 unsigned int interval = 0;
RedBearLab 0:c9d37cf1551c 55 unsigned char count_on = 0;
RedBearLab 0:c9d37cf1551c 56
RedBearLab 0:c9d37cf1551c 57 int main()
RedBearLab 0:c9d37cf1551c 58 {
RedBearLab 0:c9d37cf1551c 59 serial.baud(115200);
RedBearLab 0:c9d37cf1551c 60 serial.printf("Serial begin!\r\n");
RedBearLab 0:c9d37cf1551c 61
RedBearLab 0:c9d37cf1551c 62 /*----- BLE Utility ---------------------------------------------*/
RedBearLab 0:c9d37cf1551c 63 // set advertised local name and service UUID
RedBearLab 0:c9d37cf1551c 64 blePeripheral.setLocalName("BLE Shield");
RedBearLab 0:c9d37cf1551c 65
RedBearLab 0:c9d37cf1551c 66 blePeripheral.setAdvertisedServiceUuid(uartService.uuid());
RedBearLab 0:c9d37cf1551c 67
RedBearLab 0:c9d37cf1551c 68 // add service and characteristic
RedBearLab 0:c9d37cf1551c 69 blePeripheral.addAttribute(uartService);
RedBearLab 0:c9d37cf1551c 70 blePeripheral.addAttribute(rxCharacteristic);
RedBearLab 0:c9d37cf1551c 71 blePeripheral.addAttribute(txCharacteristic);
RedBearLab 0:c9d37cf1551c 72
RedBearLab 0:c9d37cf1551c 73 // begin initialization
RedBearLab 0:c9d37cf1551c 74 blePeripheral.begin();
RedBearLab 0:c9d37cf1551c 75 /*---------------------------------------------------------------*/
RedBearLab 0:c9d37cf1551c 76
RedBearLab 0:c9d37cf1551c 77 serial.printf("BLE UART Peripheral begin!\r\n");
RedBearLab 0:c9d37cf1551c 78
RedBearLab 0:c9d37cf1551c 79 while(1)
RedBearLab 0:c9d37cf1551c 80 {
RedBearLab 0:c9d37cf1551c 81 BLECentral central = blePeripheral.central();
RedBearLab 0:c9d37cf1551c 82
RedBearLab 0:c9d37cf1551c 83 if (central)
RedBearLab 0:c9d37cf1551c 84 {
RedBearLab 0:c9d37cf1551c 85 // central connected to peripheral
RedBearLab 0:c9d37cf1551c 86 serial.printf("Connected to central\r\n");
RedBearLab 0:c9d37cf1551c 87
RedBearLab 0:c9d37cf1551c 88 while (central.connected())
RedBearLab 0:c9d37cf1551c 89 {
RedBearLab 0:c9d37cf1551c 90 // central still connected to peripheral
RedBearLab 0:c9d37cf1551c 91 if (rxCharacteristic.written())
RedBearLab 0:c9d37cf1551c 92 {
RedBearLab 0:c9d37cf1551c 93 unsigned char rxlen = rxCharacteristic.valueLength();
RedBearLab 0:c9d37cf1551c 94 const unsigned char *val = rxCharacteristic.value();
RedBearLab 0:c9d37cf1551c 95 serial.printf("didCharacteristicWritten, Length: %d\r\n", rxlen);
RedBearLab 0:c9d37cf1551c 96 unsigned char i = 0;
RedBearLab 0:c9d37cf1551c 97 while(i<rxlen)
RedBearLab 0:c9d37cf1551c 98 {
RedBearLab 0:c9d37cf1551c 99 serial.printf("%d, ",val[i++]);
RedBearLab 0:c9d37cf1551c 100 }
RedBearLab 0:c9d37cf1551c 101 serial.printf("\r\n");
RedBearLab 0:c9d37cf1551c 102 }
RedBearLab 0:c9d37cf1551c 103
RedBearLab 0:c9d37cf1551c 104 if(serial.readable()) // Do not spend much time on doing other things when serial available! Otherwisee, data will lose.
RedBearLab 0:c9d37cf1551c 105 {
RedBearLab 0:c9d37cf1551c 106 if(!count_on)
RedBearLab 0:c9d37cf1551c 107 {
RedBearLab 0:c9d37cf1551c 108 count_on = 1;
RedBearLab 0:c9d37cf1551c 109 }
RedBearLab 0:c9d37cf1551c 110 interval = 0;
RedBearLab 0:c9d37cf1551c 111 txbuf[txlen] = serial.getc();
RedBearLab 0:c9d37cf1551c 112 txlen++;
RedBearLab 0:c9d37cf1551c 113 }
RedBearLab 0:c9d37cf1551c 114
RedBearLab 0:c9d37cf1551c 115 if(count_on) // Count the interval after receiving a new char from terminate
RedBearLab 0:c9d37cf1551c 116 {
RedBearLab 0:c9d37cf1551c 117 interval++;
RedBearLab 0:c9d37cf1551c 118 }
RedBearLab 0:c9d37cf1551c 119
RedBearLab 0:c9d37cf1551c 120 if(interval == 10) // If there is no char available last the interval, send the received chars to central.
RedBearLab 0:c9d37cf1551c 121 {
RedBearLab 0:c9d37cf1551c 122 interval = 0;
RedBearLab 0:c9d37cf1551c 123 count_on = 0;
RedBearLab 0:c9d37cf1551c 124
RedBearLab 0:c9d37cf1551c 125 serial.printf("Received from terminal: %d bytes\r\n", txlen);
RedBearLab 0:c9d37cf1551c 126 txCharacteristic.setValue((const unsigned char *)txbuf, txlen);
RedBearLab 0:c9d37cf1551c 127 txlen = 0;
RedBearLab 0:c9d37cf1551c 128 }
RedBearLab 0:c9d37cf1551c 129 }
RedBearLab 0:c9d37cf1551c 130
RedBearLab 0:c9d37cf1551c 131 // central disconnected
RedBearLab 0:c9d37cf1551c 132 serial.printf("Disconnected from central\r\n");
RedBearLab 0:c9d37cf1551c 133 }
RedBearLab 0:c9d37cf1551c 134 }
RedBearLab 0:c9d37cf1551c 135 }