BLE ADV Gateway, converts advertisement to proper JSON serial output

Dependencies:   BLE_API mbed mbedtls nRF51822

Committer:
sunsmile2015
Date:
Tue Jul 14 07:09:47 2015 +0000
Revision:
0:3dc6e424dba0
Child:
1:790c863a9ebd
initial version

Who changed what in which revision?

UserRevisionLine numberNew 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
sunsmile2015 0:3dc6e424dba0 25
sunsmile2015 0:3dc6e424dba0 26 BLE ble;
sunsmile2015 0:3dc6e424dba0 27
sunsmile2015 0:3dc6e424dba0 28 DigitalOut alivenessLED(LED1, 1);
sunsmile2015 0:3dc6e424dba0 29 TMP_nrf51 tempSensor;
sunsmile2015 0:3dc6e424dba0 30
sunsmile2015 0:3dc6e424dba0 31 bool triggerLedCharacteristic = false;
sunsmile2015 0:3dc6e424dba0 32
sunsmile2015 0:3dc6e424dba0 33 uint8_t ADV_INFO[6] = {0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00}; /* Special character || temperature value */
sunsmile2015 0:3dc6e424dba0 34 uint8_t fNewTempValue;
sunsmile2015 0:3dc6e424dba0 35
sunsmile2015 0:3dc6e424dba0 36 void periodicCallback(void) {
sunsmile2015 0:3dc6e424dba0 37 alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
sunsmile2015 0:3dc6e424dba0 38 fNewTempValue = 1;
sunsmile2015 0:3dc6e424dba0 39 }
sunsmile2015 0:3dc6e424dba0 40
sunsmile2015 0:3dc6e424dba0 41
sunsmile2015 0:3dc6e424dba0 42
sunsmile2015 0:3dc6e424dba0 43 int main(void) {
sunsmile2015 0:3dc6e424dba0 44 Ticker ticker;
sunsmile2015 0:3dc6e424dba0 45 /* Refresh temperature value every 2 seconds */
sunsmile2015 0:3dc6e424dba0 46 ticker.attach(periodicCallback, 2);
sunsmile2015 0:3dc6e424dba0 47
sunsmile2015 0:3dc6e424dba0 48 ble.init();
sunsmile2015 0:3dc6e424dba0 49
sunsmile2015 0:3dc6e424dba0 50 while (true) {
sunsmile2015 0:3dc6e424dba0 51 if (fNewTempValue) {
sunsmile2015 0:3dc6e424dba0 52 float tempVal;
sunsmile2015 0:3dc6e424dba0 53 tempVal = tempSensor.get();
sunsmile2015 0:3dc6e424dba0 54 memcpy(&ADV_INFO[2], &tempVal, 4); /* 4 bytes left for tempVal */
sunsmile2015 0:3dc6e424dba0 55 printf("temp is %f\r\n", tempVal);
sunsmile2015 0:3dc6e424dba0 56
sunsmile2015 0:3dc6e424dba0 57 if((ble.gap().getState()).advertising == 1) {
sunsmile2015 0:3dc6e424dba0 58 ble.gap().stopAdvertising();
sunsmile2015 0:3dc6e424dba0 59 ble.gap().clearAdvertisingPayload();
sunsmile2015 0:3dc6e424dba0 60 }
sunsmile2015 0:3dc6e424dba0 61 /* Setup advertising. */
sunsmile2015 0:3dc6e424dba0 62 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_LIMITED_DISCOVERABLE);
sunsmile2015 0:3dc6e424dba0 63 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN);
sunsmile2015 0:3dc6e424dba0 64 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)ADV_INFO, sizeof(ADV_INFO));
sunsmile2015 0:3dc6e424dba0 65 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
sunsmile2015 0:3dc6e424dba0 66 ble.gap().setAdvertisingInterval(500);
sunsmile2015 0:3dc6e424dba0 67
sunsmile2015 0:3dc6e424dba0 68 ble.gap().startAdvertising();
sunsmile2015 0:3dc6e424dba0 69
sunsmile2015 0:3dc6e424dba0 70 fNewTempValue = 0;
sunsmile2015 0:3dc6e424dba0 71 }
sunsmile2015 0:3dc6e424dba0 72 ble.waitForEvent();
sunsmile2015 0:3dc6e424dba0 73 }
sunsmile2015 0:3dc6e424dba0 74 }