This is a simple program to work with RedBearLab BLE Controller App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal.

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_Chat_Softswitch by david bateman

Committer:
speedave
Date:
Sun Nov 12 18:28:00 2017 +0000
Revision:
4:5868aec17035
Parent:
3:b3f6c612b603
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:1c058e553423 1 /*
RedBearLab 0:cffe8ac1bdf0 2
RedBearLab 1:1c058e553423 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:1c058e553423 4
RedBearLab 1:1c058e553423 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:1c058e553423 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:1c058e553423 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:1c058e553423 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:1c058e553423 9 subject to the following conditions:
RedBearLab 1:1c058e553423 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:1c058e553423 11
RedBearLab 1:1c058e553423 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:1c058e553423 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:1c058e553423 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:1c058e553423 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:1c058e553423 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:1c058e553423 17
RedBearLab 1:1c058e553423 18 */
RedBearLab 1:1c058e553423 19
RedBearLab 1:1c058e553423 20 /*
RedBearLab 1:1c058e553423 21 * The application works with the BLEController iOS/Android App.
RedBearLab 1:1c058e553423 22 * Type something from the Terminal to send
RedBearLab 1:1c058e553423 23 * to the BLEController App or vice verse.
RedBearLab 1:1c058e553423 24 * Characteristics received from App will print on Terminal.
RedBearLab 1:1c058e553423 25 */
RedBearLab 1:1c058e553423 26
RedBearLab 0:cffe8ac1bdf0 27 #include "mbed.h"
RedBearLab 2:4b66b69c7ecb 28 #include "ble/BLE.h"
speedave 4:5868aec17035 29 #include <stdlib.h>
speedave 4:5868aec17035 30 #include <stdio.h>
RedBearLab 0:cffe8ac1bdf0 31
RedBearLab 0:cffe8ac1bdf0 32
RedBearLab 0:cffe8ac1bdf0 33 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 34 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 35 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 36
RedBearLab 0:cffe8ac1bdf0 37 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 38
RedBearLab 2:4b66b69c7ecb 39 BLE ble;
RedBearLab 0:cffe8ac1bdf0 40
RedBearLab 0:cffe8ac1bdf0 41 Serial pc(USBTX, USBRX);
RedBearLab 0:cffe8ac1bdf0 42
RedBearLab 0:cffe8ac1bdf0 43
RedBearLab 0:cffe8ac1bdf0 44 // The Nordic UART Service
RedBearLab 0:cffe8ac1bdf0 45 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 46 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 47 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 48 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:cffe8ac1bdf0 49
RedBearLab 0:cffe8ac1bdf0 50
RedBearLab 0:cffe8ac1bdf0 51 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 52 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 53
RedBearLab 0:cffe8ac1bdf0 54 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 55 static uint8_t rx_len=0;
RedBearLab 0:cffe8ac1bdf0 56
RedBearLab 0:cffe8ac1bdf0 57
RedBearLab 0:cffe8ac1bdf0 58 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
RedBearLab 0:cffe8ac1bdf0 59
RedBearLab 0:cffe8ac1bdf0 60 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 61
RedBearLab 0:cffe8ac1bdf0 62 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 63
RedBearLab 0:cffe8ac1bdf0 64 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 65
speedave 4:5868aec17035 66 int counter=10;
speedave 4:5868aec17035 67 int counter2=0;
RedBearLab 0:cffe8ac1bdf0 68
RedBearLab 3:b3f6c612b603 69 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
RedBearLab 0:cffe8ac1bdf0 70 {
RedBearLab 0:cffe8ac1bdf0 71 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 72 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 73 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 74 }
RedBearLab 0:cffe8ac1bdf0 75
RedBearLab 2:4b66b69c7ecb 76 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 77 {
RedBearLab 0:cffe8ac1bdf0 78 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 79 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 80
RedBearLab 2:4b66b69c7ecb 81 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 82 {
RedBearLab 0:cffe8ac1bdf0 83 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 84 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 85 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 86 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 87 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 88 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 89 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 90 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 91 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 92 {
RedBearLab 0:cffe8ac1bdf0 93 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 94 }
RedBearLab 0:cffe8ac1bdf0 95 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 96 }
RedBearLab 0:cffe8ac1bdf0 97 }
RedBearLab 0:cffe8ac1bdf0 98
speedave 4:5868aec17035 99 /* serial interupt (from PC) calls this function */
RedBearLab 0:cffe8ac1bdf0 100 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 101 {
speedave 4:5868aec17035 102 rx_len = 0;
speedave 4:5868aec17035 103 /* while text in the serial buffer from the PC */
RedBearLab 0:cffe8ac1bdf0 104 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 105 {
speedave 4:5868aec17035 106 /* load characters from serial buffer into rx_buf array, one by one */
speedave 4:5868aec17035 107 rx_buf[rx_len++] = pc.getc();
speedave 4:5868aec17035 108 /*check if rx_buf array is too long, ends in 'space' or ends 'carrage return' */
RedBearLab 0:cffe8ac1bdf0 109 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 110 {
speedave 4:5868aec17035 111 /* update the rxCharacteristic with the rx_buf data fromm the PC */
RedBearLab 0:cffe8ac1bdf0 112 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
speedave 4:5868aec17035 113 /* print debug info and reset */
RedBearLab 0:cffe8ac1bdf0 114 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 115 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 116 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 117 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 118 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 119 break;
RedBearLab 0:cffe8ac1bdf0 120 }
RedBearLab 0:cffe8ac1bdf0 121 }
RedBearLab 0:cffe8ac1bdf0 122 }
RedBearLab 0:cffe8ac1bdf0 123
speedave 4:5868aec17035 124 void dummyticker(void) {
speedave 4:5868aec17035 125
speedave 4:5868aec17035 126 rx_len = 0;
speedave 4:5868aec17035 127 char counterString[2];
speedave 4:5868aec17035 128
speedave 4:5868aec17035 129 counter2++;
speedave 4:5868aec17035 130 if (counter2 > 2){
speedave 4:5868aec17035 131 counter2 = 1;
speedave 4:5868aec17035 132 }
speedave 4:5868aec17035 133
speedave 4:5868aec17035 134 /* this could be done more efficiently using sprinf */
speedave 4:5868aec17035 135 if ( counter2 == 1) {
speedave 4:5868aec17035 136 pc.printf("press1\n");
speedave 4:5868aec17035 137 /* this only seems to work with exactly 20 characters */
speedave 4:5868aec17035 138 char rx_message[] = "SWITCH1-901234567-";
speedave 4:5868aec17035 139 sprintf(counterString, "%d\n", counter);
speedave 4:5868aec17035 140 strncat(rx_message, counterString,20);
speedave 4:5868aec17035 141 pc.printf(rx_message);
speedave 4:5868aec17035 142
speedave 4:5868aec17035 143 while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
speedave 4:5868aec17035 144
speedave 4:5868aec17035 145 rx_buf[rx_len] = (int) rx_message[rx_len++];
speedave 4:5868aec17035 146 }
speedave 4:5868aec17035 147
speedave 4:5868aec17035 148 }
speedave 4:5868aec17035 149 if ( counter2 == 2) {
speedave 4:5868aec17035 150 pc.printf("press2\n");
speedave 4:5868aec17035 151 char rx_message[] = "SWITCH2-901234567-";
speedave 4:5868aec17035 152 sprintf(counterString, "%d\n", counter);
speedave 4:5868aec17035 153 strncat(rx_message, counterString,20);
speedave 4:5868aec17035 154 pc.printf(rx_message);
speedave 4:5868aec17035 155
speedave 4:5868aec17035 156 while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
speedave 4:5868aec17035 157
speedave 4:5868aec17035 158 rx_buf[rx_len] = (int) rx_message[rx_len++];
speedave 4:5868aec17035 159 }
speedave 4:5868aec17035 160 }
speedave 4:5868aec17035 161
speedave 4:5868aec17035 162 if ( counter2 == 3) {
speedave 4:5868aec17035 163 pc.printf("press2\n");
speedave 4:5868aec17035 164 char rx_message[] = "SWITCH2-901234567-";
speedave 4:5868aec17035 165 sprintf(counterString, "%d\n", counter);
speedave 4:5868aec17035 166 strncat(rx_message, counterString,20);
speedave 4:5868aec17035 167 pc.printf(rx_message);
speedave 4:5868aec17035 168
speedave 4:5868aec17035 169 while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
speedave 4:5868aec17035 170
speedave 4:5868aec17035 171 rx_buf[rx_len] = (int) rx_message[rx_len++];
speedave 4:5868aec17035 172 }
speedave 4:5868aec17035 173 }
speedave 4:5868aec17035 174
speedave 4:5868aec17035 175 if ( counter2 == 4) {
speedave 4:5868aec17035 176 pc.printf("press2\n");
speedave 4:5868aec17035 177 char rx_message[] = "SWITCH2-901234567-";
speedave 4:5868aec17035 178 sprintf(counterString, "%d\n", counter);
speedave 4:5868aec17035 179 strncat(rx_message, counterString,20);
speedave 4:5868aec17035 180 pc.printf(rx_message);
speedave 4:5868aec17035 181
speedave 4:5868aec17035 182 while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
speedave 4:5868aec17035 183
speedave 4:5868aec17035 184 rx_buf[rx_len] = (int) rx_message[rx_len++];
speedave 4:5868aec17035 185 }
speedave 4:5868aec17035 186 }
speedave 4:5868aec17035 187
speedave 4:5868aec17035 188 if ( counter2 == 5) {
speedave 4:5868aec17035 189 pc.printf("press2\n");
speedave 4:5868aec17035 190 char rx_message[] = "SWITCH2-901234567-";
speedave 4:5868aec17035 191 sprintf(counterString, "%d\n", counter);
speedave 4:5868aec17035 192 strncat(rx_message, counterString,20);
speedave 4:5868aec17035 193 pc.printf(rx_message);
speedave 4:5868aec17035 194
speedave 4:5868aec17035 195 while(rx_len<20 && rx_buf[rx_len-1]!='\0' && rx_buf[rx_len-1]!='\n'){
speedave 4:5868aec17035 196
speedave 4:5868aec17035 197 rx_buf[rx_len] = (int) rx_message[rx_len++];
speedave 4:5868aec17035 198 }
speedave 4:5868aec17035 199 }
speedave 4:5868aec17035 200
speedave 4:5868aec17035 201 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
speedave 4:5868aec17035 202
speedave 4:5868aec17035 203 pc.printf("button press\n");
speedave 4:5868aec17035 204 pc.printf("Length: ");
speedave 4:5868aec17035 205 pc.putc(rx_len);
speedave 4:5868aec17035 206 pc.printf("\r\n");
speedave 4:5868aec17035 207 rx_len = 0;
speedave 4:5868aec17035 208 counter++;
speedave 4:5868aec17035 209 if (counter > 99) {
speedave 4:5868aec17035 210 counter = 0;
speedave 4:5868aec17035 211 }
speedave 4:5868aec17035 212 }
speedave 4:5868aec17035 213
RedBearLab 0:cffe8ac1bdf0 214 int main(void)
RedBearLab 0:cffe8ac1bdf0 215 {
RedBearLab 0:cffe8ac1bdf0 216 ble.init();
RedBearLab 0:cffe8ac1bdf0 217 ble.onDisconnection(disconnectionCallback);
speedave 4:5868aec17035 218
speedave 4:5868aec17035 219 /* bluetooth data interrupt */
speedave 4:5868aec17035 220 /* when data sent from client (phone) call 'WrittenHandler' */
RedBearLab 0:cffe8ac1bdf0 221 ble.onDataWritten(WrittenHandler);
RedBearLab 0:cffe8ac1bdf0 222
RedBearLab 0:cffe8ac1bdf0 223 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 224 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 225
speedave 4:5868aec17035 226 /* serial interupt*/
speedave 4:5868aec17035 227 /* when text typed into PC terminal call 'uartCB' */
RedBearLab 0:cffe8ac1bdf0 228 pc.attach( uartCB , pc.RxIrq);
speedave 4:5868aec17035 229
speedave 4:5868aec17035 230
speedave 4:5868aec17035 231
RedBearLab 0:cffe8ac1bdf0 232 // setup advertising
RedBearLab 0:cffe8ac1bdf0 233 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 234 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 235 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
RedBearLab 0:cffe8ac1bdf0 236 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:cffe8ac1bdf0 237 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 238 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 239 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 240 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 241
RedBearLab 0:cffe8ac1bdf0 242 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 243
RedBearLab 0:cffe8ac1bdf0 244 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 245 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 246
speedave 4:5868aec17035 247 /* ticker interupt */
speedave 4:5868aec17035 248 /* timer will call 'dummyticker' every X seconds */
speedave 4:5868aec17035 249 Ticker ticker;
speedave 4:5868aec17035 250 ticker.attach(dummyticker, 3);
speedave 4:5868aec17035 251
RedBearLab 0:cffe8ac1bdf0 252 while(1)
RedBearLab 0:cffe8ac1bdf0 253 {
RedBearLab 0:cffe8ac1bdf0 254 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 255 }
RedBearLab 0:cffe8ac1bdf0 256 }
RedBearLab 0:cffe8ac1bdf0 257
RedBearLab 0:cffe8ac1bdf0 258
speedave 4:5868aec17035 259
speedave 4:5868aec17035 260
speedave 4:5868aec17035 261
RedBearLab 0:cffe8ac1bdf0 262
RedBearLab 0:cffe8ac1bdf0 263
RedBearLab 0:cffe8ac1bdf0 264
RedBearLab 0:cffe8ac1bdf0 265
RedBearLab 0:cffe8ac1bdf0 266
RedBearLab 0:cffe8ac1bdf0 267
RedBearLab 0:cffe8ac1bdf0 268
RedBearLab 0:cffe8ac1bdf0 269
RedBearLab 0:cffe8ac1bdf0 270
RedBearLab 0:cffe8ac1bdf0 271
RedBearLab 0:cffe8ac1bdf0 272
RedBearLab 0:cffe8ac1bdf0 273