This program demonstrate how to apply BLE_nRF8001 library on mbed platform working with RedBearLab BLE Shield v2.1 or above. Works on Nucleo F401RE with the 2012.07 shield. Need to press reset

Dependencies:   BLE_nRF8001 mbed

Fork of nRF8001_SimpleChat by RedBearLab

Committer:
highroads
Date:
Thu Feb 11 21:14:14 2016 +0000
Revision:
1:9c7f54eff5de
Parent:
0:c9d37cf1551c
Working with shield 2012.V7

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
highroads 1:9c7f54eff5de 35 // config for Nucleo 401RE
highroads 1:9c7f54eff5de 36 SPI spi(PA_7, PA_6, PA_5);
highroads 1:9c7f54eff5de 37 DigitalInOut BLE_RDY(PA_9); // D8
highroads 1:9c7f54eff5de 38 DigitalInOut BLE_REQ(PC_7); // D9
RedBearLab 0:c9d37cf1551c 39
RedBearLab 0:c9d37cf1551c 40 unsigned char txbuf[16] = {0};
RedBearLab 0:c9d37cf1551c 41 unsigned char txlen = 0;
RedBearLab 0:c9d37cf1551c 42
RedBearLab 0:c9d37cf1551c 43 /*----- BLE Utility -------------------------------------------------------------------------*/
RedBearLab 0:c9d37cf1551c 44 // create peripheral instance, see pinouts above
RedBearLab 0:c9d37cf1551c 45 BLEPeripheral blePeripheral = BLEPeripheral(&BLE_REQ, &BLE_RDY, NULL);
RedBearLab 0:c9d37cf1551c 46
RedBearLab 0:c9d37cf1551c 47 // create service
RedBearLab 0:c9d37cf1551c 48 BLEService uartService = BLEService("713d0000503e4c75ba943148f18d941e");
RedBearLab 0:c9d37cf1551c 49
RedBearLab 0:c9d37cf1551c 50 // create characteristic
RedBearLab 0:c9d37cf1551c 51 BLECharacteristic txCharacteristic = BLECharacteristic("713d0002503e4c75ba943148f18d941e", BLENotify, 20);
RedBearLab 0:c9d37cf1551c 52 BLECharacteristic rxCharacteristic = BLECharacteristic("713d0003503e4c75ba943148f18d941e", BLEWriteWithoutResponse, 20);
RedBearLab 0:c9d37cf1551c 53 /*--------------------------------------------------------------------------------------------*/
RedBearLab 0:c9d37cf1551c 54
RedBearLab 0:c9d37cf1551c 55 unsigned int interval = 0;
RedBearLab 0:c9d37cf1551c 56 unsigned char count_on = 0;
RedBearLab 0:c9d37cf1551c 57
RedBearLab 0:c9d37cf1551c 58 int main()
RedBearLab 0:c9d37cf1551c 59 {
RedBearLab 0:c9d37cf1551c 60 serial.baud(115200);
RedBearLab 0:c9d37cf1551c 61 serial.printf("Serial begin!\r\n");
RedBearLab 0:c9d37cf1551c 62
RedBearLab 0:c9d37cf1551c 63 /*----- BLE Utility ---------------------------------------------*/
RedBearLab 0:c9d37cf1551c 64 // set advertised local name and service UUID
RedBearLab 0:c9d37cf1551c 65 blePeripheral.setLocalName("BLE Shield");
RedBearLab 0:c9d37cf1551c 66
RedBearLab 0:c9d37cf1551c 67 blePeripheral.setAdvertisedServiceUuid(uartService.uuid());
RedBearLab 0:c9d37cf1551c 68
RedBearLab 0:c9d37cf1551c 69 // add service and characteristic
RedBearLab 0:c9d37cf1551c 70 blePeripheral.addAttribute(uartService);
RedBearLab 0:c9d37cf1551c 71 blePeripheral.addAttribute(rxCharacteristic);
RedBearLab 0:c9d37cf1551c 72 blePeripheral.addAttribute(txCharacteristic);
RedBearLab 0:c9d37cf1551c 73
RedBearLab 0:c9d37cf1551c 74 // begin initialization
RedBearLab 0:c9d37cf1551c 75 blePeripheral.begin();
RedBearLab 0:c9d37cf1551c 76 /*---------------------------------------------------------------*/
RedBearLab 0:c9d37cf1551c 77
RedBearLab 0:c9d37cf1551c 78 serial.printf("BLE UART Peripheral begin!\r\n");
RedBearLab 0:c9d37cf1551c 79
RedBearLab 0:c9d37cf1551c 80 while(1)
RedBearLab 0:c9d37cf1551c 81 {
RedBearLab 0:c9d37cf1551c 82 BLECentral central = blePeripheral.central();
RedBearLab 0:c9d37cf1551c 83
RedBearLab 0:c9d37cf1551c 84 if (central)
RedBearLab 0:c9d37cf1551c 85 {
RedBearLab 0:c9d37cf1551c 86 // central connected to peripheral
RedBearLab 0:c9d37cf1551c 87 serial.printf("Connected to central\r\n");
RedBearLab 0:c9d37cf1551c 88
RedBearLab 0:c9d37cf1551c 89 while (central.connected())
RedBearLab 0:c9d37cf1551c 90 {
RedBearLab 0:c9d37cf1551c 91 // central still connected to peripheral
RedBearLab 0:c9d37cf1551c 92 if (rxCharacteristic.written())
RedBearLab 0:c9d37cf1551c 93 {
RedBearLab 0:c9d37cf1551c 94 unsigned char rxlen = rxCharacteristic.valueLength();
RedBearLab 0:c9d37cf1551c 95 const unsigned char *val = rxCharacteristic.value();
RedBearLab 0:c9d37cf1551c 96 serial.printf("didCharacteristicWritten, Length: %d\r\n", rxlen);
RedBearLab 0:c9d37cf1551c 97 unsigned char i = 0;
RedBearLab 0:c9d37cf1551c 98 while(i<rxlen)
RedBearLab 0:c9d37cf1551c 99 {
RedBearLab 0:c9d37cf1551c 100 serial.printf("%d, ",val[i++]);
RedBearLab 0:c9d37cf1551c 101 }
RedBearLab 0:c9d37cf1551c 102 serial.printf("\r\n");
RedBearLab 0:c9d37cf1551c 103 }
RedBearLab 0:c9d37cf1551c 104
RedBearLab 0:c9d37cf1551c 105 if(serial.readable()) // Do not spend much time on doing other things when serial available! Otherwisee, data will lose.
RedBearLab 0:c9d37cf1551c 106 {
RedBearLab 0:c9d37cf1551c 107 if(!count_on)
RedBearLab 0:c9d37cf1551c 108 {
RedBearLab 0:c9d37cf1551c 109 count_on = 1;
RedBearLab 0:c9d37cf1551c 110 }
RedBearLab 0:c9d37cf1551c 111 interval = 0;
RedBearLab 0:c9d37cf1551c 112 txbuf[txlen] = serial.getc();
RedBearLab 0:c9d37cf1551c 113 txlen++;
RedBearLab 0:c9d37cf1551c 114 }
RedBearLab 0:c9d37cf1551c 115
RedBearLab 0:c9d37cf1551c 116 if(count_on) // Count the interval after receiving a new char from terminate
RedBearLab 0:c9d37cf1551c 117 {
RedBearLab 0:c9d37cf1551c 118 interval++;
RedBearLab 0:c9d37cf1551c 119 }
RedBearLab 0:c9d37cf1551c 120
RedBearLab 0:c9d37cf1551c 121 if(interval == 10) // If there is no char available last the interval, send the received chars to central.
RedBearLab 0:c9d37cf1551c 122 {
RedBearLab 0:c9d37cf1551c 123 interval = 0;
RedBearLab 0:c9d37cf1551c 124 count_on = 0;
RedBearLab 0:c9d37cf1551c 125
RedBearLab 0:c9d37cf1551c 126 serial.printf("Received from terminal: %d bytes\r\n", txlen);
RedBearLab 0:c9d37cf1551c 127 txCharacteristic.setValue((const unsigned char *)txbuf, txlen);
RedBearLab 0:c9d37cf1551c 128 txlen = 0;
RedBearLab 0:c9d37cf1551c 129 }
RedBearLab 0:c9d37cf1551c 130 }
RedBearLab 0:c9d37cf1551c 131
RedBearLab 0:c9d37cf1551c 132 // central disconnected
RedBearLab 0:c9d37cf1551c 133 serial.printf("Disconnected from central\r\n");
RedBearLab 0:c9d37cf1551c 134 }
RedBearLab 0:c9d37cf1551c 135 }
RedBearLab 0:c9d37cf1551c 136 }