![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
nRF51822 BLE project for TMP102 temperature sensor
Dependencies: BLE_API TMP102 mbed nRF51822
main.cpp@4:e5fa4c8838db, 2015-07-31 (annotated)
- Committer:
- sunsmile2015
- Date:
- Fri Jul 31 09:11:43 2015 +0000
- Revision:
- 4:e5fa4c8838db
- Parent:
- 3:3eda308b78e6
- Child:
- 5:f4d74a8cad43
use update adv payload API to change temperature value
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunsmile2015 | 0:3dc6e424dba0 | 1 | |
sunsmile2015 | 0:3dc6e424dba0 | 2 | /* mbed Microcontroller Library |
sunsmile2015 | 0:3dc6e424dba0 | 3 | * Copyright (c) 2006-2015 ARM Limited |
sunsmile2015 | 0:3dc6e424dba0 | 4 | * |
sunsmile2015 | 0:3dc6e424dba0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sunsmile2015 | 0:3dc6e424dba0 | 6 | * you may not use this file except in compliance with the License. |
sunsmile2015 | 0:3dc6e424dba0 | 7 | * You may obtain a copy of the License at |
sunsmile2015 | 0:3dc6e424dba0 | 8 | * |
sunsmile2015 | 0:3dc6e424dba0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
sunsmile2015 | 0:3dc6e424dba0 | 10 | * |
sunsmile2015 | 0:3dc6e424dba0 | 11 | * Unless required by applicable law or agreed to in writing, software |
sunsmile2015 | 0:3dc6e424dba0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
sunsmile2015 | 0:3dc6e424dba0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sunsmile2015 | 0:3dc6e424dba0 | 14 | * See the License for the specific language governing permissions and |
sunsmile2015 | 0:3dc6e424dba0 | 15 | * limitations under the License. |
sunsmile2015 | 0:3dc6e424dba0 | 16 | */ |
sunsmile2015 | 0:3dc6e424dba0 | 17 | |
sunsmile2015 | 0:3dc6e424dba0 | 18 | #include "mbed.h" |
sunsmile2015 | 0:3dc6e424dba0 | 19 | #include "ble/BLE.h" |
sunsmile2015 | 0:3dc6e424dba0 | 20 | #include "TMP_nrf51/TMP_nrf51.h" |
sunsmile2015 | 0:3dc6e424dba0 | 21 | |
sunsmile2015 | 4:e5fa4c8838db | 22 | #define APP_SPECIFIC_ID_TEST 0xFEFE |
sunsmile2015 | 3:3eda308b78e6 | 23 | |
sunsmile2015 | 3:3eda308b78e6 | 24 | #pragma pack(1) |
sunsmile2015 | 4:e5fa4c8838db | 25 | struct ApplicationData_t { |
sunsmile2015 | 4:e5fa4c8838db | 26 | uint16_t applicationSpecificId; /* An ID used to identify temperature value |
sunsmile2015 | 4:e5fa4c8838db | 27 | in the manufacture specific AD data field */ |
sunsmile2015 | 4:e5fa4c8838db | 28 | TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */ |
sunsmile2015 | 4:e5fa4c8838db | 29 | }; |
sunsmile2015 | 3:3eda308b78e6 | 30 | #pragma pack() |
sunsmile2015 | 3:3eda308b78e6 | 31 | |
sunsmile2015 | 0:3dc6e424dba0 | 32 | BLE ble; |
sunsmile2015 | 2:b935358da5ba | 33 | TMP_nrf51 tempSensor; |
sunsmile2015 | 0:3dc6e424dba0 | 34 | DigitalOut alivenessLED(LED1, 1); |
sunsmile2015 | 4:e5fa4c8838db | 35 | static bool triggerTempValueUpdate = false; |
sunsmile2015 | 0:3dc6e424dba0 | 36 | |
sunsmile2015 | 2:b935358da5ba | 37 | void periodicCallback(void) |
sunsmile2015 | 2:b935358da5ba | 38 | { |
sunsmile2015 | 2:b935358da5ba | 39 | /* Do blinky on LED1 while we're waiting for BLE events */ |
sunsmile2015 | 2:b935358da5ba | 40 | alivenessLED = !alivenessLED; |
sunsmile2015 | 4:e5fa4c8838db | 41 | triggerTempValueUpdate = true; |
sunsmile2015 | 4:e5fa4c8838db | 42 | } |
sunsmile2015 | 4:e5fa4c8838db | 43 | |
sunsmile2015 | 4:e5fa4c8838db | 44 | void accumulateApplicationData(ApplicationData_t &appData) |
sunsmile2015 | 4:e5fa4c8838db | 45 | { |
sunsmile2015 | 4:e5fa4c8838db | 46 | appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; |
sunsmile2015 | 4:e5fa4c8838db | 47 | /* Read a new temperature value */ |
sunsmile2015 | 4:e5fa4c8838db | 48 | appData.tmpSensorValue = tempSensor.get(); |
sunsmile2015 | 0:3dc6e424dba0 | 49 | } |
sunsmile2015 | 0:3dc6e424dba0 | 50 | |
sunsmile2015 | 2:b935358da5ba | 51 | void temperatureValueAdvertising(void) |
sunsmile2015 | 2:b935358da5ba | 52 | { |
sunsmile2015 | 4:e5fa4c8838db | 53 | ApplicationData_t appData; |
sunsmile2015 | 3:3eda308b78e6 | 54 | |
sunsmile2015 | 4:e5fa4c8838db | 55 | accumulateApplicationData(appData); |
sunsmile2015 | 4:e5fa4c8838db | 56 | //printf("Temp is %f\r\n", (float)appData.tmpSensorValue); |
sunsmile2015 | 3:3eda308b78e6 | 57 | |
sunsmile2015 | 3:3eda308b78e6 | 58 | /* Setup advertising payload */ |
sunsmile2015 | 3:3eda308b78e6 | 59 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */ |
sunsmile2015 | 3:3eda308b78e6 | 60 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); /* Set appearance */ |
sunsmile2015 | 4:e5fa4c8838db | 61 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); /* Set data */ |
sunsmile2015 | 3:3eda308b78e6 | 62 | /* Setup advertising parameters */ |
sunsmile2015 | 2:b935358da5ba | 63 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
sunsmile2015 | 2:b935358da5ba | 64 | ble.gap().setAdvertisingInterval(500); |
sunsmile2015 | 3:3eda308b78e6 | 65 | /* Start advertising */ |
sunsmile2015 | 2:b935358da5ba | 66 | ble.gap().startAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 67 | } |
sunsmile2015 | 2:b935358da5ba | 68 | |
sunsmile2015 | 4:e5fa4c8838db | 69 | void updateSensorValueInAdvPayload(void) |
sunsmile2015 | 4:e5fa4c8838db | 70 | { |
sunsmile2015 | 4:e5fa4c8838db | 71 | ApplicationData_t appData; |
sunsmile2015 | 4:e5fa4c8838db | 72 | |
sunsmile2015 | 4:e5fa4c8838db | 73 | accumulateApplicationData(appData); |
sunsmile2015 | 4:e5fa4c8838db | 74 | |
sunsmile2015 | 4:e5fa4c8838db | 75 | /* Stop advertising first */ |
sunsmile2015 | 4:e5fa4c8838db | 76 | ble.gap().stopAdvertising(); |
sunsmile2015 | 4:e5fa4c8838db | 77 | /* Only update temperature value field */ |
sunsmile2015 | 4:e5fa4c8838db | 78 | ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); |
sunsmile2015 | 4:e5fa4c8838db | 79 | /* Start advertising again */ |
sunsmile2015 | 4:e5fa4c8838db | 80 | ble.gap().startAdvertising(); |
sunsmile2015 | 4:e5fa4c8838db | 81 | } |
sunsmile2015 | 4:e5fa4c8838db | 82 | |
sunsmile2015 | 2:b935358da5ba | 83 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 84 | { |
sunsmile2015 | 0:3dc6e424dba0 | 85 | Ticker ticker; |
sunsmile2015 | 4:e5fa4c8838db | 86 | /* Enable trigger every 2 seconds */ |
sunsmile2015 | 0:3dc6e424dba0 | 87 | ticker.attach(periodicCallback, 2); |
sunsmile2015 | 0:3dc6e424dba0 | 88 | |
sunsmile2015 | 0:3dc6e424dba0 | 89 | ble.init(); |
sunsmile2015 | 4:e5fa4c8838db | 90 | /* Start temperature advertising */ |
sunsmile2015 | 4:e5fa4c8838db | 91 | temperatureValueAdvertising(); |
sunsmile2015 | 4:e5fa4c8838db | 92 | |
sunsmile2015 | 0:3dc6e424dba0 | 93 | while (true) { |
sunsmile2015 | 4:e5fa4c8838db | 94 | if (triggerTempValueUpdate) { |
sunsmile2015 | 4:e5fa4c8838db | 95 | /* Update temperature value */ |
sunsmile2015 | 4:e5fa4c8838db | 96 | updateSensorValueInAdvPayload(); |
sunsmile2015 | 4:e5fa4c8838db | 97 | triggerTempValueUpdate = false; |
sunsmile2015 | 0:3dc6e424dba0 | 98 | } |
sunsmile2015 | 0:3dc6e424dba0 | 99 | ble.waitForEvent(); |
sunsmile2015 | 0:3dc6e424dba0 | 100 | } |
sunsmile2015 | 0:3dc6e424dba0 | 101 | } |