tmeperature to mobile phone

Dependencies:   BLE_API MPU6050 mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
youyou
Date:
Tue Jun 10 04:56:06 2014 +0000
Revision:
7:c08bcc5e6537
Parent:
5:b0baff4a124f
BLE_temperature

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:87a7fc231fae 1 /* mbed Microcontroller Library
ktownsend 0:87a7fc231fae 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 0:87a7fc231fae 3 *
ktownsend 0:87a7fc231fae 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:87a7fc231fae 5 * you may not use this file except in compliance with the License.
ktownsend 0:87a7fc231fae 6 * You may obtain a copy of the License at
ktownsend 0:87a7fc231fae 7 *
ktownsend 0:87a7fc231fae 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:87a7fc231fae 9 *
ktownsend 0:87a7fc231fae 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:87a7fc231fae 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:87a7fc231fae 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:87a7fc231fae 13 * See the License for the specific language governing permissions and
ktownsend 0:87a7fc231fae 14 * limitations under the License.
ktownsend 0:87a7fc231fae 15 */
ktownsend 0:87a7fc231fae 16
ktownsend 0:87a7fc231fae 17 #include "mbed.h"
ktownsend 0:87a7fc231fae 18 #include "nRF51822n.h"
youyou 7:c08bcc5e6537 19 #include "MPU6050.h"
ktownsend 0:87a7fc231fae 20
Rohit Grover 3:24e2b056d229 21 nRF51822n nrf; /* BLE radio driver */
ktownsend 0:87a7fc231fae 22
Rohit Grover 3:24e2b056d229 23 Serial pc(USBTX, USBRX);
youyou 7:c08bcc5e6537 24 MPU6050 mpu(P0_22, P0_20);
ktownsend 0:87a7fc231fae 25
ktownsend 0:87a7fc231fae 26 /* Battery Level Service */
youyou 7:c08bcc5e6537 27 uint8_t batt = 88; /* Battery level */
youyou 7:c08bcc5e6537 28
Rohit Grover 3:24e2b056d229 29 GattService battService (GattService::UUID_BATTERY_SERVICE);
Rohit Grover 3:24e2b056d229 30 GattCharacteristic battLevel (GattCharacteristic::UUID_BATTERY_LEVEL_CHAR,
Rohit Grover 3:24e2b056d229 31 1,
Rohit Grover 3:24e2b056d229 32 1,
Rohit Grover 3:24e2b056d229 33 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
Rohit Grover 3:24e2b056d229 34 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
youyou 7:c08bcc5e6537 35
youyou 7:c08bcc5e6537 36 GattService tempService (GattService::UUID_HEALTH_THERMOMETER_SERVICE);
youyou 7:c08bcc5e6537 37 GattCharacteristic tempLevel (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR,
youyou 7:c08bcc5e6537 38 2,
youyou 7:c08bcc5e6537 39 3,
youyou 7:c08bcc5e6537 40 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
youyou 7:c08bcc5e6537 41 GattCharacteristic tempLocation (
youyou 7:c08bcc5e6537 42 GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR,
youyou 7:c08bcc5e6537 43 1,
youyou 7:c08bcc5e6537 44 1,
youyou 7:c08bcc5e6537 45 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
ktownsend 0:87a7fc231fae 46
ktownsend 0:87a7fc231fae 47 /* Heart Rate Service */
ktownsend 0:87a7fc231fae 48 /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
ktownsend 0:87a7fc231fae 49 /* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
ktownsend 0:87a7fc231fae 50 /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */
Rohit Grover 3:24e2b056d229 51 GattService hrmService (GattService::UUID_HEART_RATE_SERVICE);
Rohit Grover 3:24e2b056d229 52 GattCharacteristic hrmRate (
Rohit Grover 3:24e2b056d229 53 GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR,
Rohit Grover 3:24e2b056d229 54 2,
Rohit Grover 3:24e2b056d229 55 3,
Rohit Grover 3:24e2b056d229 56 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
Rohit Grover 3:24e2b056d229 57 GattCharacteristic hrmLocation (
Rohit Grover 3:24e2b056d229 58 GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR,
Rohit Grover 3:24e2b056d229 59 1,
Rohit Grover 3:24e2b056d229 60 1,
Rohit Grover 3:24e2b056d229 61 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
ktownsend 0:87a7fc231fae 62
ktownsend 0:87a7fc231fae 63 /* Device Information service */
youyou 7:c08bcc5e6537 64 uint8_t deviceName[] = {'Y', 'o', 'u','Y', 'o', 'u'};
Rohit Grover 3:24e2b056d229 65 GattService deviceInformationService (
Rohit Grover 3:24e2b056d229 66 GattService::UUID_DEVICE_INFORMATION_SERVICE);
Rohit Grover 3:24e2b056d229 67 GattCharacteristic deviceManufacturer (
Rohit Grover 3:24e2b056d229 68 GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR,
Rohit Grover 3:24e2b056d229 69 sizeof(deviceName),
Rohit Grover 3:24e2b056d229 70 sizeof(deviceName),
Rohit Grover 3:24e2b056d229 71 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
youyou 7:c08bcc5e6537 72 uint8_t version[]={'Y', 'o', 'u','Y', 'o', 'u',' ','1','.','0'};
youyou 7:c08bcc5e6537 73 GattCharacteristic firmwareVersion (
youyou 7:c08bcc5e6537 74 GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR,
youyou 7:c08bcc5e6537 75 sizeof(version),
youyou 7:c08bcc5e6537 76 sizeof(version),
youyou 7:c08bcc5e6537 77 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
ktownsend 0:87a7fc231fae 78
ktownsend 0:87a7fc231fae 79 /* Advertising data and parameters */
Rohit Grover 3:24e2b056d229 80 GapAdvertisingData advData;
Rohit Grover 3:24e2b056d229 81 GapAdvertisingData scanResponse;
Rohit Grover 3:24e2b056d229 82 GapAdvertisingParams advParams (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 3:24e2b056d229 83 uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE,
Rohit Grover 3:24e2b056d229 84 GattService::UUID_DEVICE_INFORMATION_SERVICE,
youyou 7:c08bcc5e6537 85 GattService::UUID_HEART_RATE_SERVICE,
youyou 7:c08bcc5e6537 86 GattService::UUID_HEALTH_THERMOMETER_SERVICE};
ktownsend 0:87a7fc231fae 87
ktownsend 0:87a7fc231fae 88 /**************************************************************************/
ktownsend 0:87a7fc231fae 89 /*!
ktownsend 0:87a7fc231fae 90 @brief This custom class can be used to override any GapEvents
ktownsend 0:87a7fc231fae 91 that you are interested in handling on an application level.
ktownsend 0:87a7fc231fae 92 */
ktownsend 0:87a7fc231fae 93 /**************************************************************************/
ktownsend 0:87a7fc231fae 94 class GapEventHandler : public GapEvents
ktownsend 0:87a7fc231fae 95 {
Rohit Grover 3:24e2b056d229 96 virtual void onTimeout(void)
ktownsend 0:87a7fc231fae 97 {
youyou 7:c08bcc5e6537 98 pc.printf("Advertising Timeout!\r\n");
ktownsend 0:87a7fc231fae 99 // Restart the advertising process with a much slower interval,
ktownsend 0:87a7fc231fae 100 // only start advertising again after a button press, etc.
Rohit Grover 3:24e2b056d229 101 }
ktownsend 0:87a7fc231fae 102
ktownsend 0:87a7fc231fae 103 virtual void onConnected(void)
ktownsend 0:87a7fc231fae 104 {
youyou 7:c08bcc5e6537 105 pc.printf("Connected!\r\n");
ktownsend 0:87a7fc231fae 106 }
ktownsend 0:87a7fc231fae 107
ktownsend 0:87a7fc231fae 108 virtual void onDisconnected(void)
ktownsend 0:87a7fc231fae 109 {
youyou 7:c08bcc5e6537 110 pc.printf("Disconnected!\r\n");
youyou 7:c08bcc5e6537 111 pc.printf("Restarting the advertising process\r\n");
ktownsend 0:87a7fc231fae 112 nrf.getGap().startAdvertising(advParams);
ktownsend 0:87a7fc231fae 113 }
ktownsend 0:87a7fc231fae 114 };
ktownsend 0:87a7fc231fae 115
ktownsend 0:87a7fc231fae 116 /**************************************************************************/
ktownsend 0:87a7fc231fae 117 /*!
ktownsend 0:87a7fc231fae 118 @brief This custom class can be used to override any GattServerEvents
ktownsend 0:87a7fc231fae 119 that you are interested in handling on an application level.
ktownsend 0:87a7fc231fae 120 */
ktownsend 0:87a7fc231fae 121 /**************************************************************************/
ktownsend 0:87a7fc231fae 122 class GattServerEventHandler : public GattServerEvents
ktownsend 0:87a7fc231fae 123 {
ktownsend 0:87a7fc231fae 124 //virtual void onDataSent(uint16_t charHandle) {}
ktownsend 0:87a7fc231fae 125 //virtual void onDataWritten(uint16_t charHandle) {}
Rohit Grover 3:24e2b056d229 126
ktownsend 0:87a7fc231fae 127 virtual void onUpdatesEnabled(uint16_t charHandle)
ktownsend 0:87a7fc231fae 128 {
Rohit Grover 5:b0baff4a124f 129 if (charHandle == hrmRate.getHandle()) {
youyou 7:c08bcc5e6537 130 pc.printf("Heart rate notify enabled\r\n");
youyou 7:c08bcc5e6537 131 }
youyou 7:c08bcc5e6537 132 if (charHandle == tempLevel.getHandle()) {
youyou 7:c08bcc5e6537 133 pc.printf("Temperature notify enabled\r\n");
Rohit Grover 3:24e2b056d229 134 }
ktownsend 0:87a7fc231fae 135 }
ktownsend 0:87a7fc231fae 136
ktownsend 0:87a7fc231fae 137 virtual void onUpdatesDisabled(uint16_t charHandle)
ktownsend 0:87a7fc231fae 138 {
Rohit Grover 5:b0baff4a124f 139 if (charHandle == hrmRate.getHandle()) {
youyou 7:c08bcc5e6537 140 pc.printf("Heart rate notify disabled\r\n");
youyou 7:c08bcc5e6537 141 }
youyou 7:c08bcc5e6537 142 if (charHandle == tempLevel.getHandle()) {
youyou 7:c08bcc5e6537 143 pc.printf("Temperature notify disabled\r\n");
Rohit Grover 3:24e2b056d229 144 }
ktownsend 0:87a7fc231fae 145 }
ktownsend 0:87a7fc231fae 146
ktownsend 0:87a7fc231fae 147 //virtual void onConfirmationReceived(uint16_t charHandle) {}
ktownsend 0:87a7fc231fae 148 };
ktownsend 0:87a7fc231fae 149
ktownsend 0:87a7fc231fae 150 /**************************************************************************/
ktownsend 0:87a7fc231fae 151 /*!
ktownsend 0:87a7fc231fae 152 @brief Program entry point
ktownsend 0:87a7fc231fae 153 */
ktownsend 0:87a7fc231fae 154 /**************************************************************************/
ktownsend 0:87a7fc231fae 155 int main(void)
ktownsend 0:87a7fc231fae 156 {
youyou 7:c08bcc5e6537 157 float accdata[3];
youyou 7:c08bcc5e6537 158 uint16_t time=0;
youyou 7:c08bcc5e6537 159 wait(1);
youyou 7:c08bcc5e6537 160 pc.baud(38400);
youyou 7:c08bcc5e6537 161
ktownsend 0:87a7fc231fae 162 /* Setup the local GAP/GATT event handlers */
ktownsend 0:87a7fc231fae 163 nrf.getGap().setEventHandler(new GapEventHandler());
ktownsend 0:87a7fc231fae 164 nrf.getGattServer().setEventHandler(new GattServerEventHandler());
ktownsend 0:87a7fc231fae 165
ktownsend 0:87a7fc231fae 166 /* Initialise the nRF51822 */
youyou 7:c08bcc5e6537 167 pc.printf("Initialising the nRF51822...\r\n");
ktownsend 0:87a7fc231fae 168 nrf.init();
ktownsend 0:87a7fc231fae 169
ktownsend 0:87a7fc231fae 170 /* Make sure we get a clean start */
ktownsend 0:87a7fc231fae 171 nrf.reset();
ktownsend 0:87a7fc231fae 172
ktownsend 0:87a7fc231fae 173 /* Add BLE-Only flag and complete service list to the advertising data */
ktownsend 0:87a7fc231fae 174 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 3:24e2b056d229 175 advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
Rohit Grover 3:24e2b056d229 176 (uint8_t *)uuid16_list, sizeof(uuid16_list));
ktownsend 0:87a7fc231fae 177 advData.addAppearance(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT);
ktownsend 0:87a7fc231fae 178 nrf.getGap().setAdvertisingData(advData, scanResponse);
ktownsend 0:87a7fc231fae 179
ktownsend 0:87a7fc231fae 180 /* Add the Battery Level service */
ktownsend 0:87a7fc231fae 181 battService.addCharacteristic(battLevel);
ktownsend 0:87a7fc231fae 182 nrf.getGattServer().addService(battService);
ktownsend 0:87a7fc231fae 183
ktownsend 0:87a7fc231fae 184 /* Add the Device Information service */
ktownsend 0:87a7fc231fae 185 deviceInformationService.addCharacteristic(deviceManufacturer);
youyou 7:c08bcc5e6537 186 deviceInformationService.addCharacteristic(firmwareVersion);
ktownsend 0:87a7fc231fae 187 nrf.getGattServer().addService(deviceInformationService);
Rohit Grover 3:24e2b056d229 188
ktownsend 0:87a7fc231fae 189 /* Add the Heart Rate service */
ktownsend 0:87a7fc231fae 190 hrmService.addCharacteristic(hrmRate);
ktownsend 0:87a7fc231fae 191 hrmService.addCharacteristic(hrmLocation);
ktownsend 0:87a7fc231fae 192 nrf.getGattServer().addService(hrmService);
youyou 7:c08bcc5e6537 193
youyou 7:c08bcc5e6537 194 tempService.addCharacteristic(tempLevel);
youyou 7:c08bcc5e6537 195 tempService.addCharacteristic(tempLocation);
youyou 7:c08bcc5e6537 196 nrf.getGattServer().addService(tempService);
Rohit Grover 3:24e2b056d229 197
ktownsend 0:87a7fc231fae 198 /* Start advertising (make sure you've added all your data first) */
ktownsend 0:87a7fc231fae 199 nrf.getGap().startAdvertising(advParams);
youyou 7:c08bcc5e6537 200
youyou 7:c08bcc5e6537 201
youyou 7:c08bcc5e6537 202 mpu.setFrequency(100000);
youyou 7:c08bcc5e6537 203 wait_ms(10);
youyou 7:c08bcc5e6537 204 if (mpu.testConnection())
youyou 7:c08bcc5e6537 205 {
youyou 7:c08bcc5e6537 206 pc.printf("MPU connection succeeded!\r\n");
youyou 7:c08bcc5e6537 207 }
youyou 7:c08bcc5e6537 208 else
youyou 7:c08bcc5e6537 209 {
youyou 7:c08bcc5e6537 210 pc.printf("MPU connection failed!\r\n"); //Todo: If connection fails, retry a couple times. Try resetting MPU (this would need another wire?)
youyou 7:c08bcc5e6537 211 }
youyou 7:c08bcc5e6537 212 wait(2);
youyou 7:c08bcc5e6537 213 mpu.setSleepMode(false);
youyou 7:c08bcc5e6537 214 mpu.setBW(MPU6050_BW_20);
youyou 7:c08bcc5e6537 215 mpu.setGyroRange(MPU6050_GYRO_RANGE_500);
youyou 7:c08bcc5e6537 216 mpu.setAcceleroRange(MPU6050_ACCELERO_RANGE_2G);
youyou 7:c08bcc5e6537 217
Rohit Grover 3:24e2b056d229 218
Rohit Grover 3:24e2b056d229 219 /* Wait until we are connected to a central device before updating
Rohit Grover 3:24e2b056d229 220 * anything */
youyou 7:c08bcc5e6537 221 pc.printf("Waiting for a connection ...\r\n");
Rohit Grover 3:24e2b056d229 222 while (!nrf.getGap().state.connected) {
ktownsend 0:87a7fc231fae 223 }
youyou 7:c08bcc5e6537 224 wait(3);
youyou 7:c08bcc5e6537 225
youyou 7:c08bcc5e6537 226 pc.printf("Connected!\r\n");
Rohit Grover 3:24e2b056d229 227
ktownsend 0:87a7fc231fae 228 /* Now that we're live, update the battery level characteristic, and */
ktownsend 0:87a7fc231fae 229 /* change the device manufacturer characteristic to 'mbed' */
Rohit Grover 5:b0baff4a124f 230 nrf.getGattServer().updateValue(battLevel.getHandle(), (uint8_t *)&batt,
Rohit Grover 3:24e2b056d229 231 sizeof(batt));
Rohit Grover 5:b0baff4a124f 232 nrf.getGattServer().updateValue(deviceManufacturer.getHandle(),
Rohit Grover 3:24e2b056d229 233 deviceName,
Rohit Grover 3:24e2b056d229 234 sizeof(deviceName));
youyou 7:c08bcc5e6537 235 nrf.getGattServer().updateValue(firmwareVersion.getHandle(),
youyou 7:c08bcc5e6537 236 version,
youyou 7:c08bcc5e6537 237 sizeof(version));
youyou 7:c08bcc5e6537 238
ktownsend 0:87a7fc231fae 239 /* Set the heart rate monitor location (one time only) */
ktownsend 0:87a7fc231fae 240 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */
ktownsend 0:87a7fc231fae 241 uint8_t location = 0x03; /* Finger */
ktownsend 0:87a7fc231fae 242 uint8_t hrmCounter = 100;
Rohit Grover 5:b0baff4a124f 243 nrf.getGattServer().updateValue(hrmLocation.getHandle(),
Rohit Grover 3:24e2b056d229 244 (uint8_t *)&location,
Rohit Grover 3:24e2b056d229 245 sizeof(location));
youyou 7:c08bcc5e6537 246 nrf.getGattServer().updateValue(tempLocation.getHandle(),
youyou 7:c08bcc5e6537 247 (uint8_t *)&location,
youyou 7:c08bcc5e6537 248 sizeof(location));
ktownsend 0:87a7fc231fae 249
youyou 7:c08bcc5e6537 250
youyou 7:c08bcc5e6537 251 for (;; )
youyou 7:c08bcc5e6537 252 {
Rohit Grover 3:24e2b056d229 253 wait(1);
Rohit Grover 3:24e2b056d229 254
youyou 7:c08bcc5e6537 255 /* Update battery level
Rohit Grover 3:24e2b056d229 256 batt++;
Rohit Grover 3:24e2b056d229 257 if (batt > 100) {
Rohit Grover 3:24e2b056d229 258 batt = 72;
Rohit Grover 3:24e2b056d229 259 }
Rohit Grover 5:b0baff4a124f 260 nrf.getGattServer().updateValue(battLevel.getHandle(),
Rohit Grover 3:24e2b056d229 261 (uint8_t *)&batt,
youyou 7:c08bcc5e6537 262 sizeof(batt)); */
ktownsend 0:87a7fc231fae 263
ktownsend 0:87a7fc231fae 264 /* Update the HRM measurement */
ktownsend 0:87a7fc231fae 265 /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */
ktownsend 0:87a7fc231fae 266 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
Rohit Grover 3:24e2b056d229 267 hrmCounter++;
Rohit Grover 3:24e2b056d229 268 if (hrmCounter == 175) {
Rohit Grover 3:24e2b056d229 269 hrmCounter = 100;
Rohit Grover 3:24e2b056d229 270 }
Rohit Grover 3:24e2b056d229 271 uint8_t bpm[2] = {0x00, hrmCounter};
Rohit Grover 5:b0baff4a124f 272 nrf.getGattServer().updateValue(hrmRate.getHandle(), bpm, sizeof(bpm));
youyou 7:c08bcc5e6537 273
youyou 7:c08bcc5e6537 274 float f =mpu.getTemp();
youyou 7:c08bcc5e6537 275 int t = f;
youyou 7:c08bcc5e6537 276 uint8_t temper[2];
youyou 7:c08bcc5e6537 277 temper[0]=t>>8;
youyou 7:c08bcc5e6537 278 temper[1]=t;
youyou 7:c08bcc5e6537 279 nrf.getGattServer().updateValue(tempLevel.getHandle(), temper, sizeof(temper));
youyou 7:c08bcc5e6537 280
youyou 7:c08bcc5e6537 281 pc.printf("HRM:");
youyou 7:c08bcc5e6537 282 pc.printf("%d",hrmCounter);
youyou 7:c08bcc5e6537 283 pc.printf(" Temperature:");
youyou 7:c08bcc5e6537 284 pc.printf("%.2f",f);
youyou 7:c08bcc5e6537 285 pc.printf(" ");
youyou 7:c08bcc5e6537 286 mpu.getAccelero(accdata);
youyou 7:c08bcc5e6537 287 time++;
youyou 7:c08bcc5e6537 288 pc.printf("X:%.2f Y:%.2f Z:%.2f Time:%ds\r\n",accdata[0], accdata[1], accdata[2],time);
ktownsend 0:87a7fc231fae 289 }
youyou 7:c08bcc5e6537 290 }