Graduation Thesis, use Nucleo and X-Nucleo BLE

Dependencies:   PulseSensor GSM Thermometer KalmanFilter

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