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:
caseyquinn
Date:
Tue Dec 08 05:03:24 2015 +0000
Revision:
18:c911a8928d0b
Parent:
17:077712e4e5e3
Child:
19:9886f667a62a
added in all libraries. this still works with the app.

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
jelord 17:077712e4e5e3 10 #include "SDFileSystem.h"
jelord 17:077712e4e5e3 11 #include "Adafruit_ADS1015.h"
jelord 17:077712e4e5e3 12 #include "MCP40D17.h"
jelord 17:077712e4e5e3 13 #include "BME280.h"
caseyquinn 18:c911a8928d0b 14 #include "Calibration_one.h"
jelord 17:077712e4e5e3 15
jelord 17:077712e4e5e3 16 //List of files not currently in this Code
caseyquinn 18:c911a8928d0b 17 #include "LSM303.h"
caseyquinn 18:c911a8928d0b 18 #include "SI1145.h"
caseyquinn 18:c911a8928d0b 19 #include "STC3100.h"
jelord 17:077712e4e5e3 20 //#include "US_Menu.h"
jelord 17:077712e4e5e3 21
jelord 17:077712e4e5e3 22
rgrover1 0:28f095301cb2 23
jelord 11:1058647c66e8 24 DigitalOut blower(p29, 0);
jelord 11:1058647c66e8 25 DigitalOut pbKill(p18, 1);
jelord 11:1058647c66e8 26 DigitalOut GPS_EN(p4,0);
jelord 11:1058647c66e8 27 EEPROM E2PROM(p22, p20);
jelord 11:1058647c66e8 28 CronoDot RTC(p22, p20);
jelord 12:27273e6a50b3 29 NCP5623BMUTBG RGB_LED(p22, p20);
caseyquinn 18:c911a8928d0b 30 Serial pc(USBTX, USBRX);
caseyquinn 18:c911a8928d0b 31 LSM303 movementsensor(p22, p20);
caseyquinn 18:c911a8928d0b 32 SI1145 lightsensor(p22, p20);
caseyquinn 18:c911a8928d0b 33 STC3100 gasG(p22, p20);
jelord 17:077712e4e5e3 34 I2C i2c(p22, p20);
jelord 17:077712e4e5e3 35 Adafruit_ADS1115 ads(&i2c);
caseyquinn 18:c911a8928d0b 36 Calibration calibrations(7); //Default serial/calibration if there are no values for the selected option
jelord 17:077712e4e5e3 37
jelord 17:077712e4e5e3 38
jelord 16:e066ab7e8fb3 39 BLE ble;
jelord 11:1058647c66e8 40 /*EEPROM ADDRESSING:
jelord 11:1058647c66e8 41 0:Status bit-Unused
jelord 11:1058647c66e8 42 1-15:Device Name
jelord 11:1058647c66e8 43 16-19:Flow Rate
jelord 11:1058647c66e8 44 20: Data Log Interval
jelord 11:1058647c66e8 45 21-26: Start Time: ssmmHHddMMyy
jelord 11:1058647c66e8 46 27-32: Stop Time: ssmmHHddMMyy
jelord 11:1058647c66e8 47 33: Duty Up
jelord 11:1058647c66e8 48 34: Duty Down
jelord 11:1058647c66e8 49 35-38: Home Latitude
jelord 11:1058647c66e8 50 39-42: Home Longitude
jelord 11:1058647c66e8 51 43-46: Work Latitude
jelord 11:1058647c66e8 52 47-50: Work Longitude
jelord 11:1058647c66e8 53 51: Runready: Currently useless, should be 0
jelord 11:1058647c66e8 54 52-53: Device Calibration
jelord 11:1058647c66e8 55 54: Consider RunReady
jelord 11:1058647c66e8 56 55-56: Menu Options
jelord 11:1058647c66e8 57 57+ Nothing*/
rgrover1 0:28f095301cb2 58
jelord 17:077712e4e5e3 59 Timeout stop; //This is the stop call back object
jelord 17:077712e4e5e3 60 Timeout logg; //This is the logging call back object
jelord 17:077712e4e5e3 61
jelord 17:077712e4e5e3 62 uint16_t serial_num = 1; // Default serial/calibration number
jelord 17:077712e4e5e3 63 int RunReady =0;
jelord 17:077712e4e5e3 64
jelord 17:077712e4e5e3 65
jelord 10:66549fa08986 66 const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to
jelord 10:66549fa08986 67 static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID
rgrover1 0:28f095301cb2 68
jelord 10:66549fa08986 69 UPAS_Service *upasServicePtr;
rgrover1 0:28f095301cb2 70
jelord 12:27273e6a50b3 71
jelord 17:077712e4e5e3 72
jelord 10:66549fa08986 73 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect
rgrover1 0:28f095301cb2 74 {
rgrover1 6:18d8750f39ee 75 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 76 }
rgrover1 0:28f095301cb2 77
rgrover1 0:28f095301cb2 78 void periodicCallback(void)
rgrover1 0:28f095301cb2 79 {
jelord 12:27273e6a50b3 80 RTC.get_time();
jelord 12:27273e6a50b3 81 const uint8_t refreshPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
jelord 12:27273e6a50b3 82 ble.updateCharacteristicValue( upasServicePtr->rtcCharacteristic.getValueHandle(),refreshPassValues,6);
jelord 17:077712e4e5e3 83
jelord 17:077712e4e5e3 84 }
jelord 17:077712e4e5e3 85 void periodicPrint(void)
jelord 17:077712e4e5e3 86 {
jelord 17:077712e4e5e3 87 // led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
jelord 17:077712e4e5e3 88 //RGB_LED.set_led(1,1,5);
jelord 17:077712e4e5e3 89
jelord 11:1058647c66e8 90 }
jelord 13:b43ec7e0cc1d 91
jelord 17:077712e4e5e3 92 /*
jelord 17:077712e4e5e3 93 This function is called the BLE GATT Server every time a characterisitic is written to
jelord 17:077712e4e5e3 94 All logic revolving around which bits to write to EEPROM, and what to see the RTC to us done here
jelord 17:077712e4e5e3 95 */
jelord 12:27273e6a50b3 96 void writeCharacteristicCallback(const GattWriteCallbackParams *params)
jelord 10:66549fa08986 97 {
jelord 12:27273e6a50b3 98 uint8_t *writeData = const_cast<uint8_t*>(params->data);
jelord 10:66549fa08986 99 // check to see what characteristic was written, by handle
jelord 12:27273e6a50b3 100 if(params->handle == upasServicePtr->rtcCharacteristic.getValueHandle()) {
jelord 12:27273e6a50b3 101
jelord 12:27273e6a50b3 102 //ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len);
jelord 12:27273e6a50b3 103
jelord 17:077712e4e5e3 104 E2PROM.write(0x00015, writeData+6, 12);
jelord 11:1058647c66e8 105 RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
jelord 12:27273e6a50b3 106
jelord 12:27273e6a50b3 107 }else if(params->handle == upasServicePtr->sampleTimeCharacteristic.getValueHandle()){
jelord 12:27273e6a50b3 108
jelord 12:27273e6a50b3 109 E2PROM.write(0x00015, writeData, 12);
jelord 13:b43ec7e0cc1d 110
jelord 13:b43ec7e0cc1d 111 }else if(params->handle == upasServicePtr->subjectLabelCharacteristic.getValueHandle()){
jelord 13:b43ec7e0cc1d 112 E2PROM.write(0x00001,writeData,15);
jelord 13:b43ec7e0cc1d 113
jelord 15:c9c93454dd56 114 }else if(params->handle == upasServicePtr->runReadyCharacteristic.getValueHandle()){
jelord 17:077712e4e5e3 115 uint8_t runData = writeData[0];
jelord 17:077712e4e5e3 116
jelord 17:077712e4e5e3 117 if(runData == 10){
jelord 17:077712e4e5e3 118 RunReady = 10;
jelord 17:077712e4e5e3 119 RGB_LED.set_led(1,2,3);
jelord 17:077712e4e5e3 120 }else{
jelord 17:077712e4e5e3 121 RunReady = 2;
jelord 17:077712e4e5e3 122 }
jelord 13:b43ec7e0cc1d 123
jelord 14:4fc1788b8ad2 124 }else if(params->handle == upasServicePtr->runModeCharacteristic.getValueHandle()){
jelord 13:b43ec7e0cc1d 125 /* Trigger demo mode*/
jelord 13:b43ec7e0cc1d 126 RGB_LED.set_led(3,1,0);
jelord 14:4fc1788b8ad2 127 E2PROM.write(0x00036,writeData,1);
jelord 10:66549fa08986 128 }
jelord 10:66549fa08986 129 }
jelord 10:66549fa08986 130
rgrover1 0:28f095301cb2 131 int main(void)
rgrover1 0:28f095301cb2 132 {
rgrover1 0:28f095301cb2 133 Ticker ticker;
jelord 17:077712e4e5e3 134 ticker.attach(periodicPrint, 600); //currently unused. But do not want to comment out
jelord 11:1058647c66e8 135 RGB_LED.set_led(1,1,1);
jelord 11:1058647c66e8 136 RTC.get_time();
jelord 12:27273e6a50b3 137 uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
jelord 12:27273e6a50b3 138 uint8_t sampleTimePassValues[12] = {0,};
jelord 12:27273e6a50b3 139 uint8_t subjectLabelOriginal[15] = {0,};
jelord 12:27273e6a50b3 140 E2PROM.read(0x00015, sampleTimePassValues, 12);
jelord 12:27273e6a50b3 141 E2PROM.read(0x00001, subjectLabelOriginal,15);
rgrover1 0:28f095301cb2 142
rgrover1 0:28f095301cb2 143 ble.init();
rgrover1 6:18d8750f39ee 144 ble.gap().onDisconnection(disconnectionCallback);
jelord 12:27273e6a50b3 145 ble.gattServer().onDataWritten(writeCharacteristicCallback); //add writeCharCallback (custom function) to whenever data is being written to device
jelord 12:27273e6a50b3 146 UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal); //Create a GattService that is defined in UPAS_Service.h
jelord 10:66549fa08986 147 upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service
rgrover1 0:28f095301cb2 148
jelord 10:66549fa08986 149 /* setup advertising
jelord 10:66549fa08986 150 Following lines do the follow:
jelord 10:66549fa08986 151 1:Declare the device as Bluetooth Smart(Low-Energy)
jelord 10:66549fa08986 152 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM
jelord 10:66549fa08986 153 3.Advertise the name that will be associated with the UPAS
jelord 10:66549fa08986 154 4.Allow the UPAS to advertise unrestricted (this might change) */
jelord 10:66549fa08986 155
rgrover1 6:18d8750f39ee 156 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:18d8750f39ee 157 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:18d8750f39ee 158 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:18d8750f39ee 159 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jelord 17:077712e4e5e3 160 ble.gap().setAdvertisingInterval(30); /* 160ms. */
rgrover1 6:18d8750f39ee 161 ble.gap().startAdvertising();
jelord 10:66549fa08986 162
jelord 10:66549fa08986 163 //Loop keeps device in infinite loop waiting for events to occur
jelord 13:b43ec7e0cc1d 164 while (1) {
jelord 17:077712e4e5e3 165 ble.waitForEvent();
jelord 17:077712e4e5e3 166 if(RunReady==2 && blower ==0){ //Code used to see if one-click run should begin
jelord 17:077712e4e5e3 167 calibrations.initialize(serial_num);
jelord 17:077712e4e5e3 168 RGB_LED.set_led(3,0,2);
jelord 17:077712e4e5e3 169 blower=1;
jelord 17:077712e4e5e3 170 RunReady=0;
jelord 17:077712e4e5e3 171 }
jelord 17:077712e4e5e3 172 if(RunReady==2 && blower ==1){ //Code used to see if one-click run is done.
jelord 17:077712e4e5e3 173 blower=0;
jelord 17:077712e4e5e3 174 RunReady=0;
jelord 17:077712e4e5e3 175 }
jelord 17:077712e4e5e3 176
jelord 17:077712e4e5e3 177 if(RunReady==10){ //Check to see if app is done with configurations
jelord 17:077712e4e5e3 178 blower = 0;
jelord 17:077712e4e5e3 179 ble.gap().stopAdvertising();
jelord 17:077712e4e5e3 180 break;
jelord 17:077712e4e5e3 181 }
rgrover1 0:28f095301cb2 182 }
jelord 17:077712e4e5e3 183 uint8_t startAndEndTime[12] = {0,};
jelord 17:077712e4e5e3 184 E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
jelord 17:077712e4e5e3 185 while(!RTC.compare(startAndEndTime[0], startAndEndTime[1], startAndEndTime[2], startAndEndTime[3], startAndEndTime[4], startAndEndTime[5])) { // this while waits for the start time by looping until the start time
jelord 17:077712e4e5e3 186 wait(0.5);
jelord 17:077712e4e5e3 187 RTC.get_time();
jelord 17:077712e4e5e3 188
jelord 17:077712e4e5e3 189 }
jelord 17:077712e4e5e3 190 calibrations.initialize(serial_num);
jelord 17:077712e4e5e3 191 blower=1;
jelord 17:077712e4e5e3 192
jelord 17:077712e4e5e3 193 while(!RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) { //Waits for end time
jelord 17:077712e4e5e3 194 wait(0.5);
jelord 17:077712e4e5e3 195 RTC.get_time();
jelord 17:077712e4e5e3 196
jelord 17:077712e4e5e3 197 }
jelord 17:077712e4e5e3 198 pbKill = 0; // this is were we shut everything down
jelord 17:077712e4e5e3 199
rgrover1 0:28f095301cb2 200 }