Graduation Thesis, use Nucleo and X-Nucleo BLE

Dependencies:   PulseSensor GSM Thermometer KalmanFilter

Committer:
DuyLionTran
Date:
Thu May 03 06:45:37 2018 +0000
Revision:
12:dc974f6fed97
Parent:
11:5a4313edf10d
Child:
13:e6ddc458904e
small bugs

Who changed what in which revision?

UserRevisionLine numberNew 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 0:64ca984b3efd 32 static float currentTemperature = 39.6;
DuyLionTran 0:64ca984b3efd 33 static uint8_t currentHRMCounter = 80;
DuyLionTran 11:5a4313edf10d 34 static float sendCombinedTempAndHR;
DuyLionTran 0:64ca984b3efd 35
DuyLionTran 0:64ca984b3efd 36 /* PRIVATE VARIABLES */
DuyLionTran 8:3384286a8498 37 uint8_t HRM_increasement = 1;
DuyLionTran 9:be09a9bf2e2e 38 uint8_t cnt;
DuyLionTran 8:3384286a8498 39
DuyLionTran 9:be09a9bf2e2e 40 /* No need to display the device name */
DuyLionTran 9:be09a9bf2e2e 41 //const static char DEVICE_NAME[] = "BODY SENSOR";
DuyLionTran 9:be09a9bf2e2e 42
DuyLionTran 8:3384286a8498 43 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
DuyLionTran 8:3384286a8498 44 GattService::UUID_HEALTH_THERMOMETER_SERVICE,
DuyLionTran 9:be09a9bf2e2e 45 HealthCareService::USER_DATA_SERVICE_UUID,
DuyLionTran 9:be09a9bf2e2e 46 HealthCareService::DEVICE_INFO_SERVICE_UUID
DuyLionTran 9:be09a9bf2e2e 47 };
DuyLionTran 0:64ca984b3efd 48
DuyLionTran 0:64ca984b3efd 49 /* STRUCTS/CLASSESS */
DuyLionTran 0:64ca984b3efd 50 static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20);
DuyLionTran 0:64ca984b3efd 51 HealthCareService *HealthCareServicePtr;
DuyLionTran 3:9b552b775c6e 52 //PulseSensor PulseSensor();
DuyLionTran 0:64ca984b3efd 53
DuyLionTran 4:e44cd8682f1c 54 Serial serial(USBTX, USBRX);
DuyLionTran 4:e44cd8682f1c 55
DuyLionTran 0:64ca984b3efd 56 /* ================== FUNCTION PROTOTYPES ==================== */
DuyLionTran 4:e44cd8682f1c 57 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *);
DuyLionTran 4:e44cd8682f1c 58 void onBleInitError(BLE &ble, ble_error_t error);
DuyLionTran 4:e44cd8682f1c 59 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params);
DuyLionTran 8:3384286a8498 60 void onDataWrittenCallback(const GattWriteCallbackParams *params);
DuyLionTran 4:e44cd8682f1c 61 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context);
DuyLionTran 4:e44cd8682f1c 62
DuyLionTran 9:be09a9bf2e2e 63 void updatePayload(void);
DuyLionTran 4:e44cd8682f1c 64 void main_event(void);
DuyLionTran 4:e44cd8682f1c 65 void periodicCallback(void);
DuyLionTran 0:64ca984b3efd 66
DuyLionTran 0:64ca984b3efd 67 /* ==================== FUNCTION DETAILS ===================== */
DuyLionTran 0:64ca984b3efd 68 /* Restart Advertising on disconnection*/
DuyLionTran 0:64ca984b3efd 69 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) {
DuyLionTran 0:64ca984b3efd 70 BLE::Instance().gap().startAdvertising();
DuyLionTran 8:3384286a8498 71 printf("Device disconnected with mobile/table\r\n");
DuyLionTran 0:64ca984b3efd 72 }
DuyLionTran 0:64ca984b3efd 73
DuyLionTran 9:be09a9bf2e2e 74 void updatePayload(void) {
DuyLionTran 9:be09a9bf2e2e 75 // Update the count in the SERVICE_DATA field of the advertising payload
DuyLionTran 9:be09a9bf2e2e 76 cnt++;
DuyLionTran 10:4e0f5173269e 77 uint8_t service_data[8];
DuyLionTran 10:4e0f5173269e 78 /* first 2 bytes are for service UUID */
DuyLionTran 9:be09a9bf2e2e 79 service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF;
DuyLionTran 9:be09a9bf2e2e 80 service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8;
DuyLionTran 10:4e0f5173269e 81 /* next 4 bytes are for client ID */
DuyLionTran 10:4e0f5173269e 82 service_data[2] = 0x07;
DuyLionTran 10:4e0f5173269e 83 service_data[3] = 0x09;
DuyLionTran 10:4e0f5173269e 84 service_data[4] = 0x9A;
DuyLionTran 10:4e0f5173269e 85 service_data[5] = 0xAC;
DuyLionTran 10:4e0f5173269e 86 /* last 2 bytes are sensor data */
DuyLionTran 10:4e0f5173269e 87 service_data[6] = cnt;
DuyLionTran 10:4e0f5173269e 88 service_data[7] = cnt;
DuyLionTran 9:be09a9bf2e2e 89 ble_error_t err = BLE::Instance().gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data));
DuyLionTran 9:be09a9bf2e2e 90
DuyLionTran 9:be09a9bf2e2e 91 }
DuyLionTran 9:be09a9bf2e2e 92
DuyLionTran 0:64ca984b3efd 93 void main_event(void) {
DuyLionTran 0:64ca984b3efd 94 /* Do blocking calls or whatever is necessary for sensor polling.
DuyLionTran 0:64ca984b3efd 95 In our case, we simply update the Temperature measurement. */
DuyLionTran 12:dc974f6fed97 96 currentTemperature = (currentTemperature + 0.16 > 48.92) ? 35.69 : currentTemperature + 1.18;
DuyLionTran 8:3384286a8498 97 currentHRMCounter = (currentHRMCounter + 1 > 120) ? 80 : (currentHRMCounter + HRM_increasement);
DuyLionTran 9:be09a9bf2e2e 98 updatePayload();
DuyLionTran 11:5a4313edf10d 99
DuyLionTran 11:5a4313edf10d 100 /* sendCombinedTempAndHR = (currentTemperature * 100)* 1000 + currentHRMCounter */
DuyLionTran 12:dc974f6fed97 101 sendCombinedTempAndHR = currentTemperature * 1000; /* Temperature float to int conversion */
DuyLionTran 12:dc974f6fed97 102 sendCombinedTempAndHR = sendCombinedTempAndHR + (float)(currentHRMCounter/100.0);
DuyLionTran 11:5a4313edf10d 103 // printf("sendCombinedTempAndHR %d\r\n", sendCombinedTempAndHR);
DuyLionTran 12:dc974f6fed97 104 printf("sendCombinedTempAndHR %.2f\r\n\r\n", (float)sendCombinedTempAndHR);
DuyLionTran 4:e44cd8682f1c 105 if (BLE::Instance().gap().getState().connected) {
DuyLionTran 12:dc974f6fed97 106 HealthCareServicePtr->updateTemperature(sendCombinedTempAndHR);
DuyLionTran 4:e44cd8682f1c 107 HealthCareServicePtr->updateHeartRate(currentHRMCounter);
DuyLionTran 4:e44cd8682f1c 108 }
DuyLionTran 4:e44cd8682f1c 109
DuyLionTran 0:64ca984b3efd 110 }
DuyLionTran 0:64ca984b3efd 111
DuyLionTran 0:64ca984b3efd 112 void periodicCallback(void) {
DuyLionTran 4:e44cd8682f1c 113 /* call main_event immediately */
DuyLionTran 4:e44cd8682f1c 114 eventQueue.call(main_event);
DuyLionTran 0:64ca984b3efd 115 }
DuyLionTran 0:64ca984b3efd 116
DuyLionTran 0:64ca984b3efd 117 void printMacAddress() {
DuyLionTran 0:64ca984b3efd 118 /* Print out device MAC address to the console*/
DuyLionTran 0:64ca984b3efd 119 Gap::AddressType_t addr_type;
DuyLionTran 0:64ca984b3efd 120 Gap::Address_t address;
DuyLionTran 0:64ca984b3efd 121 BLE::Instance().gap().getAddress(&addr_type, address);
DuyLionTran 0:64ca984b3efd 122 printf("DEVICE MAC ADDRESS: ");
DuyLionTran 0:64ca984b3efd 123 for (int i = 5; i >= 1; i--) {
DuyLionTran 0:64ca984b3efd 124 printf("%02x:", address[i]);
DuyLionTran 0:64ca984b3efd 125 }
DuyLionTran 0:64ca984b3efd 126 printf("%02x\r\n", address[0]);
DuyLionTran 0:64ca984b3efd 127 }
DuyLionTran 0:64ca984b3efd 128
DuyLionTran 0:64ca984b3efd 129 void onBleInitError(BLE &ble, ble_error_t error) {
DuyLionTran 0:64ca984b3efd 130 /* Initialization error handling should go here */
DuyLionTran 8:3384286a8498 131 printf("BLE init error!\r\n");
DuyLionTran 8:3384286a8498 132 }
DuyLionTran 8:3384286a8498 133
DuyLionTran 8:3384286a8498 134 /**
DuyLionTran 8:3384286a8498 135 * This callback allows the HealthCareService to receive updates to the controlState Characteristic.
DuyLionTran 8:3384286a8498 136 *
DuyLionTran 8:3384286a8498 137 * @param[in] params
DuyLionTran 8:3384286a8498 138 * Information about the characterisitc being updated.
DuyLionTran 8:3384286a8498 139 */
DuyLionTran 8:3384286a8498 140 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
DuyLionTran 8:3384286a8498 141 if ((params->handle == HealthCareServicePtr->getValueHandle()) && (params->len == 1)) {
DuyLionTran 8:3384286a8498 142 uint8_t old_HRM_increasement = HRM_increasement;
DuyLionTran 8:3384286a8498 143 HRM_increasement = *(params->data);
DuyLionTran 8:3384286a8498 144 printf("Old data: %d\r\n, New data %d\r\n", old_HRM_increasement, HRM_increasement);
DuyLionTran 8:3384286a8498 145 }
DuyLionTran 0:64ca984b3efd 146 }
DuyLionTran 0:64ca984b3efd 147
DuyLionTran 0:64ca984b3efd 148 /**
DuyLionTran 0:64ca984b3efd 149 * @brief Callback triggered when the ble initialization process has finished
DuyLionTran 0:64ca984b3efd 150 */
DuyLionTran 0:64ca984b3efd 151 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) {
DuyLionTran 0:64ca984b3efd 152 BLE& ble = params->ble;
DuyLionTran 0:64ca984b3efd 153 ble_error_t error = params->error;
DuyLionTran 0:64ca984b3efd 154
DuyLionTran 0:64ca984b3efd 155 if (error != BLE_ERROR_NONE) {
DuyLionTran 0:64ca984b3efd 156 onBleInitError(ble, error);
DuyLionTran 0:64ca984b3efd 157 return;
DuyLionTran 0:64ca984b3efd 158 }
DuyLionTran 0:64ca984b3efd 159
DuyLionTran 0:64ca984b3efd 160 /* Ensure that it is the default instance of BLE */
DuyLionTran 0:64ca984b3efd 161 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
DuyLionTran 0:64ca984b3efd 162 return;
DuyLionTran 0:64ca984b3efd 163 }
DuyLionTran 8:3384286a8498 164 uint8_t initial_HRMIncreasement = 1;
DuyLionTran 0:64ca984b3efd 165 ble.gap().onDisconnection(disconnectionCallback);
DuyLionTran 8:3384286a8498 166 ble.gattServer().onDataWritten(onDataWrittenCallback);
DuyLionTran 8:3384286a8498 167
DuyLionTran 11:5a4313edf10d 168 HealthCareServicePtr = new HealthCareService(ble, currentTemperature, HealthCareService::TEMPERATURE_LOCATION_FINGER,
DuyLionTran 11:5a4313edf10d 169 currentHRMCounter, HealthCareService::HRM_LOCATION_FINGER,
DuyLionTran 8:3384286a8498 170 initial_HRMIncreasement);
DuyLionTran 0:64ca984b3efd 171
DuyLionTran 0:64ca984b3efd 172 /* setup advertising */
DuyLionTran 0:64ca984b3efd 173 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
DuyLionTran 0:64ca984b3efd 174 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
DuyLionTran 2:16f6cfcd7505 175 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER );
DuyLionTran 0:64ca984b3efd 176 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
DuyLionTran 9:be09a9bf2e2e 177
DuyLionTran 9:be09a9bf2e2e 178 /* No need to display the device name */
DuyLionTran 9:be09a9bf2e2e 179 // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
DuyLionTran 10:4e0f5173269e 180 uint8_t service_data[8];
DuyLionTran 9:be09a9bf2e2e 181 /* first 2 bytes are for service UUID */
DuyLionTran 9:be09a9bf2e2e 182 service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF;
DuyLionTran 9:be09a9bf2e2e 183 service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8;
DuyLionTran 10:4e0f5173269e 184 /* next 4 bytes are for client ID */
DuyLionTran 10:4e0f5173269e 185 service_data[2] = 0x07;
DuyLionTran 10:4e0f5173269e 186 service_data[3] = 0x09;
DuyLionTran 11:5a4313edf10d 187 service_data[4] = 0x89;
DuyLionTran 11:5a4313edf10d 188 service_data[5] = 0xAB;
DuyLionTran 9:be09a9bf2e2e 189 /* last 2 bytes are sensor data */
DuyLionTran 10:4e0f5173269e 190 service_data[6] = cnt;
DuyLionTran 10:4e0f5173269e 191 service_data[7] = cnt;
DuyLionTran 9:be09a9bf2e2e 192
DuyLionTran 9:be09a9bf2e2e 193 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA , (uint8_t *)service_data, sizeof(service_data));
DuyLionTran 9:be09a9bf2e2e 194
DuyLionTran 0:64ca984b3efd 195 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
DuyLionTran 9:be09a9bf2e2e 196 ble.gap().setAdvertisingInterval(300); /* 1000ms. */
DuyLionTran 0:64ca984b3efd 197 ble.gap().startAdvertising();
DuyLionTran 0:64ca984b3efd 198
DuyLionTran 0:64ca984b3efd 199 printMacAddress();
DuyLionTran 8:3384286a8498 200 printf("BLE init successfully\r\n");
DuyLionTran 0:64ca984b3efd 201 }
DuyLionTran 0:64ca984b3efd 202
DuyLionTran 0:64ca984b3efd 203 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
DuyLionTran 0:64ca984b3efd 204 BLE &ble = BLE::Instance();
DuyLionTran 0:64ca984b3efd 205 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
DuyLionTran 0:64ca984b3efd 206 }
DuyLionTran 0:64ca984b3efd 207
DuyLionTran 0:64ca984b3efd 208 /* MAIN FUNCTION */
DuyLionTran 0:64ca984b3efd 209 int main() {
DuyLionTran 4:e44cd8682f1c 210
DuyLionTran 4:e44cd8682f1c 211 serial.baud(115200);
DuyLionTran 4:e44cd8682f1c 212 printf("\r\n BODY WIRELESS SENSOR NETWORK\r\n");
DuyLionTran 4:e44cd8682f1c 213 /* call periodicCallback every 500ms */
DuyLionTran 0:64ca984b3efd 214 eventQueue.call_every(500, periodicCallback);
DuyLionTran 4:e44cd8682f1c 215
DuyLionTran 4:e44cd8682f1c 216 /* init BLE */
DuyLionTran 0:64ca984b3efd 217 BLE &ble = BLE::Instance();
DuyLionTran 0:64ca984b3efd 218 ble.onEventsToProcess(scheduleBleEventsProcessing);
DuyLionTran 0:64ca984b3efd 219 ble.init(bleInitComplete);
DuyLionTran 4:e44cd8682f1c 220
DuyLionTran 4:e44cd8682f1c 221 /* dispatch the event queue */
DuyLionTran 0:64ca984b3efd 222 eventQueue.dispatch_forever();
DuyLionTran 0:64ca984b3efd 223
DuyLionTran 0:64ca984b3efd 224 return 0;
DuyLionTran 0:64ca984b3efd 225 }