This program is given as a sample exercise. It has all the functionality to be used on a BLE Nano device and to connect to SimpleChat application for Android/ iOS from RebBearLab. The aim of the exercise is to read a voltage and then to convert as good as possible the appropriate temperature in Celsius degrees. AI5 pin is considered for reading the voltage for the termistor. The ADC of AI5 is called every second. The function to be updated : update_measurements() from main.cpp file.
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_DataLogger_with_Chat by
Diff: main.cpp
- Revision:
- 6:a574229993b8
- Parent:
- 5:ee7e1a9e1985
- Child:
- 7:609dff35b660
--- a/main.cpp Thu Mar 31 15:23:00 2016 +0000 +++ b/main.cpp Thu Mar 31 16:29:40 2016 +0000 @@ -45,6 +45,7 @@ static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; +uint16_t txPower = 0xCD; uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; @@ -72,17 +73,22 @@ void WrittenHandler(const GattWriteCallbackParams *Handler) { - uint8_t buf[TXRX_BUF_LEN]; + uint8_t buf[TXRX_BUF_LEN+1]; uint16_t bytesRead, index; if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { - ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); + ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), &buf[2], &bytesRead); memset(txPayload, 0, TXRX_BUF_LEN); - memcpy(txPayload, buf, TXRX_BUF_LEN); - pc.printf("WriteHandler \r\n"); - pc.printf("Length: %d \r\n", bytesRead); - pc.printf("Data: "); + memcpy(txPayload, &buf[2], TXRX_BUF_LEN); + //echo back + buf[0]='R'; + buf[1]=':'; + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, bytesRead+2); + // print on PC monitor + //pc.printf("WriteHandler \r\n"); + //pc.printf("Length: %d \r\n", bytesRead); + pc.printf("R: "); for(index=0; index<bytesRead; index++) { pc.putc(txPayload[index]); @@ -97,11 +103,12 @@ { rx_buf[rx_len++] = pc.getc(); if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') - { - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); - pc.printf("RecHandler \r\n"); - pc.printf("Length: %d \r\n",rx_len); - rx_len = 0; + { + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); + //pc.printf("Rec: Length: %d \r\n",rx_len); + rx_len = 2; + rx_buf[0]='S'; + rx_buf[1]=':'; break; } } @@ -113,7 +120,7 @@ ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); - pc.baud(9600); + pc.baud(19200); pc.printf("SimpleChat Init \r\n"); pc.attach( uartCB , pc.RxIrq); @@ -121,9 +128,11 @@ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); + (const uint8_t *)"MyBleVT", sizeof("MyBleVT") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); + //ble.accumulateAdvertisingPayload(GapAdvertisingData::TX_POWER_LEVEL,(const uint8_t *)txPower, sizeof(txPower)); + ble.setTxPower(txPower); // 100ms; in multiples of 0.625ms. ble.setAdvertisingInterval(160);