Created
December 13, 2016 01:07
-
-
Save gparuthi/e5858a1ddd12bbe562c539bb3ef21fa6 to your computer and use it in GitHub Desktop.
BLE_SimpleChat example for Readbear BLE NANO adapted to send potentiometer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <BLE_API.h> | |
#define DEVICE_NAME "PotentiometerToChat" | |
#define TXRX_BUF_LEN 20 | |
#define DEVICE_ID 0xFF | |
BLE ble; | |
const int SOFT_POT_PIN = A3; // Pin connected to softpot wiper | |
const int GRAPH_LENGTH = 40; // Length of line graph | |
// The uuid of service and characteristics | |
static const uint8_t service1_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; | |
static const uint8_t service1_chars1_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; | |
static const uint8_t service1_chars2_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}; | |
uint8_t chars1_value[TXRX_BUF_LEN] = {0}; | |
uint8_t chars2_value[TXRX_BUF_LEN] = {0}; | |
// Create characteristic and service | |
GattCharacteristic characteristic1(service1_chars1_uuid, chars1_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE ); | |
GattCharacteristic characteristic2(service1_chars2_uuid, chars2_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); | |
GattCharacteristic *uartChars[] = {&characteristic1, &characteristic2}; | |
GattService uartService(service1_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); | |
void disconnectionCallBack(const Gap::DisconnectionCallbackParams_t *params) { | |
ble.startAdvertising(); | |
} | |
void gattServerWriteCallBack(const GattWriteCallbackParams *Handler) { | |
uint8_t buf[TXRX_BUF_LEN]; | |
uint16_t bytesRead, index; | |
Serial.println("Write Handle : "); | |
if (Handler->handle == characteristic1.getValueAttribute().getHandle()) { | |
ble.readCharacteristicValue(characteristic1.getValueAttribute().getHandle(), buf, &bytesRead); | |
for(index=0; index<bytesRead; index++) | |
{ | |
Serial.print(buf[index], HEX); | |
} | |
// write_handle(); | |
} | |
} | |
void setup() { | |
// put your setup code here, to run once | |
Serial.begin(9600); | |
Serial.println("Start "); | |
// ticker_task1.attach(sendMessage, 2); | |
ble.init(); | |
ble.onDisconnection(disconnectionCallBack); | |
ble.onDataWritten(gattServerWriteCallBack); | |
// setup adv_data and srp_data | |
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); | |
ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, | |
(const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); | |
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, | |
(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid_rev)); | |
// set adv_type | |
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); | |
// add service | |
ble.addService(uartService); | |
// set device name | |
ble.setDeviceName((const uint8_t *)DEVICE_NAME); | |
// set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4 | |
ble.setTxPower(4); | |
// set adv_interval, 100ms in multiples of 0.625ms. | |
ble.setAdvertisingInterval(160); | |
// set adv_timeout, in seconds | |
ble.setAdvertisingTimeout(0); | |
ble.startAdvertising(); | |
Serial.println("start advertising "); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
ble.waitForEvent(); | |
// Read in the soft pot's ADC value | |
int softPotADC = analogRead(SOFT_POT_PIN); | |
// Map the 0-1023 value to 0-40. On testing the actual values are between 350 and 700 | |
int softPotPosition = map(softPotADC, 350, 700, 0, GRAPH_LENGTH); | |
if (softPotPosition>0) | |
uint8_t buf[2] = {softPotPosition, (softPotPosition >> 8)}; ble.updateCharacteristicValue(characteristic2.getValueAttribute().getHandle(), buf, sizeof(buf) - 1 ); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment