This program is designed to work with RedBearLab BLE Controller App - Chat App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal. This Program scans some analog and digital inputs and save them in flash with timestamp. A RTC has been implemented. Using chat application (available for smart phones) some commands can be send to BLE Nano in order to perform some tasks. Other types of commands can be send via serial interface from a PC, especially data extraction from flash.

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat_VT by Valentin Tanasa

nRF51822_SimpleChat Extended With Data Measurements & Logger

This repo is derived from the initial SimpleChat Application and is build for BLE Nano Device, used in peripheral mode. This Application scan and measures some inputs and save them in flash with timestamp. A RTC has been implemented. Using chat application (available for smart phones) some commands can be send to BLE Nano in order to perform some tasks. Other types of commands can be send via serial interface from a PC, especially data extraction from flash.

Usage/Description:

After flashing or power on, the ble device will start to advertise and also its local time counter starts. While initializing, application will search through all flash pages, last data previosly written. From this pages will retrieve the last data and time available.

A Timer Tick (period set to 1 second) is used for updating RTC and for data measurements. When the application starts the data will not be saved in flash until a command is given to start (xf2). When started, it is recommended after connecting to ble device to set the time and date;

After the date and time updated, activate measurement logging (xf2).

Please be aware that for 5-6 seconds, each 100 seconds the connection is stopped by the application. This is due to fact that writing to flash is safe when radio connection is off. When g_MyDataIdx is between 95..100, connection should remain closed.

The Application can use somewhere around 100 flash pages (100K). This means, if data is sampled each second, the pages will start to overwrite after 100*100 seconds = 2.7 hours. Anyway the data is inserted into logger when it is different compared to previous measurement. Therefore the time the flash memory can log is bigger than 2.7 hours.

When retrieving the data log via serial, the format is as follows:

  • page starts with:
    2016_ 4_22 H: 7 => year, month, day, and hour
    26:51;363; 1; 15 => minutes: seconds; A3,A4,A5 (no info about digital inputs)
  • following rows up to the next page:
    0: 5;364; 1; 15 => minutes: seconds - time delta (time elapsed since last measurement); A3, A4, A5;

Hardware Connections

The BLE Nano Device is connected to some peripheral components as follows:

  • Light Sensor ( A3 input)
  • Temperature Sensor (A4 input)
  • Gnd Voltage (or anything else) (A5 input)
  • Buzzer (D6 output)
  • Push Button (D5 input)
  • [Optional]: MKUSB 20 Board (for Voltage Supply and for Serial Communication with a PC)

Supported Commands

  1. From RedBear Chat Application following messages are valid:
  • Any message that not start with 'x' is echoed with 'R:' as prefix, and also is send via Serial Interface to PC
  • Any message that start with 'x' is interpreted as command:
    • xi0 / xi* except {1,2,3,4} => prints the analog inputs and one digital input (the led status) as uint16 values
    • xi1 /xi2 / xi3 / xi4 =>prints the uint16 value for light, temp, gndV, led status
    • xl => toggle led status
    • xs[0-9][0-9] => buzzer; depending on digits, a digital pwm signal is send to buzzer; to deactivate: xs00
    • xtg => returns the current time
    • xti[0-9]{6} => insert time - exp: xti181004 means: 18H10M and 4 seconds
    • xdg => returns the current date
    • xdi[0-9]{6} => insert date - exp: xti160424 means: 2016 Y, 04 Month, 24 Day
    • xf1 => prints the value of g_MyDataIdx (a value now between 0..99) representing the steps until a new page will be written in flash with new data; It also print the current flash page that will be written (between 155 and 255)
    • xf2 => activate measurements logging
    • xf3 => deactivate measurements logging
    • x* => for invalid syntax a sound for 3 seconds is activated, with a message error
  1. From PC serial interface (with MKUSB 20 Board), following commands apply:
  • xf[0-9] => request printing on serial of a specific flash page ( 0 - current page; 1 - previous page, etc)
  • xd => request dump of all recorded logs (starting with current page)
  • xg => prints the value of g_MyDataIdx (a value now between 0..99) representing the steps until a new page will be written in flash with new data;
  • xca => open Radio Advertising (should be used when the Advertising is Off)
  • xcc => Close Radio connection (should be used when Connection is in Connected state)
  • xcs => Stop Radio Advertising (should be used when Advertising is active)

