![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
BLE ADV Gateway, converts advertisement to proper JSON serial output
Dependencies: BLE_API mbed mbedtls nRF51822
main.cpp@2:b935358da5ba, 2015-07-17 (annotated)
- Committer:
- sunsmile2015
- Date:
- Fri Jul 17 08:46:31 2015 +0000
- Revision:
- 2:b935358da5ba
- Parent:
- 1:790c863a9ebd
- Child:
- 3:3eda308b78e6
change the coding style
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 "ble/DiscoveredCharacteristic.h" |
sunsmile2015 | 0:3dc6e424dba0 | 21 | #include "ble/DiscoveredService.h" |
sunsmile2015 | 0:3dc6e424dba0 | 22 | #include "TMP_nrf51/TMP_nrf51.h" |
sunsmile2015 | 0:3dc6e424dba0 | 23 | |
sunsmile2015 | 0:3dc6e424dba0 | 24 | BLE ble; |
sunsmile2015 | 2:b935358da5ba | 25 | TMP_nrf51 tempSensor; |
sunsmile2015 | 0:3dc6e424dba0 | 26 | DigitalOut alivenessLED(LED1, 1); |
sunsmile2015 | 2:b935358da5ba | 27 | static bool triggerTempValueRead = true; |
sunsmile2015 | 0:3dc6e424dba0 | 28 | |
sunsmile2015 | 2:b935358da5ba | 29 | void periodicCallback(void) |
sunsmile2015 | 2:b935358da5ba | 30 | { |
sunsmile2015 | 2:b935358da5ba | 31 | /* Do blinky on LED1 while we're waiting for BLE events */ |
sunsmile2015 | 2:b935358da5ba | 32 | alivenessLED = !alivenessLED; |
sunsmile2015 | 2:b935358da5ba | 33 | triggerTempValueRead = true; |
sunsmile2015 | 0:3dc6e424dba0 | 34 | } |
sunsmile2015 | 0:3dc6e424dba0 | 35 | |
sunsmile2015 | 2:b935358da5ba | 36 | void temperatureValueAdvertising(void) |
sunsmile2015 | 2:b935358da5ba | 37 | { |
sunsmile2015 | 2:b935358da5ba | 38 | TMP_nrf51::tmpSensorValue_t tempVal; |
sunsmile2015 | 2:b935358da5ba | 39 | /* Read a new temperature value */ |
sunsmile2015 | 2:b935358da5ba | 40 | tempVal = tempSensor.get(); |
sunsmile2015 | 2:b935358da5ba | 41 | printf("Temp is %f\r\n", tempVal); |
sunsmile2015 | 2:b935358da5ba | 42 | |
sunsmile2015 | 2:b935358da5ba | 43 | /* Stop advertising and clear the payload if in advertising state */ |
sunsmile2015 | 2:b935358da5ba | 44 | if((ble.gap().getState()).advertising == 1) { |
sunsmile2015 | 2:b935358da5ba | 45 | ble.gap().stopAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 46 | ble.gap().clearAdvertisingPayload(); |
sunsmile2015 | 2:b935358da5ba | 47 | } |
sunsmile2015 | 2:b935358da5ba | 48 | /* Setup advertising. */ |
sunsmile2015 | 2:b935358da5ba | 49 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
sunsmile2015 | 2:b935358da5ba | 50 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); |
sunsmile2015 | 2:b935358da5ba | 51 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&tempVal, sizeof(TMP_nrf51::tmpSensorValue_t)); |
sunsmile2015 | 2:b935358da5ba | 52 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
sunsmile2015 | 2:b935358da5ba | 53 | ble.gap().setAdvertisingInterval(500); |
sunsmile2015 | 0:3dc6e424dba0 | 54 | |
sunsmile2015 | 2:b935358da5ba | 55 | ble.gap().startAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 56 | } |
sunsmile2015 | 2:b935358da5ba | 57 | |
sunsmile2015 | 2:b935358da5ba | 58 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 59 | { |
sunsmile2015 | 0:3dc6e424dba0 | 60 | Ticker ticker; |
sunsmile2015 | 0:3dc6e424dba0 | 61 | /* Refresh temperature value every 2 seconds */ |
sunsmile2015 | 0:3dc6e424dba0 | 62 | ticker.attach(periodicCallback, 2); |
sunsmile2015 | 0:3dc6e424dba0 | 63 | |
sunsmile2015 | 0:3dc6e424dba0 | 64 | ble.init(); |
sunsmile2015 | 0:3dc6e424dba0 | 65 | |
sunsmile2015 | 0:3dc6e424dba0 | 66 | while (true) { |
sunsmile2015 | 2:b935358da5ba | 67 | if (triggerTempValueRead) { |
sunsmile2015 | 2:b935358da5ba | 68 | temperatureValueAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 69 | triggerTempValueRead = false; |
sunsmile2015 | 0:3dc6e424dba0 | 70 | } |
sunsmile2015 | 0:3dc6e424dba0 | 71 | ble.waitForEvent(); |
sunsmile2015 | 0:3dc6e424dba0 | 72 | } |
sunsmile2015 | 0:3dc6e424dba0 | 73 | } |