Getting the HIH6130 running with an BLE example

Dependencies:   BLE_API mbed nRF51822 HIH6130

Fork of BLE_HeartRate by Skarphedinn Sigurdsson

Committer:
andyhunti
Date:
Mon Oct 13 17:35:26 2014 +0000
Revision:
2:58715fb81c43
Parent:
1:db1d0a0c334e
Updates to use custom long UUIDs.; Incrementing number proof of life.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
skarphedinnos 0:8db7a61ae7f7 1 /* mbed Microcontroller Library
skarphedinnos 0:8db7a61ae7f7 2 * Copyright (c) 2006-2013 ARM Limited
skarphedinnos 0:8db7a61ae7f7 3 *
skarphedinnos 0:8db7a61ae7f7 4 * Licensed under the Apache License, Version 2.0 (the "License");
skarphedinnos 0:8db7a61ae7f7 5 * you may not use this file except in compliance with the License.
skarphedinnos 0:8db7a61ae7f7 6 * You may obtain a copy of the License at
skarphedinnos 0:8db7a61ae7f7 7 *
skarphedinnos 0:8db7a61ae7f7 8 * http://www.apache.org/licenses/LICENSE-2.0
skarphedinnos 0:8db7a61ae7f7 9 *
skarphedinnos 0:8db7a61ae7f7 10 * Unless required by applicable law or agreed to in writing, software
skarphedinnos 0:8db7a61ae7f7 11 * distributed under the License is distributed on an "AS IS" BASIS,
skarphedinnos 0:8db7a61ae7f7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
skarphedinnos 0:8db7a61ae7f7 13 * See the License for the specific language governing permissions and
skarphedinnos 0:8db7a61ae7f7 14 * limitations under the License.
skarphedinnos 0:8db7a61ae7f7 15 */
skarphedinnos 0:8db7a61ae7f7 16 //test
skarphedinnos 0:8db7a61ae7f7 17 #include "mbed.h"
andyhunti 2:58715fb81c43 18 #include "HIH6130.h"
skarphedinnos 0:8db7a61ae7f7 19 #include "BLEDevice.h"
skarphedinnos 0:8db7a61ae7f7 20
skarphedinnos 0:8db7a61ae7f7 21 BLEDevice ble;
skarphedinnos 0:8db7a61ae7f7 22 DigitalOut led1(LED1);
skarphedinnos 0:8db7a61ae7f7 23
andyhunti 2:58715fb81c43 24 #define PIN_SDA P0_22
andyhunti 2:58715fb81c43 25 #define PIN_SCL P0_20
andyhunti 2:58715fb81c43 26
andyhunti 2:58715fb81c43 27
andyhunti 1:db1d0a0c334e 28 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
skarphedinnos 0:8db7a61ae7f7 29 * it will have an impact on code-size and power consumption. */
skarphedinnos 0:8db7a61ae7f7 30 #if NEED_CONSOLE_OUTPUT
skarphedinnos 0:8db7a61ae7f7 31 Serial pc(USBTX, USBRX);
skarphedinnos 0:8db7a61ae7f7 32 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
skarphedinnos 0:8db7a61ae7f7 33 #else
skarphedinnos 0:8db7a61ae7f7 34 #define DEBUG(...) /* nothing */
skarphedinnos 0:8db7a61ae7f7 35 #endif /* #if NEED_CONSOLE_OUTPUT */
skarphedinnos 0:8db7a61ae7f7 36
skarphedinnos 0:8db7a61ae7f7 37 /* Heart Rate Service */
skarphedinnos 0:8db7a61ae7f7 38 /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
skarphedinnos 0:8db7a61ae7f7 39 /* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
skarphedinnos 0:8db7a61ae7f7 40 /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */
andyhunti 2:58715fb81c43 41
andyhunti 2:58715fb81c43 42 uint8_t theCurrTemp = 0xFF;
andyhunti 2:58715fb81c43 43 uint8_t theMaxTemp = 0xCC;
andyhunti 2:58715fb81c43 44 uint8_t theMinTemp = 0x00;
andyhunti 2:58715fb81c43 45 uint8_t theAlarmTemp = 0x11;
andyhunti 2:58715fb81c43 46
andyhunti 2:58715fb81c43 47 static uint8_t theCurrTempArr[20] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, theCurrTemp};
andyhunti 2:58715fb81c43 48 static uint8_t theMaxTempArr[2] = {0x00, theMaxTemp};
andyhunti 2:58715fb81c43 49 static uint8_t theMinTempArr[2] = {0x00, theMinTemp};
andyhunti 2:58715fb81c43 50 static uint8_t theAlarmTempArr[2] = {0x00, theAlarmTemp};
andyhunti 2:58715fb81c43 51
andyhunti 2:58715fb81c43 52 //GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm),
andyhunti 2:58715fb81c43 53 // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
andyhunti 2:58715fb81c43 54 //static const uint8_t location = 0x03; /* Finger */
andyhunti 2:58715fb81c43 55 //GattCharacteristic hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR,
andyhunti 2:58715fb81c43 56 // (uint8_t *)&location, sizeof(location), sizeof(location),
andyhunti 2:58715fb81c43 57 // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
andyhunti 2:58715fb81c43 58 //GattCharacteristic *hrmChars[] = {&hrmRate, &hrmLocation, };
andyhunti 2:58715fb81c43 59 //GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
andyhunti 2:58715fb81c43 60 const LongUUID_t service =
andyhunti 2:58715fb81c43 61 {
andyhunti 2:58715fb81c43 62 // DEADF154-0000-0000-0000-0000DEADF154
andyhunti 2:58715fb81c43 63 0x54, 0xF1, 0xAD, 0xDE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0xf1, 0xAD, 0xDE
andyhunti 2:58715fb81c43 64 };
andyhunti 2:58715fb81c43 65
andyhunti 2:58715fb81c43 66 const LongUUID_t currTemp =
andyhunti 2:58715fb81c43 67 {
andyhunti 2:58715fb81c43 68 // CCCCFFFF-DEAD-F154-1319-740381000000
andyhunti 2:58715fb81c43 69 0x00, 0x00, 0x00, 0x81, 0x03, 0x74, 0x19, 0x13, 0x54, 0xf1, 0xAD, 0xDE, 0xff, 0xff, 0xCC, 0xCC
andyhunti 2:58715fb81c43 70 };
andyhunti 2:58715fb81c43 71 const LongUUID_t minTemp =
andyhunti 2:58715fb81c43 72 {
andyhunti 2:58715fb81c43 73 // C0C0C0C0-DEAD-F154-1319-740381000000
andyhunti 2:58715fb81c43 74 0x00, 0x00, 0x00, 0x81, 0x03, 0x74, 0x19, 0x13, 0x54, 0xf1, 0xAD, 0xDE, 0xC0, 0xC0, 0xC0, 0xC0
andyhunti 2:58715fb81c43 75 };
andyhunti 2:58715fb81c43 76 const LongUUID_t maxTemp =
andyhunti 2:58715fb81c43 77 {
andyhunti 2:58715fb81c43 78 // EDEDEDED-DEAD-F154-1319-740381000000
andyhunti 2:58715fb81c43 79 0x00, 0x00, 0x00, 0x81, 0x03, 0x74, 0x19, 0x13, 0x54, 0xf1, 0xAD, 0xDE, 0xED, 0xED, 0xED, 0xED
andyhunti 2:58715fb81c43 80 };
andyhunti 2:58715fb81c43 81 const LongUUID_t alarmTemp =
andyhunti 2:58715fb81c43 82 {
andyhunti 2:58715fb81c43 83 // AAAAAAAA-DEAD-F154-1319-740381000000
andyhunti 2:58715fb81c43 84 0x00, 0x00, 0x00, 0x81, 0x03, 0x74, 0x19, 0x13, 0x54, 0xf1, 0xAD, 0xDE, 0xAA, 0xAA, 0xAA, 0xAA
andyhunti 2:58715fb81c43 85 };
andyhunti 2:58715fb81c43 86
andyhunti 2:58715fb81c43 87
andyhunti 2:58715fb81c43 88 GattCharacteristic gattCurrTemp(currTemp, theCurrTempArr, sizeof(theCurrTempArr), sizeof(theCurrTempArr), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
andyhunti 2:58715fb81c43 89 GattCharacteristic gattMaxTemp(maxTemp, theMaxTempArr, sizeof(theMaxTempArr), sizeof(theMaxTempArr), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
andyhunti 2:58715fb81c43 90 GattCharacteristic gattMinTemp(minTemp, theMinTempArr, sizeof(theMinTempArr), sizeof(theMinTempArr), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
andyhunti 2:58715fb81c43 91 GattCharacteristic gattAlarm(alarmTemp, theAlarmTempArr, sizeof(theAlarmTempArr), sizeof(theAlarmTempArr), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
andyhunti 2:58715fb81c43 92 GattCharacteristic *tempChars[] = {&gattCurrTemp, &gattMaxTemp, &gattMinTemp, &gattAlarm };
andyhunti 2:58715fb81c43 93 GattService deadFishService(service, tempChars, sizeof(tempChars)/sizeof(GattCharacteristic *));
andyhunti 2:58715fb81c43 94 //hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
andyhunti 2:58715fb81c43 95
andyhunti 2:58715fb81c43 96 HIH6130 hih6130(PIN_SDA, PIN_SCL);
skarphedinnos 0:8db7a61ae7f7 97
skarphedinnos 0:8db7a61ae7f7 98 void disconnectionCallback(void)
skarphedinnos 0:8db7a61ae7f7 99 {
skarphedinnos 0:8db7a61ae7f7 100 DEBUG("Disconnected!\n\r");
skarphedinnos 0:8db7a61ae7f7 101 DEBUG("Restarting the advertising process\n\r");
skarphedinnos 0:8db7a61ae7f7 102 ble.startAdvertising();
skarphedinnos 0:8db7a61ae7f7 103 }
skarphedinnos 0:8db7a61ae7f7 104
skarphedinnos 0:8db7a61ae7f7 105 /**
skarphedinnos 0:8db7a61ae7f7 106 * Triggered periodically by the 'ticker' interrupt; updates hrmCounter.
skarphedinnos 0:8db7a61ae7f7 107 */
skarphedinnos 0:8db7a61ae7f7 108 void periodicCallback(void)
skarphedinnos 0:8db7a61ae7f7 109 {
skarphedinnos 0:8db7a61ae7f7 110 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
skarphedinnos 0:8db7a61ae7f7 111
andyhunti 2:58715fb81c43 112 float humidity, temperature;
andyhunti 2:58715fb81c43 113 hih6130.ReadData(&humidity, &temperature);
andyhunti 2:58715fb81c43 114
skarphedinnos 0:8db7a61ae7f7 115 if (ble.getGapState().connected) {
skarphedinnos 0:8db7a61ae7f7 116 /* Update the HRM measurement */
skarphedinnos 0:8db7a61ae7f7 117 /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */
skarphedinnos 0:8db7a61ae7f7 118 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
andyhunti 2:58715fb81c43 119 //hrmCounter++;
andyhunti 2:58715fb81c43 120 // if (hrmCounter == 175) {
andyhunti 2:58715fb81c43 121 // hrmCounter = 100;
andyhunti 2:58715fb81c43 122 // }
andyhunti 2:58715fb81c43 123 // bpm[1] = hrmCounter;
andyhunti 2:58715fb81c43 124 // bpm[1] = 0xCC;
andyhunti 2:58715fb81c43 125 // bpm[1] = (uint8_t)temperature;
andyhunti 2:58715fb81c43 126 // DEBUG("cast temp to %d", bpm[1]);
andyhunti 2:58715fb81c43 127 // ble.updateCharacteristicValue(hrmRate.getHandle(), bpm, sizeof(bpm));
andyhunti 2:58715fb81c43 128 theCurrTempArr[1]++;
andyhunti 2:58715fb81c43 129 ble.updateCharacteristicValue(gattCurrTemp.getHandle(), theCurrTempArr, sizeof(theCurrTempArr));
skarphedinnos 0:8db7a61ae7f7 130 }
skarphedinnos 0:8db7a61ae7f7 131 }
skarphedinnos 0:8db7a61ae7f7 132
skarphedinnos 0:8db7a61ae7f7 133 int main(void)
skarphedinnos 0:8db7a61ae7f7 134 {
skarphedinnos 0:8db7a61ae7f7 135 led1 = 1;
skarphedinnos 0:8db7a61ae7f7 136 Ticker ticker;
andyhunti 2:58715fb81c43 137 ticker.attach(periodicCallback, 0.2);
skarphedinnos 0:8db7a61ae7f7 138
skarphedinnos 0:8db7a61ae7f7 139 DEBUG("Initialising the nRF51822\n\r");
skarphedinnos 0:8db7a61ae7f7 140 ble.init();
skarphedinnos 0:8db7a61ae7f7 141 ble.onDisconnection(disconnectionCallback);
skarphedinnos 0:8db7a61ae7f7 142
skarphedinnos 0:8db7a61ae7f7 143 /* setup advertising */
skarphedinnos 0:8db7a61ae7f7 144 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
skarphedinnos 0:8db7a61ae7f7 145 ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT);
skarphedinnos 0:8db7a61ae7f7 146 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
skarphedinnos 0:8db7a61ae7f7 147 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
skarphedinnos 0:8db7a61ae7f7 148 ble.startAdvertising();
andyhunti 2:58715fb81c43 149 DEBUG("Started Advertising\n\r");
skarphedinnos 0:8db7a61ae7f7 150
andyhunti 2:58715fb81c43 151 ble.addService(deadFishService);
andyhunti 2:58715fb81c43 152 DEBUG("Added service");
skarphedinnos 0:8db7a61ae7f7 153
skarphedinnos 0:8db7a61ae7f7 154 while (true) {
skarphedinnos 0:8db7a61ae7f7 155 ble.waitForEvent();
skarphedinnos 0:8db7a61ae7f7 156 }
skarphedinnos 0:8db7a61ae7f7 157 }