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:
Fri Oct 23 02:44:23 2015 +0000
Revision:
11:1058647c66e8
Parent:
10:66549fa08986
Child:
12:27273e6a50b3
App can now set real-time, sample start and end time with this custom menu.

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"
rgrover1 0:28f095301cb2 9
rgrover1 6:18d8750f39ee 10 BLE ble;
jelord 11:1058647c66e8 11 //DigitalOut led1(LED1); //Use of leds is important for debugging
jelord 11:1058647c66e8 12 DigitalOut blower(p29, 0);
jelord 11:1058647c66e8 13 DigitalOut pbKill(p18, 1);
jelord 11:1058647c66e8 14 DigitalOut GPS_EN(p4,0);
jelord 11:1058647c66e8 15 EEPROM E2PROM(p22, p20);
jelord 11:1058647c66e8 16 CronoDot RTC(p22, p20);
jelord 11:1058647c66e8 17 NCP5623BMUTBG RGB_LED(p22, p20);
jelord 11:1058647c66e8 18 /*EEPROM ADDRESSING:
jelord 11:1058647c66e8 19 0:Status bit-Unused
jelord 11:1058647c66e8 20 1-15:Device Name
jelord 11:1058647c66e8 21 16-19:Flow Rate
jelord 11:1058647c66e8 22 20: Data Log Interval
jelord 11:1058647c66e8 23 21-26: Start Time: ssmmHHddMMyy
jelord 11:1058647c66e8 24 27-32: Stop Time: ssmmHHddMMyy
jelord 11:1058647c66e8 25 33: Duty Up
jelord 11:1058647c66e8 26 34: Duty Down
jelord 11:1058647c66e8 27 35-38: Home Latitude
jelord 11:1058647c66e8 28 39-42: Home Longitude
jelord 11:1058647c66e8 29 43-46: Work Latitude
jelord 11:1058647c66e8 30 47-50: Work Longitude
jelord 11:1058647c66e8 31 51: Runready: Currently useless, should be 0
jelord 11:1058647c66e8 32 52-53: Device Calibration
jelord 11:1058647c66e8 33 54: Consider RunReady
jelord 11:1058647c66e8 34 55-56: Menu Options
jelord 11:1058647c66e8 35 57+ Nothing*/
jelord 11:1058647c66e8 36 // .write(uint32_t addr, uint8_t dt[], uint16_t length);
jelord 11:1058647c66e8 37 // addr -- is where the data in dt[] will be stored, max of 0x3FFFF, 0x00000 to 0x1FFFF on EEPROM 1 and 0x20000 to 0x3FFFF on EEPROM 2
jelord 11:1058647c66e8 38 // dt[] -- is unit8_t array that contains the data to be stored. This can only be uint8_t types, single bytes
jelord 11:1058647c66e8 39 // length -- is the number of bytes to save t the EEPROM, starting at dt[0]
jelord 11:1058647c66e8 40 //
jelord 11:1058647c66e8 41 // returns:
jelord 11:1058647c66e8 42 // Ture(1) -- when if has finished saving the data
jelord 11:1058647c66e8 43 // False(0) -- when length is to long to fit on the remainder of the page, when this happens it does not save any data in order to avoid over writing past data
jelord 11:1058647c66e8 44 //
jelord 11:1058647c66e8 45 // .read(uint32_t addr, uint8_t *rt_data, uint16_t length);
jelord 11:1058647c66e8 46 // addr -- where the data is requested from, max of 0x3FFFF, 0x00000 to 0x1FFFF on EEPROM 1 and 0x20000 to 0x3FFFF on EEPROM 2
jelord 11:1058647c66e8 47 // rt_data -- a local variable where the data from the EEPROM should be stored, again in uint8_t types
jelord 11:1058647c66e8 48 // length -- how much to read from the EERPOM, this may be limited to <256 bytes?
rgrover1 0:28f095301cb2 49
jelord 10:66549fa08986 50 const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to
jelord 10:66549fa08986 51 static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID
rgrover1 0:28f095301cb2 52
jelord 10:66549fa08986 53 UPAS_Service *upasServicePtr;
rgrover1 0:28f095301cb2 54
jelord 10:66549fa08986 55 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect
rgrover1 0:28f095301cb2 56 {
rgrover1 6:18d8750f39ee 57 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 58 }
rgrover1 0:28f095301cb2 59
rgrover1 0:28f095301cb2 60 void periodicCallback(void)
rgrover1 0:28f095301cb2 61 {
jelord 11:1058647c66e8 62 // led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
rgrover1 0:28f095301cb2 63 }
jelord 11:1058647c66e8 64 void readCharCallin(const GattReadCallbackParams *eventDataP)
jelord 11:1058647c66e8 65 {
jelord 11:1058647c66e8 66 RTC.get_time();
jelord 11:1058647c66e8 67 }
jelord 10:66549fa08986 68 void writeCharCallback(const GattWriteCallbackParams *params)
jelord 10:66549fa08986 69 {
jelord 11:1058647c66e8 70 // led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
jelord 10:66549fa08986 71
jelord 10:66549fa08986 72 // check to see what characteristic was written, by handle
jelord 10:66549fa08986 73 if(params->handle == upasServicePtr->writeChar.getValueHandle()) {
jelord 10:66549fa08986 74 // toggle LED if only 1 byte is written
jelord 10:66549fa08986 75 if(params->len == 1) {
jelord 11:1058647c66e8 76 //led1 = params->data[0];
jelord 10:66549fa08986 77 }
jelord 10:66549fa08986 78 // update the readChar with the value of writeChar
jelord 10:66549fa08986 79 ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len);
jelord 11:1058647c66e8 80 uint8_t *writeData = const_cast<uint8_t*>(params->data);
jelord 11:1058647c66e8 81
jelord 11:1058647c66e8 82 E2PROM.write(0x00015, writeData+6, 12);
jelord 11:1058647c66e8 83 RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
jelord 10:66549fa08986 84 }
jelord 10:66549fa08986 85 }
jelord 10:66549fa08986 86
rgrover1 0:28f095301cb2 87 int main(void)
rgrover1 0:28f095301cb2 88 {
jelord 11:1058647c66e8 89 // led1 = 1;
rgrover1 0:28f095301cb2 90 Ticker ticker;
rgrover1 0:28f095301cb2 91 ticker.attach(periodicCallback, 1);
jelord 11:1058647c66e8 92 RGB_LED.set_led(1,1,1);
jelord 11:1058647c66e8 93 RTC.get_time();
jelord 11:1058647c66e8 94 uint8_t newReadValues[20] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
jelord 11:1058647c66e8 95 E2PROM.read(0x00015, newReadValues+6, 12);
rgrover1 0:28f095301cb2 96
rgrover1 0:28f095301cb2 97 ble.init();
rgrover1 6:18d8750f39ee 98 ble.gap().onDisconnection(disconnectionCallback);
jelord 10:66549fa08986 99 ble.gattServer().onDataWritten(writeCharCallback); //add writeCharCallback (custom function) to whenever data is being written to device
jelord 11:1058647c66e8 100 ble.gattServer().onDataRead(readCharCallin);
jelord 11:1058647c66e8 101 UPAS_Service upasService(ble, false,newReadValues); //Create a GattService that is defined in UPAS_Service.h
jelord 10:66549fa08986 102 upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service
rgrover1 0:28f095301cb2 103
jelord 10:66549fa08986 104 /* setup advertising
jelord 10:66549fa08986 105 Following lines do the follow:
jelord 10:66549fa08986 106 1:Declare the device as Bluetooth Smart(Low-Energy)
jelord 10:66549fa08986 107 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM
jelord 10:66549fa08986 108 3.Advertise the name that will be associated with the UPAS
jelord 10:66549fa08986 109 4.Allow the UPAS to advertise unrestricted (this might change) */
jelord 10:66549fa08986 110
rgrover1 6:18d8750f39ee 111 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:18d8750f39ee 112 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:18d8750f39ee 113 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:18d8750f39ee 114 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jelord 10:66549fa08986 115 ble.gap().setAdvertisingInterval(160); /* 160ms. */
rgrover1 6:18d8750f39ee 116 ble.gap().startAdvertising();
jelord 10:66549fa08986 117
jelord 10:66549fa08986 118 //Loop keeps device in infinite loop waiting for events to occur
rgrover1 0:28f095301cb2 119 while (true) {
rgrover1 0:28f095301cb2 120 ble.waitForEvent();
rgrover1 0:28f095301cb2 121 }
rgrover1 0:28f095301cb2 122 }