BLE ADV Gateway, converts advertisement to proper JSON serial output

Dependencies:   BLE_API mbed mbedtls nRF51822

Committer:
rgrover1
Date:
Wed Aug 26 12:21:47 2015 +0000
Revision:
5:f4d74a8cad43
Parent:
4:e5fa4c8838db
Child:
7:0a8bbb6dea16
A very simple temperature-beacon; to go with the temperature-scanner.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sunsmile2015 0:3dc6e424dba0 1 /* mbed Microcontroller Library
sunsmile2015 0:3dc6e424dba0 2 * Copyright (c) 2006-2015 ARM Limited
sunsmile2015 0:3dc6e424dba0 3 *
sunsmile2015 0:3dc6e424dba0 4 * Licensed under the Apache License, Version 2.0 (the "License");
sunsmile2015 0:3dc6e424dba0 5 * you may not use this file except in compliance with the License.
sunsmile2015 0:3dc6e424dba0 6 * You may obtain a copy of the License at
sunsmile2015 0:3dc6e424dba0 7 *
sunsmile2015 0:3dc6e424dba0 8 * http://www.apache.org/licenses/LICENSE-2.0
sunsmile2015 0:3dc6e424dba0 9 *
sunsmile2015 0:3dc6e424dba0 10 * Unless required by applicable law or agreed to in writing, software
sunsmile2015 0:3dc6e424dba0 11 * distributed under the License is distributed on an "AS IS" BASIS,
sunsmile2015 0:3dc6e424dba0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sunsmile2015 0:3dc6e424dba0 13 * See the License for the specific language governing permissions and
sunsmile2015 0:3dc6e424dba0 14 * limitations under the License.
sunsmile2015 0:3dc6e424dba0 15 */
sunsmile2015 0:3dc6e424dba0 16
sunsmile2015 0:3dc6e424dba0 17 #include "mbed.h"
rgrover1 5:f4d74a8cad43 18 #include "toolchain.h"
sunsmile2015 0:3dc6e424dba0 19 #include "ble/BLE.h"
sunsmile2015 0:3dc6e424dba0 20 #include "TMP_nrf51/TMP_nrf51.h"
sunsmile2015 0:3dc6e424dba0 21
rgrover1 5:f4d74a8cad43 22 BLE ble;
sunsmile2015 3:3eda308b78e6 23
rgrover1 5:f4d74a8cad43 24 static Ticker ticker;
rgrover1 5:f4d74a8cad43 25 static TMP_nrf51 tempSensor;
rgrover1 5:f4d74a8cad43 26 static bool triggerTempValueUpdate = false;
rgrover1 5:f4d74a8cad43 27 static DigitalOut alivenessLED(LED1, 1);
rgrover1 5:f4d74a8cad43 28
sunsmile2015 4:e5fa4c8838db 29 struct ApplicationData_t {
rgrover1 5:f4d74a8cad43 30 uint16_t applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */
rgrover1 5:f4d74a8cad43 31 TMP_nrf51::TempSensorValue_t tmpSensorValue; /* User defined application data */
rgrover1 5:f4d74a8cad43 32 } PACKED;
sunsmile2015 0:3dc6e424dba0 33
sunsmile2015 2:b935358da5ba 34 void periodicCallback(void)
sunsmile2015 2:b935358da5ba 35 {
rgrover1 5:f4d74a8cad43 36 alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. */
rgrover1 5:f4d74a8cad43 37
rgrover1 5:f4d74a8cad43 38 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
rgrover1 5:f4d74a8cad43 39 * heavy-weight sensor polling from the main thread (where we should be able to block safely, if needed). */
sunsmile2015 4:e5fa4c8838db 40 triggerTempValueUpdate = true;
sunsmile2015 4:e5fa4c8838db 41 }
sunsmile2015 4:e5fa4c8838db 42
rgrover1 5:f4d74a8cad43 43 void setupApplicationData(ApplicationData_t &appData)
sunsmile2015 4:e5fa4c8838db 44 {
rgrover1 5:f4d74a8cad43 45 static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
sunsmile2015 4:e5fa4c8838db 46 appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
rgrover1 5:f4d74a8cad43 47 appData.tmpSensorValue = tempSensor.get();
sunsmile2015 0:3dc6e424dba0 48 }
sunsmile2015 0:3dc6e424dba0 49
rgrover1 5:f4d74a8cad43 50 void startAdvertisingTemperature(void)
sunsmile2015 2:b935358da5ba 51 {
rgrover1 5:f4d74a8cad43 52 /* Setup advertising payload */
rgrover1 5:f4d74a8cad43 53 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 5:f4d74a8cad43 54 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
sunsmile2015 4:e5fa4c8838db 55 ApplicationData_t appData;
rgrover1 5:f4d74a8cad43 56 setupApplicationData(appData);
rgrover1 5:f4d74a8cad43 57 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
rgrover1 5:f4d74a8cad43 58
sunsmile2015 3:3eda308b78e6 59 /* Setup advertising parameters */
sunsmile2015 2:b935358da5ba 60 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
sunsmile2015 2:b935358da5ba 61 ble.gap().setAdvertisingInterval(500);
sunsmile2015 2:b935358da5ba 62
sunsmile2015 4:e5fa4c8838db 63 ble.gap().startAdvertising();
sunsmile2015 4:e5fa4c8838db 64 }
sunsmile2015 4:e5fa4c8838db 65
sunsmile2015 2:b935358da5ba 66 int main(void)
sunsmile2015 2:b935358da5ba 67 {
rgrover1 5:f4d74a8cad43 68 ticker.attach(periodicCallback, 2); /* trigger sensor polling every 2 seconds */
sunsmile2015 0:3dc6e424dba0 69
sunsmile2015 0:3dc6e424dba0 70 ble.init();
rgrover1 5:f4d74a8cad43 71 startAdvertisingTemperature();
rgrover1 5:f4d74a8cad43 72
sunsmile2015 0:3dc6e424dba0 73 while (true) {
sunsmile2015 4:e5fa4c8838db 74 if (triggerTempValueUpdate) {
rgrover1 5:f4d74a8cad43 75 // Do blocking calls or whatever hardware-specific action is necessary to poll the sensor.
rgrover1 5:f4d74a8cad43 76 ApplicationData_t appData;
rgrover1 5:f4d74a8cad43 77 setupApplicationData(appData);
rgrover1 5:f4d74a8cad43 78 ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
rgrover1 5:f4d74a8cad43 79
sunsmile2015 4:e5fa4c8838db 80 triggerTempValueUpdate = false;
sunsmile2015 0:3dc6e424dba0 81 }
rgrover1 5:f4d74a8cad43 82
sunsmile2015 0:3dc6e424dba0 83 ble.waitForEvent();
sunsmile2015 0:3dc6e424dba0 84 }
sunsmile2015 0:3dc6e424dba0 85 }