Adafruit Bluefruit with KL25Z

Dependencies:   BLE_nRF8001 mbed

Simple implementation of UART on FRDM KL25Z board with nRF8001 Adafruit breakout. Uses RedBearLabs Library and could be made to work with their BLE Shield.

UART Characteristics can be changed to work with either RedBearLabs BLE Controller app or using the Nordic settings the nRF apps or the Adafruit Bluefruit LE apps.

Committer:
highroads
Date:
Thu Mar 09 23:06:15 2017 +0000
Revision:
3:b5dc2454dfc3
Parent:
2:e6d7390d4429
Child:
4:fb6008860882
Added uuid for redbearlabs and nordic apps

Who changed what in which revision?

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