Other Commands:

  • button push => closes the connection/advertising and turn off the led. If push again reset the previous conditions;

References

The mbed BLE API is meant to be used in projects on developer.mbed.org. Please see examples and sample project files there. A good starting point are these pages:

For this Application:

Committer:
tanasaro10
Date:
Thu Mar 31 16:29:40 2016 +0000
Revision:
6:a574229993b8
Parent:
5:ee7e1a9e1985
Child:
7:609dff35b660
Some new features included:; -> signals the sender and receiver messages; -> echo for message sent from RedBear App

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"
RedBearLab 0:cffe8ac1bdf0 29
RedBearLab 0:cffe8ac1bdf0 30
RedBearLab 0:cffe8ac1bdf0 31 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 32 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 33 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 34
RedBearLab 0:cffe8ac1bdf0 35 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 36
RedBearLab 2:4b66b69c7ecb 37 BLE ble;
RedBearLab 0:cffe8ac1bdf0 38
RedBearLab 0:cffe8ac1bdf0 39 Serial pc(USBTX, USBRX);
RedBearLab 0:cffe8ac1bdf0 40
RedBearLab 0:cffe8ac1bdf0 41
RedBearLab 0:cffe8ac1bdf0 42 // The Nordic UART Service
RedBearLab 0:cffe8ac1bdf0 43 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 44 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 45 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 46 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 47
tanasaro10 6:a574229993b8 48 uint16_t txPower = 0xCD;
RedBearLab 0:cffe8ac1bdf0 49
RedBearLab 0:cffe8ac1bdf0 50 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 51 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 52
RedBearLab 0:cffe8ac1bdf0 53 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 54 static uint8_t rx_len=0;
RedBearLab 0:cffe8ac1bdf0 55
RedBearLab 0:cffe8ac1bdf0 56
RedBearLab 0:cffe8ac1bdf0 57 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 58
RedBearLab 0:cffe8ac1bdf0 59 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 60
RedBearLab 0:cffe8ac1bdf0 61 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 62
RedBearLab 0:cffe8ac1bdf0 63 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 64
RedBearLab 0:cffe8ac1bdf0 65
RedBearLab 0:cffe8ac1bdf0 66
RedBearLab 0:cffe8ac1bdf0 67 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 68 {
RedBearLab 0:cffe8ac1bdf0 69 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 70 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 71 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 72 }
RedBearLab 0:cffe8ac1bdf0 73
RedBearLab 2:4b66b69c7ecb 74 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 75 {
tanasaro10 6:a574229993b8 76 uint8_t buf[TXRX_BUF_LEN+1];
RedBearLab 0:cffe8ac1bdf0 77 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 78
RedBearLab 2:4b66b69c7ecb 79 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 80 {
tanasaro10 6:a574229993b8 81 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), &buf[2], &bytesRead);
RedBearLab 0:cffe8ac1bdf0 82 memset(txPayload, 0, TXRX_BUF_LEN);
tanasaro10 6:a574229993b8 83 memcpy(txPayload, &buf[2], TXRX_BUF_LEN);
tanasaro10 6:a574229993b8 84 //echo back
tanasaro10 6:a574229993b8 85 buf[0]='R';
tanasaro10 6:a574229993b8 86 buf[1]=':';
tanasaro10 6:a574229993b8 87 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, bytesRead+2);
tanasaro10 6:a574229993b8 88 // print on PC monitor
tanasaro10 6:a574229993b8 89 //pc.printf("WriteHandler \r\n");
tanasaro10 6:a574229993b8 90 //pc.printf("Length: %d \r\n", bytesRead);
tanasaro10 6:a574229993b8 91 pc.printf("R: ");
RedBearLab 0:cffe8ac1bdf0 92 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 93 {
RedBearLab 0:cffe8ac1bdf0 94 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 95 }
RedBearLab 0:cffe8ac1bdf0 96 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 97 }
RedBearLab 0:cffe8ac1bdf0 98 }
RedBearLab 0:cffe8ac1bdf0 99
RedBearLab 0:cffe8ac1bdf0 100 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 101 {
RedBearLab 0:cffe8ac1bdf0 102 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 103 {
RedBearLab 0:cffe8ac1bdf0 104 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 105 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
tanasaro10 6:a574229993b8 106 {
tanasaro10 6:a574229993b8 107 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
tanasaro10 6:a574229993b8 108 //pc.printf("Rec: Length: %d \r\n",rx_len);
tanasaro10 6:a574229993b8 109 rx_len = 2;
tanasaro10 6:a574229993b8 110 rx_buf[0]='S';
tanasaro10 6:a574229993b8 111 rx_buf[1]=':';
RedBearLab 0:cffe8ac1bdf0 112 break;
RedBearLab 0:cffe8ac1bdf0 113 }
RedBearLab 0:cffe8ac1bdf0 114 }
RedBearLab 0:cffe8ac1bdf0 115 }
RedBearLab 0:cffe8ac1bdf0 116
RedBearLab 0:cffe8ac1bdf0 117 int main(void)
RedBearLab 0:cffe8ac1bdf0 118 {
RedBearLab 0:cffe8ac1bdf0 119 ble.init();
RedBearLab 0:cffe8ac1bdf0 120 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 121 ble.onDataWritten(WrittenHandler);
RedBearLab 0:cffe8ac1bdf0 122
tanasaro10 6:a574229993b8 123 pc.baud(19200);
RedBearLab 0:cffe8ac1bdf0 124 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 125
RedBearLab 0:cffe8ac1bdf0 126 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 127 // setup advertising
RedBearLab 0:cffe8ac1bdf0 128 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 129 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 130 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
tanasaro10 6:a574229993b8 131 (const uint8_t *)"MyBleVT", sizeof("MyBleVT") - 1);
RedBearLab 0:cffe8ac1bdf0 132 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 133 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
tanasaro10 6:a574229993b8 134 //ble.accumulateAdvertisingPayload(GapAdvertisingData::TX_POWER_LEVEL,(const uint8_t *)txPower, sizeof(txPower));
tanasaro10 6:a574229993b8 135 ble.setTxPower(txPower);
RedBearLab 0:cffe8ac1bdf0 136 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 137 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 138
RedBearLab 0:cffe8ac1bdf0 139 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 140
RedBearLab 0:cffe8ac1bdf0 141 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 142 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 143
RedBearLab 0:cffe8ac1bdf0 144 while(1)
RedBearLab 0:cffe8ac1bdf0 145 {
RedBearLab 0:cffe8ac1bdf0 146 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 147 }
RedBearLab 0:cffe8ac1bdf0 148 }
RedBearLab 0:cffe8ac1bdf0 149
RedBearLab 0:cffe8ac1bdf0 150
RedBearLab 0:cffe8ac1bdf0 151
RedBearLab 0:cffe8ac1bdf0 152
RedBearLab 0:cffe8ac1bdf0 153
RedBearLab 0:cffe8ac1bdf0 154
RedBearLab 0:cffe8ac1bdf0 155
RedBearLab 0:cffe8ac1bdf0 156
RedBearLab 0:cffe8ac1bdf0 157
RedBearLab 0:cffe8ac1bdf0 158
RedBearLab 0:cffe8ac1bdf0 159
RedBearLab 0:cffe8ac1bdf0 160
RedBearLab 0:cffe8ac1bdf0 161
RedBearLab 0:cffe8ac1bdf0 162
RedBearLab 0:cffe8ac1bdf0 163
RedBearLab 0:cffe8ac1bdf0 164