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 Nov 03 01:41:53 2015 +0000
Revision:
15:c9c93454dd56
Parent:
14:4fc1788b8ad2
Child:
16:e066ab7e8fb3
uncomment runready for it to work with rest of code

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"
jelord 11:1058647c66e8 6 #include "EEPROM.h"
jelord 11:1058647c66e8 7 #include "CronoDot.h"
jelord 11:1058647c66e8 8 #include "NCP5623BMUTBG.h"
jelord 13:b43ec7e0cc1d 9
rgrover1 0:28f095301cb2 10
rgrover1 6:18d8750f39ee 11 BLE ble;
jelord 11:1058647c66e8 12 //DigitalOut led1(LED1); //Use of leds is important for debugging
jelord 11:1058647c66e8 13 DigitalOut blower(p29, 0);
jelord 11:1058647c66e8 14 DigitalOut pbKill(p18, 1);
jelord 11:1058647c66e8 15 DigitalOut GPS_EN(p4,0);
jelord 11:1058647c66e8 16 EEPROM E2PROM(p22, p20);
jelord 11:1058647c66e8 17 CronoDot RTC(p22, p20);
jelord 12:27273e6a50b3 18 NCP5623BMUTBG RGB_LED(p22, p20);
jelord 13:b43ec7e0cc1d 19
jelord 11:1058647c66e8 20 /*EEPROM ADDRESSING:
jelord 11:1058647c66e8 21 0:Status bit-Unused
jelord 11:1058647c66e8 22 1-15:Device Name
jelord 11:1058647c66e8 23 16-19:Flow Rate
jelord 11:1058647c66e8 24 20: Data Log Interval
jelord 11:1058647c66e8 25 21-26: Start Time: ssmmHHddMMyy
jelord 11:1058647c66e8 26 27-32: Stop Time: ssmmHHddMMyy
jelord 11:1058647c66e8 27 33: Duty Up
jelord 11:1058647c66e8 28 34: Duty Down
jelord 11:1058647c66e8 29 35-38: Home Latitude
jelord 11:1058647c66e8 30 39-42: Home Longitude
jelord 11:1058647c66e8 31 43-46: Work Latitude
jelord 11:1058647c66e8 32 47-50: Work Longitude
jelord 11:1058647c66e8 33 51: Runready: Currently useless, should be 0
jelord 11:1058647c66e8 34 52-53: Device Calibration
jelord 11:1058647c66e8 35 54: Consider RunReady
jelord 11:1058647c66e8 36 55-56: Menu Options
jelord 11:1058647c66e8 37 57+ Nothing*/
rgrover1 0:28f095301cb2 38
jelord 12:27273e6a50b3 39 Timeout stop; //This is the stop call back object
jelord 12:27273e6a50b3 40 Timeout logg; //This is the logging call back object
jelord 10:66549fa08986 41 const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to
jelord 10:66549fa08986 42 static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID
rgrover1 0:28f095301cb2 43
jelord 10:66549fa08986 44 UPAS_Service *upasServicePtr;
rgrover1 0:28f095301cb2 45
jelord 12:27273e6a50b3 46
jelord 12:27273e6a50b3 47 //MARK: THIS IS ALL CODE NOT TAKEN FROM OTHER MENU>>>DONT ALTER!!!
jelord 10:66549fa08986 48 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect
rgrover1 0:28f095301cb2 49 {
rgrover1 6:18d8750f39ee 50 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 51 }
rgrover1 0:28f095301cb2 52
rgrover1 0:28f095301cb2 53 void periodicCallback(void)
rgrover1 0:28f095301cb2 54 {
jelord 11:1058647c66e8 55 // led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
jelord 12:27273e6a50b3 56 RTC.get_time();
jelord 12:27273e6a50b3 57 const uint8_t refreshPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
jelord 12:27273e6a50b3 58 ble.updateCharacteristicValue( upasServicePtr->rtcCharacteristic.getValueHandle(),refreshPassValues,6);
jelord 13:b43ec7e0cc1d 59 RGB_LED.set_led(3,0,2);
jelord 11:1058647c66e8 60 }
jelord 13:b43ec7e0cc1d 61
jelord 12:27273e6a50b3 62 void writeCharacteristicCallback(const GattWriteCallbackParams *params)
jelord 10:66549fa08986 63 {
jelord 12:27273e6a50b3 64 uint8_t *writeData = const_cast<uint8_t*>(params->data);
jelord 13:b43ec7e0cc1d 65
jelord 10:66549fa08986 66 // check to see what characteristic was written, by handle
jelord 12:27273e6a50b3 67 if(params->handle == upasServicePtr->rtcCharacteristic.getValueHandle()) {
jelord 12:27273e6a50b3 68
jelord 12:27273e6a50b3 69 //ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len);
jelord 12:27273e6a50b3 70
jelord 12:27273e6a50b3 71 // E2PROM.write(0x00015, writeData+6, 12);
jelord 11:1058647c66e8 72 RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
jelord 12:27273e6a50b3 73
jelord 12:27273e6a50b3 74 }else if(params->handle == upasServicePtr->sampleTimeCharacteristic.getValueHandle()){
jelord 12:27273e6a50b3 75
jelord 12:27273e6a50b3 76 E2PROM.write(0x00015, writeData, 12);
jelord 13:b43ec7e0cc1d 77
jelord 13:b43ec7e0cc1d 78 }else if(params->handle == upasServicePtr->subjectLabelCharacteristic.getValueHandle()){
jelord 13:b43ec7e0cc1d 79 E2PROM.write(0x00001,writeData,15);
jelord 13:b43ec7e0cc1d 80
jelord 15:c9c93454dd56 81 }else if(params->handle == upasServicePtr->runReadyCharacteristic.getValueHandle()){
jelord 13:b43ec7e0cc1d 82 /* Trigger twenty-four run-time mode*/
jelord 13:b43ec7e0cc1d 83 RGB_LED.set_led(2,3,1);
jelord 15:c9c93454dd56 84 //RunReady = 2;
jelord 13:b43ec7e0cc1d 85
jelord 14:4fc1788b8ad2 86 }else if(params->handle == upasServicePtr->runModeCharacteristic.getValueHandle()){
jelord 13:b43ec7e0cc1d 87 /* Trigger demo mode*/
jelord 13:b43ec7e0cc1d 88 RGB_LED.set_led(3,1,0);
jelord 14:4fc1788b8ad2 89 E2PROM.write(0x00036,writeData,1);
jelord 10:66549fa08986 90 }
jelord 10:66549fa08986 91 }
jelord 10:66549fa08986 92
rgrover1 0:28f095301cb2 93 int main(void)
rgrover1 0:28f095301cb2 94 {
jelord 11:1058647c66e8 95 // led1 = 1;
rgrover1 0:28f095301cb2 96 Ticker ticker;
jelord 13:b43ec7e0cc1d 97 ticker.attach(periodicCallback, 10);
jelord 11:1058647c66e8 98 RGB_LED.set_led(1,1,1);
jelord 11:1058647c66e8 99 RTC.get_time();
jelord 12:27273e6a50b3 100 uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
jelord 12:27273e6a50b3 101 uint8_t sampleTimePassValues[12] = {0,};
jelord 12:27273e6a50b3 102 uint8_t subjectLabelOriginal[15] = {0,};
jelord 12:27273e6a50b3 103 E2PROM.read(0x00015, sampleTimePassValues, 12);
jelord 12:27273e6a50b3 104 E2PROM.read(0x00001, subjectLabelOriginal,15);
rgrover1 0:28f095301cb2 105
rgrover1 0:28f095301cb2 106 ble.init();
rgrover1 6:18d8750f39ee 107 ble.gap().onDisconnection(disconnectionCallback);
jelord 12:27273e6a50b3 108 ble.gattServer().onDataWritten(writeCharacteristicCallback); //add writeCharCallback (custom function) to whenever data is being written to device
jelord 12:27273e6a50b3 109 UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal); //Create a GattService that is defined in UPAS_Service.h
jelord 10:66549fa08986 110 upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service
rgrover1 0:28f095301cb2 111
jelord 10:66549fa08986 112 /* setup advertising
jelord 10:66549fa08986 113 Following lines do the follow:
jelord 10:66549fa08986 114 1:Declare the device as Bluetooth Smart(Low-Energy)
jelord 10:66549fa08986 115 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM
jelord 10:66549fa08986 116 3.Advertise the name that will be associated with the UPAS
jelord 10:66549fa08986 117 4.Allow the UPAS to advertise unrestricted (this might change) */
jelord 10:66549fa08986 118
rgrover1 6:18d8750f39ee 119 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:18d8750f39ee 120 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:18d8750f39ee 121 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:18d8750f39ee 122 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jelord 10:66549fa08986 123 ble.gap().setAdvertisingInterval(160); /* 160ms. */
rgrover1 6:18d8750f39ee 124 ble.gap().startAdvertising();
jelord 10:66549fa08986 125
jelord 10:66549fa08986 126 //Loop keeps device in infinite loop waiting for events to occur
jelord 13:b43ec7e0cc1d 127 while (1) {
rgrover1 0:28f095301cb2 128 ble.waitForEvent();
rgrover1 0:28f095301cb2 129 }
rgrover1 0:28f095301cb2 130 }