Graduation Thesis, use Nucleo and X-Nucleo BLE
Dependencies: PulseSensor GSM Thermometer KalmanFilter
Application/main.cpp@18:a9d67c51715e, 2018-05-24 (annotated)
- Committer:
- DuyLionTran
- Date:
- Thu May 24 17:53:46 2018 +0000
- Revision:
- 18:a9d67c51715e
- Parent:
- 17:b7c2db3e7282
- Child:
- 19:a4f3328e9d09
developing connection callback
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 | 7:d8032114995b | 10 | * version 0.9.6 02-21-2018 Some modification for LM35 |
DuyLionTran | 8:3384286a8498 | 11 | * version 0.9.8 03-04-2018 Data receiving from client device added |
DuyLionTran | 9:be09a9bf2e2e | 12 | * version 1.0 03-09-2018 Some minor bugs fixed |
DuyLionTran | 10:4e0f5173269e | 13 | * version 1.0.5 03-09-2018 Some minor bugs fixed |
DuyLionTran | 0:64ca984b3efd | 14 | |
DuyLionTran | 0:64ca984b3efd | 15 | /* ======================== INCLUDES ========================= */ |
DuyLionTran | 0:64ca984b3efd | 16 | #include <events/mbed_events.h> |
DuyLionTran | 0:64ca984b3efd | 17 | #include <mbed.h> |
DuyLionTran | 0:64ca984b3efd | 18 | #include "ble/BLE.h" |
DuyLionTran | 0:64ca984b3efd | 19 | #include "ble_healthcare_service.h" |
DuyLionTran | 5:c12c16c0d2ea | 20 | #include "GSM.h" |
DuyLionTran | 4:e44cd8682f1c | 21 | #include "LM35.h" |
DuyLionTran | 3:9b552b775c6e | 22 | #include "PulseSensor.h" |
DuyLionTran | 0:64ca984b3efd | 23 | |
DuyLionTran | 4:e44cd8682f1c | 24 | |
DuyLionTran | 4:e44cd8682f1c | 25 | |
DuyLionTran | 0:64ca984b3efd | 26 | /* ======================== DEFINES ========================== */ |
DuyLionTran | 4:e44cd8682f1c | 27 | #define PULSE_SENSOR_PIN A0 |
DuyLionTran | 4:e44cd8682f1c | 28 | #define THERM_SENSOR_PIN A1 |
DuyLionTran | 0:64ca984b3efd | 29 | |
DuyLionTran | 0:64ca984b3efd | 30 | /* ======================= VARIABLES ========================= */ |
DuyLionTran | 0:64ca984b3efd | 31 | /* GLOBAL VARIABLES */ |
DuyLionTran | 17:b7c2db3e7282 | 32 | static float currentTemperature = 39.6; |
DuyLionTran | 17:b7c2db3e7282 | 33 | static uint16_t currentHRMCounter = 80; |
DuyLionTran | 17:b7c2db3e7282 | 34 | static float sendCombinedTempAndHR; |
DuyLionTran | 17:b7c2db3e7282 | 35 | static uint16_t sendCombinedHRAndTemp; |
DuyLionTran | 0:64ca984b3efd | 36 | |
DuyLionTran | 0:64ca984b3efd | 37 | /* PRIVATE VARIABLES */ |
DuyLionTran | 8:3384286a8498 | 38 | uint8_t HRM_increasement = 1; |
DuyLionTran | 9:be09a9bf2e2e | 39 | uint8_t cnt; |
DuyLionTran | 8:3384286a8498 | 40 | |
DuyLionTran | 17:b7c2db3e7282 | 41 | uint8_t startSendFloat = 0; |
DuyLionTran | 17:b7c2db3e7282 | 42 | uint8_t saveFloat; |
DuyLionTran | 17:b7c2db3e7282 | 43 | |
DuyLionTran | 9:be09a9bf2e2e | 44 | /* No need to display the device name */ |
DuyLionTran | 9:be09a9bf2e2e | 45 | //const static char DEVICE_NAME[] = "BODY SENSOR"; |
DuyLionTran | 9:be09a9bf2e2e | 46 | |
DuyLionTran | 8:3384286a8498 | 47 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
DuyLionTran | 8:3384286a8498 | 48 | GattService::UUID_HEALTH_THERMOMETER_SERVICE, |
DuyLionTran | 9:be09a9bf2e2e | 49 | HealthCareService::USER_DATA_SERVICE_UUID, |
DuyLionTran | 9:be09a9bf2e2e | 50 | HealthCareService::DEVICE_INFO_SERVICE_UUID |
DuyLionTran | 9:be09a9bf2e2e | 51 | }; |
DuyLionTran | 17:b7c2db3e7282 | 52 | |
DuyLionTran | 17:b7c2db3e7282 | 53 | uint8_t htsPosition = HealthCareService::HealthCareService::TEMPERATURE_LOCATION_FINGER; |
DuyLionTran | 17:b7c2db3e7282 | 54 | uint8_t hrmPosition = HealthCareService::HealthCareService::HRM_LOCATION_FINGER; |
DuyLionTran | 0:64ca984b3efd | 55 | |
DuyLionTran | 0:64ca984b3efd | 56 | /* STRUCTS/CLASSESS */ |
DuyLionTran | 17:b7c2db3e7282 | 57 | HealthCareService *HealthCareServicePtr; |
DuyLionTran | 17:b7c2db3e7282 | 58 | |
DuyLionTran | 17:b7c2db3e7282 | 59 | //PulseSensor PulseSensor(); |
DuyLionTran | 0:64ca984b3efd | 60 | static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20); |
DuyLionTran | 4:e44cd8682f1c | 61 | Serial serial(USBTX, USBRX); |
DuyLionTran | 0:64ca984b3efd | 62 | /* ================== FUNCTION PROTOTYPES ==================== */ |
DuyLionTran | 18:a9d67c51715e | 63 | void connectionCallback(const Gap::ConnectionCallbackParams_t *); |
DuyLionTran | 4:e44cd8682f1c | 64 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); |
DuyLionTran | 4:e44cd8682f1c | 65 | void onBleInitError(BLE &ble, ble_error_t error); |
DuyLionTran | 4:e44cd8682f1c | 66 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); |
DuyLionTran | 8:3384286a8498 | 67 | void onDataWrittenCallback(const GattWriteCallbackParams *params); |
DuyLionTran | 4:e44cd8682f1c | 68 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context); |
DuyLionTran | 4:e44cd8682f1c | 69 | |
DuyLionTran | 9:be09a9bf2e2e | 70 | void updatePayload(void); |
DuyLionTran | 4:e44cd8682f1c | 71 | void main_event(void); |
DuyLionTran | 4:e44cd8682f1c | 72 | void periodicCallback(void); |
DuyLionTran | 0:64ca984b3efd | 73 | |
DuyLionTran | 0:64ca984b3efd | 74 | /* ==================== FUNCTION DETAILS ===================== */ |
DuyLionTran | 0:64ca984b3efd | 75 | /* Restart Advertising on disconnection*/ |
DuyLionTran | 0:64ca984b3efd | 76 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { |
DuyLionTran | 0:64ca984b3efd | 77 | BLE::Instance().gap().startAdvertising(); |
DuyLionTran | 8:3384286a8498 | 78 | printf("Device disconnected with mobile/table\r\n"); |
DuyLionTran | 0:64ca984b3efd | 79 | } |
DuyLionTran | 0:64ca984b3efd | 80 | |
DuyLionTran | 18:a9d67c51715e | 81 | void connectionCallback(const Gap::ConnectionCallbackParams_t *) { |
DuyLionTran | 18:a9d67c51715e | 82 | printf("Deviced connected"); |
DuyLionTran | 18:a9d67c51715e | 83 | startSendFloat = 0; |
DuyLionTran | 18:a9d67c51715e | 84 | } |
DuyLionTran | 18:a9d67c51715e | 85 | |
DuyLionTran | 9:be09a9bf2e2e | 86 | void updatePayload(void) { |
DuyLionTran | 9:be09a9bf2e2e | 87 | // Update the count in the SERVICE_DATA field of the advertising payload |
DuyLionTran | 9:be09a9bf2e2e | 88 | cnt++; |
DuyLionTran | 10:4e0f5173269e | 89 | uint8_t service_data[8]; |
DuyLionTran | 10:4e0f5173269e | 90 | /* first 2 bytes are for service UUID */ |
DuyLionTran | 9:be09a9bf2e2e | 91 | service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF; |
DuyLionTran | 9:be09a9bf2e2e | 92 | service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8; |
DuyLionTran | 10:4e0f5173269e | 93 | /* next 4 bytes are for client ID */ |
DuyLionTran | 10:4e0f5173269e | 94 | service_data[2] = 0x07; |
DuyLionTran | 10:4e0f5173269e | 95 | service_data[3] = 0x09; |
DuyLionTran | 10:4e0f5173269e | 96 | service_data[4] = 0x9A; |
DuyLionTran | 10:4e0f5173269e | 97 | service_data[5] = 0xAC; |
DuyLionTran | 10:4e0f5173269e | 98 | /* last 2 bytes are sensor data */ |
DuyLionTran | 10:4e0f5173269e | 99 | service_data[6] = cnt; |
DuyLionTran | 10:4e0f5173269e | 100 | service_data[7] = cnt; |
DuyLionTran | 9:be09a9bf2e2e | 101 | ble_error_t err = BLE::Instance().gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); |
DuyLionTran | 9:be09a9bf2e2e | 102 | |
DuyLionTran | 9:be09a9bf2e2e | 103 | } |
DuyLionTran | 9:be09a9bf2e2e | 104 | |
DuyLionTran | 0:64ca984b3efd | 105 | void main_event(void) { |
DuyLionTran | 0:64ca984b3efd | 106 | /* Do blocking calls or whatever is necessary for sensor polling. |
DuyLionTran | 0:64ca984b3efd | 107 | In our case, we simply update the Temperature measurement. */ |
DuyLionTran | 17:b7c2db3e7282 | 108 | /* TODO Read temperature */ |
DuyLionTran | 17:b7c2db3e7282 | 109 | currentTemperature = (currentTemperature + 0.16 > 42.79) ? 35.69 : currentTemperature + 1.18; |
DuyLionTran | 17:b7c2db3e7282 | 110 | |
DuyLionTran | 17:b7c2db3e7282 | 111 | /* TODO Read Heart Rate */ |
DuyLionTran | 17:b7c2db3e7282 | 112 | currentHRMCounter = (currentHRMCounter + 1 > 120) ? 75 : (currentHRMCounter + HRM_increasement); |
DuyLionTran | 17:b7c2db3e7282 | 113 | |
DuyLionTran | 17:b7c2db3e7282 | 114 | /* TODO Update Service data */ |
DuyLionTran | 9:be09a9bf2e2e | 115 | updatePayload(); |
DuyLionTran | 11:5a4313edf10d | 116 | |
DuyLionTran | 11:5a4313edf10d | 117 | /* sendCombinedTempAndHR = (currentTemperature * 100)* 1000 + currentHRMCounter */ |
DuyLionTran | 12:dc974f6fed97 | 118 | sendCombinedTempAndHR = currentTemperature * 1000; /* Temperature float to int conversion */ |
DuyLionTran | 12:dc974f6fed97 | 119 | sendCombinedTempAndHR = sendCombinedTempAndHR + (float)(currentHRMCounter/100.0); |
DuyLionTran | 17:b7c2db3e7282 | 120 | |
DuyLionTran | 17:b7c2db3e7282 | 121 | switch (startSendFloat) { |
DuyLionTran | 17:b7c2db3e7282 | 122 | case 0: /* sendCombinedHRAndTemp = (currentHRMCounter * 100) + currentTemperature */ |
DuyLionTran | 17:b7c2db3e7282 | 123 | /* Because the maximum size of HRM Value is 2 bytes */ |
DuyLionTran | 17:b7c2db3e7282 | 124 | sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 17:b7c2db3e7282 | 125 | uint16_t tempTemp = currentTemperature * 100; |
DuyLionTran | 17:b7c2db3e7282 | 126 | saveFloat = tempTemp % 100; |
DuyLionTran | 17:b7c2db3e7282 | 127 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)currentTemperature; |
DuyLionTran | 17:b7c2db3e7282 | 128 | break; |
DuyLionTran | 17:b7c2db3e7282 | 129 | |
DuyLionTran | 17:b7c2db3e7282 | 130 | case 1: sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 17:b7c2db3e7282 | 131 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)13; |
DuyLionTran | 17:b7c2db3e7282 | 132 | break; |
DuyLionTran | 17:b7c2db3e7282 | 133 | |
DuyLionTran | 17:b7c2db3e7282 | 134 | case 2: sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 17:b7c2db3e7282 | 135 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)10; |
DuyLionTran | 17:b7c2db3e7282 | 136 | break; |
DuyLionTran | 17:b7c2db3e7282 | 137 | |
DuyLionTran | 17:b7c2db3e7282 | 138 | case 3: sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 17:b7c2db3e7282 | 139 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)saveFloat; |
DuyLionTran | 17:b7c2db3e7282 | 140 | break; |
DuyLionTran | 17:b7c2db3e7282 | 141 | |
DuyLionTran | 17:b7c2db3e7282 | 142 | case 4: sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 17:b7c2db3e7282 | 143 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)20; |
DuyLionTran | 17:b7c2db3e7282 | 144 | startSendFloat = 0; |
DuyLionTran | 17:b7c2db3e7282 | 145 | break; |
DuyLionTran | 17:b7c2db3e7282 | 146 | |
DuyLionTran | 17:b7c2db3e7282 | 147 | default: break; |
DuyLionTran | 17:b7c2db3e7282 | 148 | } |
DuyLionTran | 11:5a4313edf10d | 149 | // printf("sendCombinedTempAndHR %d\r\n", sendCombinedTempAndHR); |
DuyLionTran | 16:0325e647496f | 150 | // printf("sendCombinedTempAndHR %.2f\r\n\r\n", (float)sendCombinedTempAndHR); |
DuyLionTran | 17:b7c2db3e7282 | 151 | // printf("currentHRMCounter %d\r\n", currentHRMCounter); |
DuyLionTran | 17:b7c2db3e7282 | 152 | // printf("currentTemperature %d\r\n", (uint8_t)currentTemperature); |
DuyLionTran | 17:b7c2db3e7282 | 153 | // printf("sendCombinedHRAndTemp %d\r\n", sendCombinedHRAndTemp); |
DuyLionTran | 17:b7c2db3e7282 | 154 | |
DuyLionTran | 4:e44cd8682f1c | 155 | if (BLE::Instance().gap().getState().connected) { |
DuyLionTran | 12:dc974f6fed97 | 156 | HealthCareServicePtr->updateTemperature(sendCombinedTempAndHR); |
DuyLionTran | 17:b7c2db3e7282 | 157 | HealthCareServicePtr->updateHeartRate(sendCombinedHRAndTemp); |
DuyLionTran | 4:e44cd8682f1c | 158 | } |
DuyLionTran | 17:b7c2db3e7282 | 159 | startSendFloat++; |
DuyLionTran | 0:64ca984b3efd | 160 | } |
DuyLionTran | 0:64ca984b3efd | 161 | |
DuyLionTran | 0:64ca984b3efd | 162 | void periodicCallback(void) { |
DuyLionTran | 4:e44cd8682f1c | 163 | /* call main_event immediately */ |
DuyLionTran | 4:e44cd8682f1c | 164 | eventQueue.call(main_event); |
DuyLionTran | 0:64ca984b3efd | 165 | } |
DuyLionTran | 0:64ca984b3efd | 166 | |
DuyLionTran | 0:64ca984b3efd | 167 | void printMacAddress() { |
DuyLionTran | 0:64ca984b3efd | 168 | /* Print out device MAC address to the console*/ |
DuyLionTran | 0:64ca984b3efd | 169 | Gap::AddressType_t addr_type; |
DuyLionTran | 0:64ca984b3efd | 170 | Gap::Address_t address; |
DuyLionTran | 0:64ca984b3efd | 171 | BLE::Instance().gap().getAddress(&addr_type, address); |
DuyLionTran | 0:64ca984b3efd | 172 | printf("DEVICE MAC ADDRESS: "); |
DuyLionTran | 0:64ca984b3efd | 173 | for (int i = 5; i >= 1; i--) { |
DuyLionTran | 0:64ca984b3efd | 174 | printf("%02x:", address[i]); |
DuyLionTran | 0:64ca984b3efd | 175 | } |
DuyLionTran | 0:64ca984b3efd | 176 | printf("%02x\r\n", address[0]); |
DuyLionTran | 0:64ca984b3efd | 177 | } |
DuyLionTran | 0:64ca984b3efd | 178 | |
DuyLionTran | 0:64ca984b3efd | 179 | void onBleInitError(BLE &ble, ble_error_t error) { |
DuyLionTran | 0:64ca984b3efd | 180 | /* Initialization error handling should go here */ |
DuyLionTran | 8:3384286a8498 | 181 | printf("BLE init error!\r\n"); |
DuyLionTran | 8:3384286a8498 | 182 | } |
DuyLionTran | 8:3384286a8498 | 183 | |
DuyLionTran | 8:3384286a8498 | 184 | /** |
DuyLionTran | 8:3384286a8498 | 185 | * This callback allows the HealthCareService to receive updates to the controlState Characteristic. |
DuyLionTran | 8:3384286a8498 | 186 | * |
DuyLionTran | 8:3384286a8498 | 187 | * @param[in] params |
DuyLionTran | 8:3384286a8498 | 188 | * Information about the characterisitc being updated. |
DuyLionTran | 8:3384286a8498 | 189 | */ |
DuyLionTran | 8:3384286a8498 | 190 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
DuyLionTran | 16:0325e647496f | 191 | // printf("Write callback, value %d\r\n", *(params->data)); |
DuyLionTran | 16:0325e647496f | 192 | if ((params->handle == HealthCareServicePtr->getTypeHandle()) && (params->len >= 1)) { |
DuyLionTran | 14:e6029b780879 | 193 | uint8_t oldPosition = htsPosition; |
DuyLionTran | 14:e6029b780879 | 194 | htsPosition = *(params->data); |
DuyLionTran | 16:0325e647496f | 195 | printf("Old type: %d, New type %d\r\n", oldPosition, htsPosition); |
DuyLionTran | 16:0325e647496f | 196 | HealthCareServicePtr->updateType(htsPosition); |
DuyLionTran | 8:3384286a8498 | 197 | } |
DuyLionTran | 16:0325e647496f | 198 | if ((params->handle == HealthCareServicePtr->getLocationHandle()) && (params->len >= 1)) { |
DuyLionTran | 16:0325e647496f | 199 | uint8_t oldPosition = hrmPosition; |
DuyLionTran | 16:0325e647496f | 200 | hrmPosition = *(params->data); |
DuyLionTran | 16:0325e647496f | 201 | printf("Old location: %d, New location %d\r\n", oldPosition, hrmPosition); |
DuyLionTran | 16:0325e647496f | 202 | HealthCareServicePtr->updateLocation(hrmPosition); |
DuyLionTran | 16:0325e647496f | 203 | } |
DuyLionTran | 0:64ca984b3efd | 204 | } |
DuyLionTran | 0:64ca984b3efd | 205 | |
DuyLionTran | 0:64ca984b3efd | 206 | /** |
DuyLionTran | 0:64ca984b3efd | 207 | * @brief Callback triggered when the ble initialization process has finished |
DuyLionTran | 0:64ca984b3efd | 208 | */ |
DuyLionTran | 0:64ca984b3efd | 209 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { |
DuyLionTran | 0:64ca984b3efd | 210 | BLE& ble = params->ble; |
DuyLionTran | 0:64ca984b3efd | 211 | ble_error_t error = params->error; |
DuyLionTran | 0:64ca984b3efd | 212 | |
DuyLionTran | 0:64ca984b3efd | 213 | if (error != BLE_ERROR_NONE) { |
DuyLionTran | 0:64ca984b3efd | 214 | onBleInitError(ble, error); |
DuyLionTran | 0:64ca984b3efd | 215 | return; |
DuyLionTran | 0:64ca984b3efd | 216 | } |
DuyLionTran | 0:64ca984b3efd | 217 | |
DuyLionTran | 0:64ca984b3efd | 218 | /* Ensure that it is the default instance of BLE */ |
DuyLionTran | 0:64ca984b3efd | 219 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
DuyLionTran | 0:64ca984b3efd | 220 | return; |
DuyLionTran | 0:64ca984b3efd | 221 | } |
DuyLionTran | 8:3384286a8498 | 222 | uint8_t initial_HRMIncreasement = 1; |
DuyLionTran | 18:a9d67c51715e | 223 | ble.gap().onConnection(connectionCallback); |
DuyLionTran | 0:64ca984b3efd | 224 | ble.gap().onDisconnection(disconnectionCallback); |
DuyLionTran | 8:3384286a8498 | 225 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
DuyLionTran | 8:3384286a8498 | 226 | |
DuyLionTran | 14:e6029b780879 | 227 | HealthCareServicePtr = new HealthCareService(ble, currentTemperature, htsPosition, |
DuyLionTran | 15:00a1c0ea570c | 228 | currentHRMCounter, hrmPosition, |
DuyLionTran | 8:3384286a8498 | 229 | initial_HRMIncreasement); |
DuyLionTran | 0:64ca984b3efd | 230 | |
DuyLionTran | 0:64ca984b3efd | 231 | /* setup advertising */ |
DuyLionTran | 0:64ca984b3efd | 232 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
DuyLionTran | 0:64ca984b3efd | 233 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
DuyLionTran | 2:16f6cfcd7505 | 234 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER ); |
DuyLionTran | 0:64ca984b3efd | 235 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
DuyLionTran | 9:be09a9bf2e2e | 236 | |
DuyLionTran | 9:be09a9bf2e2e | 237 | /* No need to display the device name */ |
DuyLionTran | 9:be09a9bf2e2e | 238 | // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
DuyLionTran | 10:4e0f5173269e | 239 | uint8_t service_data[8]; |
DuyLionTran | 9:be09a9bf2e2e | 240 | /* first 2 bytes are for service UUID */ |
DuyLionTran | 9:be09a9bf2e2e | 241 | service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF; |
DuyLionTran | 9:be09a9bf2e2e | 242 | service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8; |
DuyLionTran | 10:4e0f5173269e | 243 | /* next 4 bytes are for client ID */ |
DuyLionTran | 10:4e0f5173269e | 244 | service_data[2] = 0x07; |
DuyLionTran | 10:4e0f5173269e | 245 | service_data[3] = 0x09; |
DuyLionTran | 11:5a4313edf10d | 246 | service_data[4] = 0x89; |
DuyLionTran | 11:5a4313edf10d | 247 | service_data[5] = 0xAB; |
DuyLionTran | 9:be09a9bf2e2e | 248 | /* last 2 bytes are sensor data */ |
DuyLionTran | 10:4e0f5173269e | 249 | service_data[6] = cnt; |
DuyLionTran | 10:4e0f5173269e | 250 | service_data[7] = cnt; |
DuyLionTran | 9:be09a9bf2e2e | 251 | |
DuyLionTran | 9:be09a9bf2e2e | 252 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA , (uint8_t *)service_data, sizeof(service_data)); |
DuyLionTran | 9:be09a9bf2e2e | 253 | |
DuyLionTran | 0:64ca984b3efd | 254 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
DuyLionTran | 13:e6ddc458904e | 255 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
DuyLionTran | 0:64ca984b3efd | 256 | ble.gap().startAdvertising(); |
DuyLionTran | 0:64ca984b3efd | 257 | |
DuyLionTran | 0:64ca984b3efd | 258 | printMacAddress(); |
DuyLionTran | 8:3384286a8498 | 259 | printf("BLE init successfully\r\n"); |
DuyLionTran | 0:64ca984b3efd | 260 | } |
DuyLionTran | 0:64ca984b3efd | 261 | |
DuyLionTran | 0:64ca984b3efd | 262 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
DuyLionTran | 0:64ca984b3efd | 263 | BLE &ble = BLE::Instance(); |
DuyLionTran | 0:64ca984b3efd | 264 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
DuyLionTran | 0:64ca984b3efd | 265 | } |
DuyLionTran | 0:64ca984b3efd | 266 | |
DuyLionTran | 0:64ca984b3efd | 267 | /* MAIN FUNCTION */ |
DuyLionTran | 0:64ca984b3efd | 268 | int main() { |
DuyLionTran | 4:e44cd8682f1c | 269 | |
DuyLionTran | 4:e44cd8682f1c | 270 | serial.baud(115200); |
DuyLionTran | 4:e44cd8682f1c | 271 | printf("\r\n BODY WIRELESS SENSOR NETWORK\r\n"); |
DuyLionTran | 4:e44cd8682f1c | 272 | /* call periodicCallback every 500ms */ |
DuyLionTran | 13:e6ddc458904e | 273 | eventQueue.call_every(1000, periodicCallback); |
DuyLionTran | 4:e44cd8682f1c | 274 | |
DuyLionTran | 4:e44cd8682f1c | 275 | /* init BLE */ |
DuyLionTran | 0:64ca984b3efd | 276 | BLE &ble = BLE::Instance(); |
DuyLionTran | 0:64ca984b3efd | 277 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
DuyLionTran | 0:64ca984b3efd | 278 | ble.init(bleInitComplete); |
DuyLionTran | 4:e44cd8682f1c | 279 | |
DuyLionTran | 4:e44cd8682f1c | 280 | /* dispatch the event queue */ |
DuyLionTran | 0:64ca984b3efd | 281 | eventQueue.dispatch_forever(); |
DuyLionTran | 0:64ca984b3efd | 282 | |
DuyLionTran | 0:64ca984b3efd | 283 | return 0; |
DuyLionTran | 0:64ca984b3efd | 284 | } |