![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
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
main.cpp@12:27273e6a50b3, 2015-10-26 (annotated)
- Committer:
- jelord
- Date:
- Mon Oct 26 21:27:48 2015 +0000
- Revision:
- 12:27273e6a50b3
- Parent:
- 11:1058647c66e8
- Child:
- 13:b43ec7e0cc1d
Added characteristic to UPAS_SERVICE.h that advertises the subject label from the EEPROM
Who changed what in which revision?
User | Revision | Line number | New 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 | 12:27273e6a50b3 | 9 | #include "SDFileSystem.h" |
jelord | 12:27273e6a50b3 | 10 | #include "Adafruit_ADS1015.h" |
jelord | 12:27273e6a50b3 | 11 | #include "MCP40D17.h" |
jelord | 12:27273e6a50b3 | 12 | #include "STC3100.h" |
jelord | 12:27273e6a50b3 | 13 | #include "LSM303.h" |
jelord | 12:27273e6a50b3 | 14 | #include "BME280.h" |
jelord | 12:27273e6a50b3 | 15 | #include "Calibration.h" |
rgrover1 | 0:28f095301cb2 | 16 | |
rgrover1 | 6:18d8750f39ee | 17 | BLE ble; |
jelord | 11:1058647c66e8 | 18 | //DigitalOut led1(LED1); //Use of leds is important for debugging |
jelord | 11:1058647c66e8 | 19 | DigitalOut blower(p29, 0); |
jelord | 11:1058647c66e8 | 20 | DigitalOut pbKill(p18, 1); |
jelord | 11:1058647c66e8 | 21 | DigitalOut GPS_EN(p4,0); |
jelord | 11:1058647c66e8 | 22 | EEPROM E2PROM(p22, p20); |
jelord | 11:1058647c66e8 | 23 | CronoDot RTC(p22, p20); |
jelord | 12:27273e6a50b3 | 24 | NCP5623BMUTBG RGB_LED(p22, p20); |
jelord | 12:27273e6a50b3 | 25 | Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option |
jelord | 12:27273e6a50b3 | 26 | LSM303 movementsensor(p22, p20); |
jelord | 12:27273e6a50b3 | 27 | I2C i2c(p22, p20); |
jelord | 12:27273e6a50b3 | 28 | Adafruit_ADS1115 ads(&i2c); |
jelord | 12:27273e6a50b3 | 29 | MCP40D17 DigPot(&i2c); |
jelord | 12:27273e6a50b3 | 30 | BME280 bmesensor(p22, p20); |
jelord | 12:27273e6a50b3 | 31 | STC3100 gasG(p22, p20); |
jelord | 11:1058647c66e8 | 32 | /*EEPROM ADDRESSING: |
jelord | 11:1058647c66e8 | 33 | 0:Status bit-Unused |
jelord | 11:1058647c66e8 | 34 | 1-15:Device Name |
jelord | 11:1058647c66e8 | 35 | 16-19:Flow Rate |
jelord | 11:1058647c66e8 | 36 | 20: Data Log Interval |
jelord | 11:1058647c66e8 | 37 | 21-26: Start Time: ssmmHHddMMyy |
jelord | 11:1058647c66e8 | 38 | 27-32: Stop Time: ssmmHHddMMyy |
jelord | 11:1058647c66e8 | 39 | 33: Duty Up |
jelord | 11:1058647c66e8 | 40 | 34: Duty Down |
jelord | 11:1058647c66e8 | 41 | 35-38: Home Latitude |
jelord | 11:1058647c66e8 | 42 | 39-42: Home Longitude |
jelord | 11:1058647c66e8 | 43 | 43-46: Work Latitude |
jelord | 11:1058647c66e8 | 44 | 47-50: Work Longitude |
jelord | 11:1058647c66e8 | 45 | 51: Runready: Currently useless, should be 0 |
jelord | 11:1058647c66e8 | 46 | 52-53: Device Calibration |
jelord | 11:1058647c66e8 | 47 | 54: Consider RunReady |
jelord | 11:1058647c66e8 | 48 | 55-56: Menu Options |
jelord | 11:1058647c66e8 | 49 | 57+ Nothing*/ |
rgrover1 | 0:28f095301cb2 | 50 | |
jelord | 12:27273e6a50b3 | 51 | Timeout stop; //This is the stop call back object |
jelord | 12:27273e6a50b3 | 52 | Timeout logg; //This is the logging call back object |
jelord | 10:66549fa08986 | 53 | const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to |
jelord | 10:66549fa08986 | 54 | static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID |
rgrover1 | 0:28f095301cb2 | 55 | |
jelord | 10:66549fa08986 | 56 | UPAS_Service *upasServicePtr; |
rgrover1 | 0:28f095301cb2 | 57 | |
jelord | 12:27273e6a50b3 | 58 | float press,temp,rh; |
jelord | 12:27273e6a50b3 | 59 | int uv,vis,ir; |
jelord | 12:27273e6a50b3 | 60 | float accel_x ,accel_y ,accel_z,angle_x,angle_y,angle_z,compass,accel_comp,mag_x,mag_y,mag_z,massflow,volflow,omronVolt,atmoRho; |
jelord | 12:27273e6a50b3 | 61 | |
jelord | 12:27273e6a50b3 | 62 | int vInReading,vBlowerReading,omronDiff,omronReading,logInerval = 10; |
jelord | 12:27273e6a50b3 | 63 | |
jelord | 12:27273e6a50b3 | 64 | float volflowSet = 1.0; |
jelord | 12:27273e6a50b3 | 65 | //int logInerval = 10; |
jelord | 12:27273e6a50b3 | 66 | double secondsD = 0,lastsecondD = 0; |
jelord | 12:27273e6a50b3 | 67 | float massflowSet; |
jelord | 12:27273e6a50b3 | 68 | float deltaVflow = 0.0,deltaMflow = 0.0; |
jelord | 12:27273e6a50b3 | 69 | float gainFlow; |
jelord | 12:27273e6a50b3 | 70 | float sampledVol; //L, total sampled volume |
jelord | 12:27273e6a50b3 | 71 | |
jelord | 12:27273e6a50b3 | 72 | int digital_pot_setpoint,digital_pot_set,digital_pot_change; //min = 0x7F, max = 0x00 |
jelord | 12:27273e6a50b3 | 73 | int digitalpotMax = 127; |
jelord | 12:27273e6a50b3 | 74 | int digitalpotMin = 2; |
jelord | 12:27273e6a50b3 | 75 | |
jelord | 12:27273e6a50b3 | 76 | int dutyUp,dutyDown; |
jelord | 12:27273e6a50b3 | 77 | |
jelord | 12:27273e6a50b3 | 78 | // variables are only place holders for the US_Menu // |
jelord | 12:27273e6a50b3 | 79 | int refreshtime; |
jelord | 12:27273e6a50b3 | 80 | float home_lat, home_lon, work_lat, work_lon; |
jelord | 12:27273e6a50b3 | 81 | //*************************************************// |
jelord | 12:27273e6a50b3 | 82 | |
jelord | 12:27273e6a50b3 | 83 | char device_name[] = "---------------"; |
jelord | 12:27273e6a50b3 | 84 | char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt"; |
jelord | 12:27273e6a50b3 | 85 | SDFileSystem sd(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, SPIS_PSELSS, "sd"); |
jelord | 12:27273e6a50b3 | 86 | |
jelord | 12:27273e6a50b3 | 87 | //FLAG: NEED FOR MY CODE |
jelord | 12:27273e6a50b3 | 88 | //void check_stop() // this checks if it's time to stop and shutdown |
jelord | 12:27273e6a50b3 | 89 | //{ |
jelord | 12:27273e6a50b3 | 90 | // |
jelord | 12:27273e6a50b3 | 91 | // if(RTC.compare(Menu.f_sec, Menu.f_min, Menu.f_hour, Menu.f_day, Menu.f_month, Menu.f_year)) { |
jelord | 12:27273e6a50b3 | 92 | // pbKill = 0; // this is were we shut everything down |
jelord | 12:27273e6a50b3 | 93 | // printf("If you're reading this something has gone very wrong."); |
jelord | 12:27273e6a50b3 | 94 | // } |
jelord | 12:27273e6a50b3 | 95 | // stop.detach(); |
jelord | 12:27273e6a50b3 | 96 | // stop.attach(&check_stop, 9); |
jelord | 12:27273e6a50b3 | 97 | //} |
jelord | 12:27273e6a50b3 | 98 | |
jelord | 12:27273e6a50b3 | 99 | |
jelord | 12:27273e6a50b3 | 100 | |
jelord | 12:27273e6a50b3 | 101 | |
jelord | 12:27273e6a50b3 | 102 | |
jelord | 12:27273e6a50b3 | 103 | |
jelord | 12:27273e6a50b3 | 104 | //MARK: THIS IS ALL CODE NOT TAKEN FROM OTHER MENU>>>DONT ALTER!!! |
jelord | 10:66549fa08986 | 105 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect |
rgrover1 | 0:28f095301cb2 | 106 | { |
rgrover1 | 6:18d8750f39ee | 107 | ble.gap().startAdvertising(); |
rgrover1 | 0:28f095301cb2 | 108 | } |
rgrover1 | 0:28f095301cb2 | 109 | |
rgrover1 | 0:28f095301cb2 | 110 | void periodicCallback(void) |
rgrover1 | 0:28f095301cb2 | 111 | { |
jelord | 11:1058647c66e8 | 112 | // led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ |
rgrover1 | 0:28f095301cb2 | 113 | } |
jelord | 11:1058647c66e8 | 114 | void readCharCallin(const GattReadCallbackParams *eventDataP) |
jelord | 11:1058647c66e8 | 115 | { |
jelord | 12:27273e6a50b3 | 116 | RTC.get_time(); |
jelord | 12:27273e6a50b3 | 117 | const uint8_t refreshPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year}; |
jelord | 12:27273e6a50b3 | 118 | ble.updateCharacteristicValue( upasServicePtr->rtcCharacteristic.getValueHandle(),refreshPassValues,6); |
jelord | 11:1058647c66e8 | 119 | } |
jelord | 12:27273e6a50b3 | 120 | void writeCharacteristicCallback(const GattWriteCallbackParams *params) |
jelord | 10:66549fa08986 | 121 | { |
jelord | 12:27273e6a50b3 | 122 | uint8_t *writeData = const_cast<uint8_t*>(params->data); |
jelord | 10:66549fa08986 | 123 | // check to see what characteristic was written, by handle |
jelord | 12:27273e6a50b3 | 124 | if(params->handle == upasServicePtr->rtcCharacteristic.getValueHandle()) { |
jelord | 12:27273e6a50b3 | 125 | |
jelord | 12:27273e6a50b3 | 126 | //ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len); |
jelord | 12:27273e6a50b3 | 127 | |
jelord | 12:27273e6a50b3 | 128 | // E2PROM.write(0x00015, writeData+6, 12); |
jelord | 11:1058647c66e8 | 129 | RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]); |
jelord | 12:27273e6a50b3 | 130 | |
jelord | 12:27273e6a50b3 | 131 | }else if(params->handle == upasServicePtr->sampleTimeCharacteristic.getValueHandle()){ |
jelord | 12:27273e6a50b3 | 132 | |
jelord | 12:27273e6a50b3 | 133 | E2PROM.write(0x00015, writeData, 12); |
jelord | 10:66549fa08986 | 134 | } |
jelord | 10:66549fa08986 | 135 | } |
jelord | 10:66549fa08986 | 136 | |
rgrover1 | 0:28f095301cb2 | 137 | int main(void) |
rgrover1 | 0:28f095301cb2 | 138 | { |
jelord | 11:1058647c66e8 | 139 | // led1 = 1; |
rgrover1 | 0:28f095301cb2 | 140 | Ticker ticker; |
rgrover1 | 0:28f095301cb2 | 141 | ticker.attach(periodicCallback, 1); |
jelord | 11:1058647c66e8 | 142 | RGB_LED.set_led(1,1,1); |
jelord | 11:1058647c66e8 | 143 | RTC.get_time(); |
jelord | 12:27273e6a50b3 | 144 | uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year}; |
jelord | 12:27273e6a50b3 | 145 | uint8_t sampleTimePassValues[12] = {0,}; |
jelord | 12:27273e6a50b3 | 146 | uint8_t subjectLabelOriginal[15] = {0,}; |
jelord | 12:27273e6a50b3 | 147 | E2PROM.read(0x00015, sampleTimePassValues, 12); |
jelord | 12:27273e6a50b3 | 148 | E2PROM.read(0x00001, subjectLabelOriginal,15); |
rgrover1 | 0:28f095301cb2 | 149 | |
rgrover1 | 0:28f095301cb2 | 150 | ble.init(); |
rgrover1 | 6:18d8750f39ee | 151 | ble.gap().onDisconnection(disconnectionCallback); |
jelord | 12:27273e6a50b3 | 152 | ble.gattServer().onDataWritten(writeCharacteristicCallback); //add writeCharCallback (custom function) to whenever data is being written to device |
jelord | 11:1058647c66e8 | 153 | ble.gattServer().onDataRead(readCharCallin); |
jelord | 12:27273e6a50b3 | 154 | UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal); //Create a GattService that is defined in UPAS_Service.h |
jelord | 10:66549fa08986 | 155 | upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service |
rgrover1 | 0:28f095301cb2 | 156 | |
jelord | 10:66549fa08986 | 157 | /* setup advertising |
jelord | 10:66549fa08986 | 158 | Following lines do the follow: |
jelord | 10:66549fa08986 | 159 | 1:Declare the device as Bluetooth Smart(Low-Energy) |
jelord | 10:66549fa08986 | 160 | 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM |
jelord | 10:66549fa08986 | 161 | 3.Advertise the name that will be associated with the UPAS |
jelord | 10:66549fa08986 | 162 | 4.Allow the UPAS to advertise unrestricted (this might change) */ |
jelord | 10:66549fa08986 | 163 | |
rgrover1 | 6:18d8750f39ee | 164 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 6:18d8750f39ee | 165 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 6:18d8750f39ee | 166 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 6:18d8750f39ee | 167 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jelord | 10:66549fa08986 | 168 | ble.gap().setAdvertisingInterval(160); /* 160ms. */ |
rgrover1 | 6:18d8750f39ee | 169 | ble.gap().startAdvertising(); |
jelord | 10:66549fa08986 | 170 | |
jelord | 10:66549fa08986 | 171 | //Loop keeps device in infinite loop waiting for events to occur |
rgrover1 | 0:28f095301cb2 | 172 | while (true) { |
rgrover1 | 0:28f095301cb2 | 173 | ble.waitForEvent(); |
rgrover1 | 0:28f095301cb2 | 174 | } |
rgrover1 | 0:28f095301cb2 | 175 | } |