Graduation Thesis, use Nucleo and X-Nucleo BLE
Dependencies: PulseSensor GSM Thermometer KalmanFilter
application/main.cpp@6:26a4b005cb75, 2018-02-21 (annotated)
- Committer:
- DuyLionTran
- Date:
- Wed Feb 21 03:43:08 2018 +0000
- Revision:
- 6:26a4b005cb75
- Parent:
- 5:c12c16c0d2ea
- Child:
- 7:d8032114995b
* version 0.9.6 02-21-2018 Update mbed-os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuyLionTran | 0:64ca984b3efd | 1 | /** |
DuyLionTran | 2:16f6cfcd7505 | 2 | * This is the project for "BLE HealthCare". The device is attached on any patient's body at will. |
DuyLionTran | 0:64ca984b3efd | 3 | |
DuyLionTran | 0:64ca984b3efd | 4 | * Revision: |
DuyLionTran | 2:16f6cfcd7505 | 5 | * version 0.8 02-12-2018 |
DuyLionTran | 2:16f6cfcd7505 | 6 | * version 0.8.5 02-14-2018 |
DuyLionTran | 4:e44cd8682f1c | 7 | * version 0.9 02-15-2018 Pulse sensor and thermometer added |
DuyLionTran | 4:e44cd8682f1c | 8 | * version 0.9.5 02-16-2018 Calculation for pulse sensor and thermometer. GSM library added |
DuyLionTran | 6:26a4b005cb75 | 9 | * version 0.9.6 02-21-2018 Update mbed-os |
DuyLionTran | 0:64ca984b3efd | 10 | |
DuyLionTran | 0:64ca984b3efd | 11 | /* ======================== INCLUDES ========================= */ |
DuyLionTran | 0:64ca984b3efd | 12 | #include <events/mbed_events.h> |
DuyLionTran | 0:64ca984b3efd | 13 | #include <mbed.h> |
DuyLionTran | 0:64ca984b3efd | 14 | #include "ble/BLE.h" |
DuyLionTran | 0:64ca984b3efd | 15 | #include "ble_healthcare_service.h" |
DuyLionTran | 5:c12c16c0d2ea | 16 | #include "GSM.h" |
DuyLionTran | 4:e44cd8682f1c | 17 | #include "LM35.h" |
DuyLionTran | 3:9b552b775c6e | 18 | #include "PulseSensor.h" |
DuyLionTran | 0:64ca984b3efd | 19 | |
DuyLionTran | 4:e44cd8682f1c | 20 | |
DuyLionTran | 4:e44cd8682f1c | 21 | |
DuyLionTran | 0:64ca984b3efd | 22 | /* ======================== DEFINES ========================== */ |
DuyLionTran | 4:e44cd8682f1c | 23 | #define THERM_FINGER_LOCATION 3 |
DuyLionTran | 4:e44cd8682f1c | 24 | #define HRM_FINGER_LOCATION 3 |
DuyLionTran | 4:e44cd8682f1c | 25 | |
DuyLionTran | 4:e44cd8682f1c | 26 | #define PULSE_SENSOR_PIN A0 |
DuyLionTran | 4:e44cd8682f1c | 27 | #define THERM_SENSOR_PIN A1 |
DuyLionTran | 0:64ca984b3efd | 28 | |
DuyLionTran | 0:64ca984b3efd | 29 | /* ======================= VARIABLES ========================= */ |
DuyLionTran | 0:64ca984b3efd | 30 | /* GLOBAL VARIABLES */ |
DuyLionTran | 0:64ca984b3efd | 31 | static float currentTemperature = 39.6; |
DuyLionTran | 0:64ca984b3efd | 32 | static uint8_t currentHRMCounter = 80; |
DuyLionTran | 0:64ca984b3efd | 33 | |
DuyLionTran | 0:64ca984b3efd | 34 | /* PRIVATE VARIABLES */ |
DuyLionTran | 0:64ca984b3efd | 35 | const static char DEVICE_NAME[] = "HEALTH STATE"; |
DuyLionTran | 3:9b552b775c6e | 36 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
DuyLionTran | 0:64ca984b3efd | 37 | |
DuyLionTran | 0:64ca984b3efd | 38 | /* STRUCTS/CLASSESS */ |
DuyLionTran | 0:64ca984b3efd | 39 | static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20); |
DuyLionTran | 0:64ca984b3efd | 40 | HealthCareService *HealthCareServicePtr; |
DuyLionTran | 3:9b552b775c6e | 41 | //PulseSensor PulseSensor(); |
DuyLionTran | 0:64ca984b3efd | 42 | |
DuyLionTran | 4:e44cd8682f1c | 43 | Serial serial(USBTX, USBRX); |
DuyLionTran | 4:e44cd8682f1c | 44 | |
DuyLionTran | 0:64ca984b3efd | 45 | /* ================== FUNCTION PROTOTYPES ==================== */ |
DuyLionTran | 4:e44cd8682f1c | 46 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); |
DuyLionTran | 4:e44cd8682f1c | 47 | void onBleInitError(BLE &ble, ble_error_t error); |
DuyLionTran | 4:e44cd8682f1c | 48 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); |
DuyLionTran | 4:e44cd8682f1c | 49 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context); |
DuyLionTran | 4:e44cd8682f1c | 50 | |
DuyLionTran | 4:e44cd8682f1c | 51 | void main_event(void); |
DuyLionTran | 4:e44cd8682f1c | 52 | void periodicCallback(void); |
DuyLionTran | 0:64ca984b3efd | 53 | |
DuyLionTran | 0:64ca984b3efd | 54 | /* ==================== FUNCTION DETAILS ===================== */ |
DuyLionTran | 0:64ca984b3efd | 55 | /* Restart Advertising on disconnection*/ |
DuyLionTran | 0:64ca984b3efd | 56 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { |
DuyLionTran | 0:64ca984b3efd | 57 | BLE::Instance().gap().startAdvertising(); |
DuyLionTran | 0:64ca984b3efd | 58 | } |
DuyLionTran | 0:64ca984b3efd | 59 | |
DuyLionTran | 0:64ca984b3efd | 60 | void main_event(void) { |
DuyLionTran | 0:64ca984b3efd | 61 | /* Do blocking calls or whatever is necessary for sensor polling. |
DuyLionTran | 0:64ca984b3efd | 62 | In our case, we simply update the Temperature measurement. */ |
DuyLionTran | 0:64ca984b3efd | 63 | currentTemperature = (currentTemperature + 0.1 > 43.0) ? 39.6 : currentTemperature + 0.1; |
DuyLionTran | 0:64ca984b3efd | 64 | currentHRMCounter = (currentHRMCounter + 1 > 120) ? 80 : currentHRMCounter + 1; |
DuyLionTran | 4:e44cd8682f1c | 65 | if (BLE::Instance().gap().getState().connected) { |
DuyLionTran | 4:e44cd8682f1c | 66 | HealthCareServicePtr->updateTemperature(currentTemperature); |
DuyLionTran | 4:e44cd8682f1c | 67 | HealthCareServicePtr->updateHeartRate(currentHRMCounter); |
DuyLionTran | 4:e44cd8682f1c | 68 | } |
DuyLionTran | 4:e44cd8682f1c | 69 | |
DuyLionTran | 0:64ca984b3efd | 70 | } |
DuyLionTran | 0:64ca984b3efd | 71 | |
DuyLionTran | 0:64ca984b3efd | 72 | void periodicCallback(void) { |
DuyLionTran | 4:e44cd8682f1c | 73 | /* call main_event immediately */ |
DuyLionTran | 4:e44cd8682f1c | 74 | eventQueue.call(main_event); |
DuyLionTran | 0:64ca984b3efd | 75 | } |
DuyLionTran | 0:64ca984b3efd | 76 | |
DuyLionTran | 0:64ca984b3efd | 77 | void printMacAddress() { |
DuyLionTran | 0:64ca984b3efd | 78 | /* Print out device MAC address to the console*/ |
DuyLionTran | 0:64ca984b3efd | 79 | Gap::AddressType_t addr_type; |
DuyLionTran | 0:64ca984b3efd | 80 | Gap::Address_t address; |
DuyLionTran | 0:64ca984b3efd | 81 | BLE::Instance().gap().getAddress(&addr_type, address); |
DuyLionTran | 0:64ca984b3efd | 82 | printf("DEVICE MAC ADDRESS: "); |
DuyLionTran | 0:64ca984b3efd | 83 | for (int i = 5; i >= 1; i--) { |
DuyLionTran | 0:64ca984b3efd | 84 | printf("%02x:", address[i]); |
DuyLionTran | 0:64ca984b3efd | 85 | } |
DuyLionTran | 0:64ca984b3efd | 86 | printf("%02x\r\n", address[0]); |
DuyLionTran | 0:64ca984b3efd | 87 | } |
DuyLionTran | 0:64ca984b3efd | 88 | |
DuyLionTran | 0:64ca984b3efd | 89 | void onBleInitError(BLE &ble, ble_error_t error) { |
DuyLionTran | 0:64ca984b3efd | 90 | /* Initialization error handling should go here */ |
DuyLionTran | 4:e44cd8682f1c | 91 | |
DuyLionTran | 0:64ca984b3efd | 92 | } |
DuyLionTran | 0:64ca984b3efd | 93 | |
DuyLionTran | 0:64ca984b3efd | 94 | /** |
DuyLionTran | 0:64ca984b3efd | 95 | * @brief Callback triggered when the ble initialization process has finished |
DuyLionTran | 0:64ca984b3efd | 96 | */ |
DuyLionTran | 0:64ca984b3efd | 97 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { |
DuyLionTran | 0:64ca984b3efd | 98 | BLE& ble = params->ble; |
DuyLionTran | 0:64ca984b3efd | 99 | ble_error_t error = params->error; |
DuyLionTran | 0:64ca984b3efd | 100 | |
DuyLionTran | 0:64ca984b3efd | 101 | if (error != BLE_ERROR_NONE) { |
DuyLionTran | 0:64ca984b3efd | 102 | onBleInitError(ble, error); |
DuyLionTran | 0:64ca984b3efd | 103 | return; |
DuyLionTran | 0:64ca984b3efd | 104 | } |
DuyLionTran | 0:64ca984b3efd | 105 | |
DuyLionTran | 0:64ca984b3efd | 106 | /* Ensure that it is the default instance of BLE */ |
DuyLionTran | 0:64ca984b3efd | 107 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
DuyLionTran | 0:64ca984b3efd | 108 | return; |
DuyLionTran | 0:64ca984b3efd | 109 | } |
DuyLionTran | 0:64ca984b3efd | 110 | |
DuyLionTran | 0:64ca984b3efd | 111 | ble.gap().onDisconnection(disconnectionCallback); |
DuyLionTran | 4:e44cd8682f1c | 112 | HealthCareServicePtr = new HealthCareService(ble, currentTemperature, THERM_FINGER_LOCATION, |
DuyLionTran | 4:e44cd8682f1c | 113 | currentHRMCounter, HRM_FINGER_LOCATION); |
DuyLionTran | 0:64ca984b3efd | 114 | |
DuyLionTran | 0:64ca984b3efd | 115 | /* setup advertising */ |
DuyLionTran | 0:64ca984b3efd | 116 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
DuyLionTran | 0:64ca984b3efd | 117 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
DuyLionTran | 2:16f6cfcd7505 | 118 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER ); |
DuyLionTran | 0:64ca984b3efd | 119 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
DuyLionTran | 0:64ca984b3efd | 120 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
DuyLionTran | 0:64ca984b3efd | 121 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
DuyLionTran | 0:64ca984b3efd | 122 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
DuyLionTran | 0:64ca984b3efd | 123 | ble.gap().startAdvertising(); |
DuyLionTran | 0:64ca984b3efd | 124 | |
DuyLionTran | 0:64ca984b3efd | 125 | printMacAddress(); |
DuyLionTran | 0:64ca984b3efd | 126 | } |
DuyLionTran | 0:64ca984b3efd | 127 | |
DuyLionTran | 0:64ca984b3efd | 128 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
DuyLionTran | 0:64ca984b3efd | 129 | BLE &ble = BLE::Instance(); |
DuyLionTran | 0:64ca984b3efd | 130 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
DuyLionTran | 0:64ca984b3efd | 131 | } |
DuyLionTran | 0:64ca984b3efd | 132 | |
DuyLionTran | 0:64ca984b3efd | 133 | /* MAIN FUNCTION */ |
DuyLionTran | 0:64ca984b3efd | 134 | int main() { |
DuyLionTran | 4:e44cd8682f1c | 135 | |
DuyLionTran | 4:e44cd8682f1c | 136 | serial.baud(115200); |
DuyLionTran | 4:e44cd8682f1c | 137 | printf("\r\n BODY WIRELESS SENSOR NETWORK\r\n"); |
DuyLionTran | 4:e44cd8682f1c | 138 | /* call periodicCallback every 500ms */ |
DuyLionTran | 0:64ca984b3efd | 139 | eventQueue.call_every(500, periodicCallback); |
DuyLionTran | 4:e44cd8682f1c | 140 | |
DuyLionTran | 4:e44cd8682f1c | 141 | /* init BLE */ |
DuyLionTran | 0:64ca984b3efd | 142 | BLE &ble = BLE::Instance(); |
DuyLionTran | 0:64ca984b3efd | 143 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
DuyLionTran | 0:64ca984b3efd | 144 | ble.init(bleInitComplete); |
DuyLionTran | 4:e44cd8682f1c | 145 | |
DuyLionTran | 4:e44cd8682f1c | 146 | /* dispatch the event queue */ |
DuyLionTran | 0:64ca984b3efd | 147 | eventQueue.dispatch_forever(); |
DuyLionTran | 0:64ca984b3efd | 148 | |
DuyLionTran | 0:64ca984b3efd | 149 | return 0; |
DuyLionTran | 0:64ca984b3efd | 150 | } |