Graduation Thesis, use Nucleo and X-Nucleo BLE
Dependencies: PulseSensor GSM Thermometer KalmanFilter
Application/main.cpp
- Committer:
- DuyLionTran
- Date:
- 2018-05-29
- Revision:
- 20:d88c0f1fd8cc
- Parent:
- 19:a4f3328e9d09
- Child:
- 21:fa70fabe6c49
File content as of revision 20:d88c0f1fd8cc:
/** * This is the project for "BLE HealthCare". The device is attached on any patient's body at will. * Revision: * version 0.8 02-12-2018 * version 0.8.5 02-14-2018 * version 0.9 02-15-2018 Pulse sensor and thermometer added * version 0.9.5 02-16-2018 Calculation for pulse sensor and thermometer. GSM library added * version 0.9.6 02-21-2018 Update mbed-os * version 0.9.6 02-21-2018 Some modification for LM35 * version 0.9.8 03-04-2018 Data receiving from client device added * version 1.0 03-09-2018 Some minor bugs fixed * version 1.0.5 03-09-2018 Some minor bugs fixed /* ======================== INCLUDES ========================= */ #include <events/mbed_events.h> #include <mbed.h> #include "ble/BLE.h" #include "ble_healthcare_service.h" #include "GSM.h" #include "LM35.h" #include "PulseSensor.h" /* ======================== DEFINES ========================== */ #define PULSE_SENSOR_PIN A0 #define THERM_SENSOR_PIN A1 #define START_SEND_INT_TEMP 13 #define START_SEND_FLOAT_TEMP 10 #define STOP_SEND_TEMP 20 /* ======================= VARIABLES ========================= */ /* GLOBAL VARIABLES */ static float currentTemperature = 39.6; static uint16_t currentHRMCounter; static float sendCombinedTempAndHR; static uint16_t sendCombinedHRAndTemp; /* PRIVATE VARIABLES */ uint8_t cnt; uint8_t startSendFloat = 0; bool isConnectedToDevice = false; /* No need to display the device name */ //const static char DEVICE_NAME[] = "BODY SENSOR"; static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, GattService::UUID_HEALTH_THERMOMETER_SERVICE, HealthCareService::USER_DATA_SERVICE_UUID, HealthCareService::DEVICE_INFO_SERVICE_UUID }; uint8_t htsPosition = HealthCareService::HealthCareService::TEMPERATURE_LOCATION_FINGER; uint8_t hrmPosition = HealthCareService::HealthCareService::HRM_LOCATION_FINGER; /* STRUCTS/CLASSESS */ HealthCareService *HealthCareServicePtr; //PulseSensor PulseSensor(); static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20); Serial serial(USBTX, USBRX); /* ================== FUNCTION PROTOTYPES ==================== */ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *event); void onBleInitError(BLE &ble, ble_error_t error); void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); void onDataWrittenCallback(const GattWriteCallbackParams *params); void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context); void sendDataToProcessing(char symbol, int data); void updatePayload(void); void main_event(void); void periodicCallback(void); /* ==================== FUNCTION DETAILS ===================== */ /* Restart Advertising on disconnection */ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *event) { BLE::Instance().gap().startAdvertising(); printf("Device disconnected with mobile/table\r\n"); isConnectedToDevice = false; } void sendDataToProcessing(char symbol, int data) { if (symbol == 'B') { currentHRMCounter = data/1; printf("%c%d\r\n", symbol, (uint8_t)(data/1)); } } void updatePayload(void) { // Update the count in the SERVICE_DATA field of the advertising payload cnt++; uint8_t service_data[8]; /* first 2 bytes are for service UUID */ service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF; service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8; /* next 4 bytes are for client ID */ service_data[2] = 0x07; service_data[3] = 0x09; service_data[4] = 0x9A; service_data[5] = 0xAC; /* last 2 bytes are sensor data */ service_data[6] = cnt; service_data[7] = cnt; ble_error_t err = BLE::Instance().gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); } void main_event(void) { uint16_t intTemperatureValuex100; uint8_t fractionalTemperature; uint8_t decimalTemperature; if (BLE::Instance().gap().getState().connected) { isConnectedToDevice = true; } /* Do blocking calls or whatever is necessary for sensor polling. In our case, we simply update the Temperature measurement. */ /* TODO Read temperature */ currentTemperature = (currentTemperature + 0.16 > 42.79) ? 35.69 : currentTemperature + 1.18; /* TODO Read Heart Rate */ // currentHRMCounter = (currentHRMCounter + 1 > 120) ? 75 : (currentHRMCounter + HRM_increasement); /* Updated in callback function */ /* Some little tricks here to make the temperature decimal and fractional parts different from the send codes */ intTemperatureValuex100 = currentTemperature * 100; fractionalTemperature = intTemperatureValuex100 % 100; decimalTemperature = intTemperatureValuex100 / 100; if ((fractionalTemperature == START_SEND_INT_TEMP) || (fractionalTemperature == START_SEND_FLOAT_TEMP) || (fractionalTemperature == STOP_SEND_TEMP) || (fractionalTemperature == decimalTemperature)) { fractionalTemperature = fractionalTemperature + 1; } /* TODO Update Service data */ updatePayload(); /* sendCombinedTempAndHR = (currentTemperature * 100)* 1000 + currentHRMCounter */ sendCombinedTempAndHR = currentTemperature * 1000; /* Temperature float to int conversion */ sendCombinedTempAndHR = sendCombinedTempAndHR + (float)(currentHRMCounter/100.0); switch (startSendFloat) { case 0: sendCombinedHRAndTemp = currentHRMCounter * 100; sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)START_SEND_INT_TEMP; break; case 1: /* sendCombinedHRAndTemp = (currentHRMCounter * 100) + decimalTemperature */ /* Because the maximum size of HRM Value is 2 bytes */ sendCombinedHRAndTemp = currentHRMCounter * 100; sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)decimalTemperature; break; case 2: sendCombinedHRAndTemp = currentHRMCounter * 100; sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)START_SEND_FLOAT_TEMP; break; case 3: sendCombinedHRAndTemp = currentHRMCounter * 100; sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)fractionalTemperature; break; default: break; } // printf("sendCombinedTempAndHR %d\r\n", sendCombinedTempAndHR); // printf("sendCombinedTempAndHR %.2f\r\n\r\n", (float)sendCombinedTempAndHR); // printf("currentHRMCounter %d\r\n", currentHRMCounter); // printf("currentTemperature %d\r\n", (uint8_t)currentTemperature); // printf("sendCombinedHRAndTemp %d\r\n", sendCombinedHRAndTemp); if (isConnectedToDevice) { HealthCareServicePtr->updateTemperature(sendCombinedTempAndHR); HealthCareServicePtr->updateHeartRate(sendCombinedHRAndTemp); startSendFloat = (startSendFloat + 1) % 4; } else { startSendFloat = 0; } } void periodicCallback(void) { /* call main_event immediately */ eventQueue.call(main_event); } void printMacAddress() { /* Print out device MAC address to the console*/ Gap::AddressType_t addr_type; Gap::Address_t address; BLE::Instance().gap().getAddress(&addr_type, address); printf("DEVICE MAC ADDRESS: "); for (int i = 5; i >= 1; i--) { printf("%02x:", address[i]); } printf("%02x\r\n", address[0]); } void onBleInitError(BLE &ble, ble_error_t error) { /* Initialization error handling should go here */ printf("BLE init error!\r\n"); } /** * This callback allows the HealthCareService to receive updates to the controlState Characteristic. * * @param[in] params * Information about the characterisitc being updated. */ void onDataWrittenCallback(const GattWriteCallbackParams *params) { // printf("Write callback, value %d\r\n", *(params->data)); if ((params->handle == HealthCareServicePtr->getTypeHandle()) && (params->len >= 1)) { uint8_t oldPosition = htsPosition; htsPosition = *(params->data); printf("Old type: %d, New type %d\r\n", oldPosition, htsPosition); HealthCareServicePtr->updateType(htsPosition); } if ((params->handle == HealthCareServicePtr->getLocationHandle()) && (params->len >= 1)) { uint8_t oldPosition = hrmPosition; hrmPosition = *(params->data); printf("Old location: %d, New location %d\r\n", oldPosition, hrmPosition); HealthCareServicePtr->updateLocation(hrmPosition); } } /** * @brief Callback triggered when the ble initialization process has finished */ void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; ble_error_t error = params->error; uint8_t service_data[8]; if (error != BLE_ERROR_NONE) { onBleInitError(ble, error); return; } /* Ensure that it is the default instance of BLE */ if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } uint8_t initial_HRMIncreasement = 1; ble.gap().onDisconnection(&disconnectionCallback); ble.gattServer().onDataWritten(onDataWrittenCallback); HealthCareServicePtr = new HealthCareService(ble, currentTemperature, htsPosition, currentHRMCounter, hrmPosition, initial_HRMIncreasement); /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER ); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); /* No need to display the device name */ // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); /* uint8_t service_data[8]; */ /* first 2 bytes are for service UUID */ service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF; service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8; /* next 4 bytes are for client ID */ service_data[2] = 0x07; service_data[3] = 0x09; service_data[4] = 0x89; service_data[5] = 0xAB; /* last 2 bytes are sensor data */ service_data[6] = cnt; service_data[7] = cnt; ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA , (uint8_t *)service_data, sizeof(service_data)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ ble.gap().startAdvertising(); printMacAddress(); printf("BLE init successfully\r\n"); } void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { BLE &ble = BLE::Instance(); eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); } /* MAIN FUNCTION */ int main() { serial.baud(115200); printf("\r\n BODY WIRELESS SENSOR NETWORK\r\n"); /* call periodicCallback every 500ms */ PulseSensor sensor(A2, sendDataToProcessing, 50); eventQueue.call_every(1000, periodicCallback); /* init BLE */ BLE &ble = BLE::Instance(); ble.onEventsToProcess(scheduleBleEventsProcessing); ble.init(bleInitComplete); /* dispatch the event queue */ sensor.start(); eventQueue.dispatch_forever(); return 0; }