This is a basic program that provides the necessary BLE service to allow communications with the UPAS

Dependencies:   BLE_API mbed nRF51822 CronoDot EEPROM NCP5623BMUTBG ADS1115 BME280 Calibration_one MCP40D17 SDFileSystem LSM303 SI1145 STC3100

Fork of BLE_Button by Bluetooth Low Energy

Committer:
jelord
Date:
Tue Oct 20 20:38:04 2015 +0000
Revision:
10:66549fa08986
Parent:
9:0f6951db24f1
Child:
11:1058647c66e8
Program to control BLE communication with UPAS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jelord 10:66549fa08986 1 //CODE BY JAKE LORD
jelord 10:66549fa08986 2 //ALL RIGHTS RESERVED BY VOLCKENS GROUP, FORT COLLINS CO
rgrover1 0:28f095301cb2 3 #include "mbed.h"
rgrover1 5:43a3ab27f2e4 4 #include "BLE.h"
jelord 10:66549fa08986 5 #include "UPAS_Service.h"
rgrover1 0:28f095301cb2 6
rgrover1 6:18d8750f39ee 7 BLE ble;
jelord 10:66549fa08986 8 DigitalOut led1(LED1); //Use of leds is important for debugging
rgrover1 0:28f095301cb2 9
jelord 10:66549fa08986 10 const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to
jelord 10:66549fa08986 11 static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID
rgrover1 0:28f095301cb2 12
jelord 10:66549fa08986 13 UPAS_Service *upasServicePtr;
rgrover1 0:28f095301cb2 14
jelord 10:66549fa08986 15 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect
rgrover1 0:28f095301cb2 16 {
rgrover1 6:18d8750f39ee 17 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 18 }
rgrover1 0:28f095301cb2 19
rgrover1 0:28f095301cb2 20 void periodicCallback(void)
rgrover1 0:28f095301cb2 21 {
rgrover1 0:28f095301cb2 22 led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
rgrover1 0:28f095301cb2 23 }
rgrover1 0:28f095301cb2 24
jelord 10:66549fa08986 25 void writeCharCallback(const GattWriteCallbackParams *params)
jelord 10:66549fa08986 26 {
jelord 10:66549fa08986 27 led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
jelord 10:66549fa08986 28
jelord 10:66549fa08986 29 // check to see what characteristic was written, by handle
jelord 10:66549fa08986 30 if(params->handle == upasServicePtr->writeChar.getValueHandle()) {
jelord 10:66549fa08986 31 // toggle LED if only 1 byte is written
jelord 10:66549fa08986 32 if(params->len == 1) {
jelord 10:66549fa08986 33 led1 = params->data[0];
jelord 10:66549fa08986 34 }
jelord 10:66549fa08986 35 // update the readChar with the value of writeChar
jelord 10:66549fa08986 36 ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len);
jelord 10:66549fa08986 37 }
jelord 10:66549fa08986 38 }
jelord 10:66549fa08986 39
rgrover1 0:28f095301cb2 40 int main(void)
rgrover1 0:28f095301cb2 41 {
rgrover1 0:28f095301cb2 42 led1 = 1;
rgrover1 0:28f095301cb2 43 Ticker ticker;
rgrover1 0:28f095301cb2 44 ticker.attach(periodicCallback, 1);
jelord 10:66549fa08986 45
jelord 10:66549fa08986 46 uint8_t readValues[57] = {0x31,0x30,0x2F,0x31,0x38,0x2F,0x31,0x35,0x20,0x31,0x32,0x3A,0x30,0x30,0x3A,0x30,0x30}; //dummy array for testing
rgrover1 0:28f095301cb2 47
rgrover1 0:28f095301cb2 48 ble.init();
rgrover1 6:18d8750f39ee 49 ble.gap().onDisconnection(disconnectionCallback);
jelord 10:66549fa08986 50 ble.gattServer().onDataWritten(writeCharCallback); //add writeCharCallback (custom function) to whenever data is being written to device
jelord 10:66549fa08986 51
jelord 10:66549fa08986 52 UPAS_Service upasService(ble, false,readValues); //Create a GattService that is defined in UPAS_Service.h
jelord 10:66549fa08986 53 upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service
rgrover1 0:28f095301cb2 54
jelord 10:66549fa08986 55 /* setup advertising
jelord 10:66549fa08986 56 Following lines do the follow:
jelord 10:66549fa08986 57 1:Declare the device as Bluetooth Smart(Low-Energy)
jelord 10:66549fa08986 58 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM
jelord 10:66549fa08986 59 3.Advertise the name that will be associated with the UPAS
jelord 10:66549fa08986 60 4.Allow the UPAS to advertise unrestricted (this might change) */
jelord 10:66549fa08986 61
rgrover1 6:18d8750f39ee 62 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:18d8750f39ee 63 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:18d8750f39ee 64 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:18d8750f39ee 65 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jelord 10:66549fa08986 66 ble.gap().setAdvertisingInterval(160); /* 160ms. */
rgrover1 6:18d8750f39ee 67 ble.gap().startAdvertising();
jelord 10:66549fa08986 68
jelord 10:66549fa08986 69 //Loop keeps device in infinite loop waiting for events to occur
rgrover1 0:28f095301cb2 70 while (true) {
rgrover1 0:28f095301cb2 71 ble.waitForEvent();
rgrover1 0:28f095301cb2 72 }
rgrover1 0:28f095301cb2 73 }