Test code. Blinks by default at 2 seconds. Receives single characters from PC (sensor) and transmit to "nRF Connect" on droid. Droid can send ascii to Nano. When droid sends "A", blinking is twice per second. "B" returns to slow.

Dependencies:   BLE_API mbed nRF51822

Fork of IofT_comm by Eric MacDonald

Committer:
emacdonald
Date:
Mon Jan 30 23:04:36 2017 +0000
Revision:
6:42bb0cde20f0
Parent:
5:3384d9d80bf3
YSU Bluetooth Sensor - "A" for fast blink, "B" for slow blink through either "Light Blue" on iPhone or "nRF_Connect" on Droid.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:1c058e553423 1
RedBearLab 1:1c058e553423 2 /*
RedBearLab 1:1c058e553423 3 * The application works with the BLEController iOS/Android App.
RedBearLab 1:1c058e553423 4 * Type something from the Terminal to send
RedBearLab 1:1c058e553423 5 * to the BLEController App or vice verse.
RedBearLab 1:1c058e553423 6 * Characteristics received from App will print on Terminal.
RedBearLab 1:1c058e553423 7 */
RedBearLab 1:1c058e553423 8
RedBearLab 0:cffe8ac1bdf0 9 #include "mbed.h"
RedBearLab 2:4b66b69c7ecb 10 #include "ble/BLE.h"
RedBearLab 0:cffe8ac1bdf0 11
RedBearLab 0:cffe8ac1bdf0 12
RedBearLab 0:cffe8ac1bdf0 13 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 14 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 15 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 16
emacdonald 5:3384d9d80bf3 17 #define TXRX_BUF_LEN 4
RedBearLab 0:cffe8ac1bdf0 18
RedBearLab 2:4b66b69c7ecb 19 BLE ble;
emacdonald 4:378d43fa5d20 20 DigitalOut led1(LED1);
RedBearLab 0:cffe8ac1bdf0 21 Serial pc(USBTX, USBRX);
RedBearLab 0:cffe8ac1bdf0 22
RedBearLab 0:cffe8ac1bdf0 23
RedBearLab 0:cffe8ac1bdf0 24 // The Nordic UART Service
RedBearLab 0:cffe8ac1bdf0 25 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 26 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 27 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 28 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 29
RedBearLab 0:cffe8ac1bdf0 30
RedBearLab 0:cffe8ac1bdf0 31 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 32 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 33
emacdonald 5:3384d9d80bf3 34 //static uint8_t rx_buf[TXRX_BUF_LEN];
emacdonald 5:3384d9d80bf3 35 static uint8_t rx_buf[4] = "a";
emacdonald 5:3384d9d80bf3 36 static uint8_t rx_len=2;
emacdonald 5:3384d9d80bf3 37 uint8_t blinklow;
emacdonald 5:3384d9d80bf3 38 uint8_t blinkperiod;
emacdonald 5:3384d9d80bf3 39 uint8_t blinkrate;
emacdonald 5:3384d9d80bf3 40 uint8_t blinkcount;
RedBearLab 0:cffe8ac1bdf0 41
RedBearLab 0:cffe8ac1bdf0 42
RedBearLab 0:cffe8ac1bdf0 43 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 44
RedBearLab 0:cffe8ac1bdf0 45 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 46
RedBearLab 0:cffe8ac1bdf0 47 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 48
RedBearLab 0:cffe8ac1bdf0 49 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 50
RedBearLab 0:cffe8ac1bdf0 51
RedBearLab 0:cffe8ac1bdf0 52
RedBearLab 3:b3f6c612b603 53 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
RedBearLab 0:cffe8ac1bdf0 54 {
RedBearLab 0:cffe8ac1bdf0 55 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 56 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 57 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 58 }
RedBearLab 0:cffe8ac1bdf0 59
emacdonald 4:378d43fa5d20 60 // Serial Write from BLE Read
RedBearLab 2:4b66b69c7ecb 61 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 62 {
RedBearLab 0:cffe8ac1bdf0 63 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 64 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 65
RedBearLab 2:4b66b69c7ecb 66 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 67 {
emacdonald 4:378d43fa5d20 68 led1 = !led1;
RedBearLab 0:cffe8ac1bdf0 69 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 70 memset(txPayload, 0, TXRX_BUF_LEN);
emacdonald 5:3384d9d80bf3 71 memcpy(txPayload, buf, TXRX_BUF_LEN);
emacdonald 5:3384d9d80bf3 72 if(bytesRead==0x01) {
emacdonald 5:3384d9d80bf3 73 if(txPayload[0] == 0x41){
emacdonald 5:3384d9d80bf3 74 pc.printf("Fast \r\n");
emacdonald 5:3384d9d80bf3 75 blinkperiod = 5;
emacdonald 5:3384d9d80bf3 76 }
emacdonald 5:3384d9d80bf3 77 if(txPayload[0] == 0x42){
emacdonald 5:3384d9d80bf3 78 pc.printf("Slow \r\n");
emacdonald 5:3384d9d80bf3 79 blinkperiod = 20;
emacdonald 5:3384d9d80bf3 80 }
emacdonald 5:3384d9d80bf3 81 }
emacdonald 5:3384d9d80bf3 82 pc.printf("BLE received = ");
RedBearLab 0:cffe8ac1bdf0 83 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 84 {
RedBearLab 0:cffe8ac1bdf0 85 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 86 }
RedBearLab 0:cffe8ac1bdf0 87 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 88 }
RedBearLab 0:cffe8ac1bdf0 89 }
RedBearLab 0:cffe8ac1bdf0 90
emacdonald 5:3384d9d80bf3 91 // BLE Write from Serial read
RedBearLab 0:cffe8ac1bdf0 92 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 93 {
RedBearLab 0:cffe8ac1bdf0 94 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 95 {
emacdonald 5:3384d9d80bf3 96 rx_buf[0] = pc.getc();
emacdonald 5:3384d9d80bf3 97 led1 = !led1;
emacdonald 5:3384d9d80bf3 98 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
emacdonald 5:3384d9d80bf3 99 pc.printf("Received sensor data transmitted on BLE ");
emacdonald 5:3384d9d80bf3 100 pc.putc(rx_buf[0]);
emacdonald 5:3384d9d80bf3 101 pc.printf(" \r\n");
RedBearLab 0:cffe8ac1bdf0 102 }
RedBearLab 0:cffe8ac1bdf0 103 }
RedBearLab 0:cffe8ac1bdf0 104
emacdonald 5:3384d9d80bf3 105 //void u//artCB(void)
emacdonald 5:3384d9d80bf3 106 // {
emacdonald 5:3384d9d80bf3 107 // while(pc.readable())
emacdonald 5:3384d9d80bf3 108 // {
emacdonald 5:3384d9d80bf3 109 // rx_buf[rx_len++] = pc.getc();
emacdonald 5:3384d9d80bf3 110 // led1 = !led1;
emacdonald 5:3384d9d80bf3 111 // if(rx_len>=4 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
emacdonald 5:3384d9d80bf3 112 // {
emacdonald 5:3384d9d80bf3 113 // ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
emacdonald 5:3384d9d80bf3 114 // pc.printf("Received sensor data transmitted on BLE ");
emacdonald 5:3384d9d80bf3 115 // pc.putc(rx_len);
emacdonald 5:3384d9d80bf3 116 // pc.printf("\r\n");
emacdonald 5:3384d9d80bf3 117 // rx_len = 0;
emacdonald 5:3384d9d80bf3 118 // break;
emacdonald 5:3384d9d80bf3 119 // }
emacdonald 5:3384d9d80bf3 120 // }
emacdonald 5:3384d9d80bf3 121 //}
emacdonald 5:3384d9d80bf3 122
emacdonald 4:378d43fa5d20 123 void periodicCallback(void)
emacdonald 4:378d43fa5d20 124 {
emacdonald 5:3384d9d80bf3 125 blinkcount = blinkcount + 1;
emacdonald 5:3384d9d80bf3 126 if(blinkcount >= blinkperiod)
emacdonald 5:3384d9d80bf3 127 {
emacdonald 5:3384d9d80bf3 128 led1 = 0;
emacdonald 5:3384d9d80bf3 129 blinkcount = 0;
emacdonald 5:3384d9d80bf3 130 pc.printf("Blinked\r\n");
emacdonald 5:3384d9d80bf3 131 }
emacdonald 5:3384d9d80bf3 132 else if(blinkcount > 1)
emacdonald 5:3384d9d80bf3 133 {
emacdonald 5:3384d9d80bf3 134 led1 = 1;
emacdonald 5:3384d9d80bf3 135 }
emacdonald 5:3384d9d80bf3 136 else
emacdonald 5:3384d9d80bf3 137 {
emacdonald 5:3384d9d80bf3 138 led1 = 0;
emacdonald 5:3384d9d80bf3 139 }
emacdonald 5:3384d9d80bf3 140
emacdonald 4:378d43fa5d20 141 }
emacdonald 4:378d43fa5d20 142
RedBearLab 0:cffe8ac1bdf0 143 int main(void)
RedBearLab 0:cffe8ac1bdf0 144 {
emacdonald 5:3384d9d80bf3 145 led1 = 0;
emacdonald 5:3384d9d80bf3 146 blinkcount = 0;
emacdonald 5:3384d9d80bf3 147 blinkperiod = 20;
emacdonald 4:378d43fa5d20 148 Ticker ticker;
emacdonald 5:3384d9d80bf3 149 ticker.attach(periodicCallback, 0.1); // blink LED every second
RedBearLab 0:cffe8ac1bdf0 150 ble.init();
RedBearLab 0:cffe8ac1bdf0 151 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 152 ble.onDataWritten(WrittenHandler);
RedBearLab 0:cffe8ac1bdf0 153
RedBearLab 0:cffe8ac1bdf0 154 pc.baud(9600);
emacdonald 5:3384d9d80bf3 155 pc.printf("IoT Init \r\n");
RedBearLab 0:cffe8ac1bdf0 156
RedBearLab 0:cffe8ac1bdf0 157 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 158 // setup advertising
RedBearLab 0:cffe8ac1bdf0 159 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 160 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 161 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
emacdonald 6:42bb0cde20f0 162 (const uint8_t *)"YSU_bluetooth", sizeof("YSU_bluetooth") - 1);
RedBearLab 0:cffe8ac1bdf0 163 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 164 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 165 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 166 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 167
RedBearLab 0:cffe8ac1bdf0 168 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 169
RedBearLab 0:cffe8ac1bdf0 170 ble.startAdvertising();
emacdonald 5:3384d9d80bf3 171 pc.printf("Advertising Started \r\n");
RedBearLab 0:cffe8ac1bdf0 172
RedBearLab 0:cffe8ac1bdf0 173 while(1)
RedBearLab 0:cffe8ac1bdf0 174 {
RedBearLab 0:cffe8ac1bdf0 175 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 176 }
emacdonald 5:3384d9d80bf3 177 }