Graduation Thesis, use Nucleo and X-Nucleo BLE

Dependencies:   PulseSensor GSM Thermometer KalmanFilter

Committer:
DuyLionTran
Date:
Sun May 27 14:51:21 2018 +0000
Revision:
19:a4f3328e9d09
Parent:
18:a9d67c51715e
Child:
20:d88c0f1fd8cc
adding flash programming and sensor read

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