Graduation Thesis, use Nucleo and X-Nucleo BLE

Dependencies:   PulseSensor GSM Thermometer KalmanFilter

Committer:
DuyLionTran
Date:
Wed Feb 14 01:29:53 2018 +0000
Revision:
1:9eadd2dc4b6e
Parent:
0:64ca984b3efd
Child:
2:16f6cfcd7505
version 0.8 02-12-2018

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 0:64ca984b3efd 1 /**
DuyLionTran 0:64ca984b3efd 2 * This is the code for "BLE Device for motorbike". The device is attached on any bike at will.
DuyLionTran 0:64ca984b3efd 3 * User can control 2 switches and these switches can control anything that user wants ie: turn on
DuyLionTran 0:64ca984b3efd 4 * the bike, turn on the alarm system of the bike, turn on the light...
DuyLionTran 0:64ca984b3efd 5 * Temperature sensor is also included in the device. User can view the temperature when he/she gets
DuyLionTran 0:64ca984b3efd 6 * near the bike.
DuyLionTran 0:64ca984b3efd 7
DuyLionTran 0:64ca984b3efd 8 * Revision:
DuyLionTran 1:9eadd2dc4b6e 9 * version 0.8 02-12-2018
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 0:64ca984b3efd 16
DuyLionTran 0:64ca984b3efd 17 /* ======================== DEFINES ========================== */
DuyLionTran 0:64ca984b3efd 18
DuyLionTran 0:64ca984b3efd 19 /* ======================= VARIABLES ========================= */
DuyLionTran 0:64ca984b3efd 20 /* GLOBAL VARIABLES */
DuyLionTran 0:64ca984b3efd 21 static float currentTemperature = 39.6;
DuyLionTran 0:64ca984b3efd 22 static uint8_t currentHRMCounter = 80;
DuyLionTran 0:64ca984b3efd 23
DuyLionTran 0:64ca984b3efd 24 /* PRIVATE VARIABLES */
DuyLionTran 0:64ca984b3efd 25 const static char DEVICE_NAME[] = "HEALTH STATE";
DuyLionTran 0:64ca984b3efd 26 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
DuyLionTran 0:64ca984b3efd 27
DuyLionTran 0:64ca984b3efd 28
DuyLionTran 0:64ca984b3efd 29 /* STRUCTS/CLASSESS */
DuyLionTran 0:64ca984b3efd 30 static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20);
DuyLionTran 0:64ca984b3efd 31 HealthCareService *HealthCareServicePtr;
DuyLionTran 0:64ca984b3efd 32
DuyLionTran 0:64ca984b3efd 33 /* ================== FUNCTION PROTOTYPES ==================== */
DuyLionTran 0:64ca984b3efd 34
DuyLionTran 0:64ca984b3efd 35 /* ==================== FUNCTION DETAILS ===================== */
DuyLionTran 0:64ca984b3efd 36 /* Restart Advertising on disconnection*/
DuyLionTran 0:64ca984b3efd 37 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) {
DuyLionTran 0:64ca984b3efd 38 BLE::Instance().gap().startAdvertising();
DuyLionTran 0:64ca984b3efd 39 }
DuyLionTran 0:64ca984b3efd 40
DuyLionTran 0:64ca984b3efd 41
DuyLionTran 0:64ca984b3efd 42 void main_event(void) {
DuyLionTran 0:64ca984b3efd 43 /* Do blocking calls or whatever is necessary for sensor polling.
DuyLionTran 0:64ca984b3efd 44 In our case, we simply update the Temperature measurement. */
DuyLionTran 0:64ca984b3efd 45 currentTemperature = (currentTemperature + 0.1 > 43.0) ? 39.6 : currentTemperature + 0.1;
DuyLionTran 0:64ca984b3efd 46 currentHRMCounter = (currentHRMCounter + 1 > 120) ? 80 : currentHRMCounter + 1;
DuyLionTran 0:64ca984b3efd 47 HealthCareServicePtr->updateTemperature(currentTemperature);
DuyLionTran 0:64ca984b3efd 48 HealthCareServicePtr->updateHeartRate(currentHRMCounter);
DuyLionTran 0:64ca984b3efd 49 }
DuyLionTran 0:64ca984b3efd 50
DuyLionTran 0:64ca984b3efd 51 void periodicCallback(void) {
DuyLionTran 0:64ca984b3efd 52 if (BLE::Instance().gap().getState().connected) {
DuyLionTran 0:64ca984b3efd 53 eventQueue.call(main_event);
DuyLionTran 0:64ca984b3efd 54 }
DuyLionTran 0:64ca984b3efd 55 }
DuyLionTran 0:64ca984b3efd 56
DuyLionTran 0:64ca984b3efd 57 void printMacAddress() {
DuyLionTran 0:64ca984b3efd 58 /* Print out device MAC address to the console*/
DuyLionTran 0:64ca984b3efd 59 Gap::AddressType_t addr_type;
DuyLionTran 0:64ca984b3efd 60 Gap::Address_t address;
DuyLionTran 0:64ca984b3efd 61 BLE::Instance().gap().getAddress(&addr_type, address);
DuyLionTran 0:64ca984b3efd 62 printf("DEVICE MAC ADDRESS: ");
DuyLionTran 0:64ca984b3efd 63 for (int i = 5; i >= 1; i--) {
DuyLionTran 0:64ca984b3efd 64 printf("%02x:", address[i]);
DuyLionTran 0:64ca984b3efd 65 }
DuyLionTran 0:64ca984b3efd 66 printf("%02x\r\n", address[0]);
DuyLionTran 0:64ca984b3efd 67 }
DuyLionTran 0:64ca984b3efd 68
DuyLionTran 0:64ca984b3efd 69 void onBleInitError(BLE &ble, ble_error_t error) {
DuyLionTran 0:64ca984b3efd 70 /* Initialization error handling should go here */
DuyLionTran 0:64ca984b3efd 71 }
DuyLionTran 0:64ca984b3efd 72
DuyLionTran 0:64ca984b3efd 73 /**
DuyLionTran 0:64ca984b3efd 74 * @brief Callback triggered when the ble initialization process has finished
DuyLionTran 0:64ca984b3efd 75 */
DuyLionTran 0:64ca984b3efd 76 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) {
DuyLionTran 0:64ca984b3efd 77 BLE& ble = params->ble;
DuyLionTran 0:64ca984b3efd 78 ble_error_t error = params->error;
DuyLionTran 0:64ca984b3efd 79
DuyLionTran 0:64ca984b3efd 80 if (error != BLE_ERROR_NONE) {
DuyLionTran 0:64ca984b3efd 81 onBleInitError(ble, error);
DuyLionTran 0:64ca984b3efd 82 return;
DuyLionTran 0:64ca984b3efd 83 }
DuyLionTran 0:64ca984b3efd 84
DuyLionTran 0:64ca984b3efd 85 /* Ensure that it is the default instance of BLE */
DuyLionTran 0:64ca984b3efd 86 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
DuyLionTran 0:64ca984b3efd 87 return;
DuyLionTran 0:64ca984b3efd 88 }
DuyLionTran 0:64ca984b3efd 89
DuyLionTran 0:64ca984b3efd 90 ble.gap().onDisconnection(disconnectionCallback);
DuyLionTran 0:64ca984b3efd 91 HealthCareServicePtr = new HealthCareService(ble, currentTemperature, 3,
DuyLionTran 1:9eadd2dc4b6e 92 currentHRMCounter, 3);
DuyLionTran 0:64ca984b3efd 93
DuyLionTran 0:64ca984b3efd 94 /* setup advertising */
DuyLionTran 0:64ca984b3efd 95 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
DuyLionTran 0:64ca984b3efd 96 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
DuyLionTran 0:64ca984b3efd 97 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
DuyLionTran 0:64ca984b3efd 98 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
DuyLionTran 0:64ca984b3efd 99 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
DuyLionTran 0:64ca984b3efd 100 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
DuyLionTran 0:64ca984b3efd 101 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
DuyLionTran 0:64ca984b3efd 102 ble.gap().startAdvertising();
DuyLionTran 0:64ca984b3efd 103
DuyLionTran 0:64ca984b3efd 104 printMacAddress();
DuyLionTran 0:64ca984b3efd 105 }
DuyLionTran 0:64ca984b3efd 106
DuyLionTran 0:64ca984b3efd 107 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
DuyLionTran 0:64ca984b3efd 108 BLE &ble = BLE::Instance();
DuyLionTran 0:64ca984b3efd 109 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
DuyLionTran 0:64ca984b3efd 110 }
DuyLionTran 0:64ca984b3efd 111
DuyLionTran 0:64ca984b3efd 112 /* MAIN FUNCTION */
DuyLionTran 0:64ca984b3efd 113 int main() {
DuyLionTran 0:64ca984b3efd 114 eventQueue.call_every(500, periodicCallback);
DuyLionTran 0:64ca984b3efd 115
DuyLionTran 0:64ca984b3efd 116 BLE &ble = BLE::Instance();
DuyLionTran 0:64ca984b3efd 117 ble.onEventsToProcess(scheduleBleEventsProcessing);
DuyLionTran 0:64ca984b3efd 118 ble.init(bleInitComplete);
DuyLionTran 0:64ca984b3efd 119
DuyLionTran 0:64ca984b3efd 120 eventQueue.dispatch_forever();
DuyLionTran 0:64ca984b3efd 121
DuyLionTran 0:64ca984b3efd 122 return 0;
DuyLionTran 0:64ca984b3efd 123 }