![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
BLE iBeacon demonstrating how to change the payload data every second
Dependencies: BLE_API mbed nRF51822
Fork of BLE_iBeacon by
main.cpp@48:6568db49629c, 2014-12-04 (annotated)
- Committer:
- Protoneer
- Date:
- Thu Dec 04 05:47:24 2014 +0000
- Revision:
- 48:6568db49629c
- Parent:
- 47:447eb23e67e2
- Child:
- 49:a3428aa11fc7
Init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:7613d21e5974 | 1 | #include "mbed.h" |
Rohit Grover |
32:7b7093b653a8 | 2 | #include "BLEDevice.h" |
Protoneer | 48:6568db49629c | 3 | #include "nrf_temp.h" |
ktownsend | 0:7613d21e5974 | 4 | |
Rohit Grover |
32:7b7093b653a8 | 5 | BLEDevice ble; |
Rohit Grover |
31:93e50a3c3dc6 | 6 | |
Protoneer | 48:6568db49629c | 7 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
Protoneer | 48:6568db49629c | 8 | * it will have an impact on code-size and power consumption. */ |
ktownsend | 0:7613d21e5974 | 9 | |
Rohit Grover |
31:93e50a3c3dc6 | 10 | #if NEED_CONSOLE_OUTPUT |
Rohit Grover |
31:93e50a3c3dc6 | 11 | Serial pc(USBTX, USBRX); |
Protoneer | 48:6568db49629c | 12 | DigitalOut led1(LED1); |
Rohit Grover |
31:93e50a3c3dc6 | 13 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
Rohit Grover |
31:93e50a3c3dc6 | 14 | #else |
Rohit Grover |
31:93e50a3c3dc6 | 15 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
31:93e50a3c3dc6 | 16 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
ktownsend | 0:7613d21e5974 | 17 | |
Protoneer | 48:6568db49629c | 18 | uint8_t beaconPayload[] = { |
rgrover1 | 43:b5dc3241fc91 | 19 | 0x4C, 0x00, |
rgrover1 | 43:b5dc3241fc91 | 20 | 0x02, |
rgrover1 | 43:b5dc3241fc91 | 21 | 0x15, |
rgrover1 | 44:71ff94cd9c1d | 22 | 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, |
rgrover1 | 44:71ff94cd9c1d | 23 | 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0, |
rgrover1 | 43:b5dc3241fc91 | 24 | 0x00, 0x00, |
rgrover1 | 43:b5dc3241fc91 | 25 | 0x00, 0x00, |
rgrover1 | 47:447eb23e67e2 | 26 | 0xC7 |
Rohit Grover |
10:391c1acf4b9d | 27 | }; |
Rohit Grover |
10:391c1acf4b9d | 28 | |
Protoneer | 48:6568db49629c | 29 | uint8_t counter = 100; |
Protoneer | 48:6568db49629c | 30 | static volatile bool triggerPayloadUpdatePolling = false; |
Protoneer | 48:6568db49629c | 31 | |
Protoneer | 48:6568db49629c | 32 | void periodicCallback(void) |
Protoneer | 48:6568db49629c | 33 | { |
Protoneer | 48:6568db49629c | 34 | led1 = !led1; |
Protoneer | 48:6568db49629c | 35 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
Protoneer | 48:6568db49629c | 36 | * heavy-weight sensor polling from the main thread. */ |
Protoneer | 48:6568db49629c | 37 | triggerPayloadUpdatePolling = true; |
Protoneer | 48:6568db49629c | 38 | } |
Protoneer | 48:6568db49629c | 39 | |
ktownsend | 0:7613d21e5974 | 40 | int main(void) |
ktownsend | 0:7613d21e5974 | 41 | { |
Protoneer | 48:6568db49629c | 42 | |
Protoneer | 48:6568db49629c | 43 | |
Protoneer | 48:6568db49629c | 44 | Ticker ticker; |
Protoneer | 48:6568db49629c | 45 | ticker.attach(periodicCallback, 1); //Sets update time of sensors |
Protoneer | 48:6568db49629c | 46 | |
Rohit Grover |
31:93e50a3c3dc6 | 47 | DEBUG("Initialising BTLE transport\n\r"); |
Protoneer | 48:6568db49629c | 48 | beaconPayload[21] = 0x01; //Minor value 2 |
Protoneer | 48:6568db49629c | 49 | beaconPayload[23] = 0x01; //Minor value 2 |
Rohit Grover |
11:6774f4827024 | 50 | ble.init(); |
Protoneer | 48:6568db49629c | 51 | nrf_temp_init(); |
Protoneer | 48:6568db49629c | 52 | // pc.printf("temperature init"); |
ktownsend | 0:7613d21e5974 | 53 | |
Rohit Grover |
37:205deeded79d | 54 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Rohit Grover |
27:8d4f5bda1191 | 55 | ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload, sizeof(beaconPayload)); |
Rohit Grover |
20:5e84b5b253a5 | 56 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
Protoneer | 48:6568db49629c | 57 | ble.setAdvertisingInterval(16000); /* 1s; in multiples of 0.625ms. */ |
Rohit Grover |
19:869d8c7306b4 | 58 | ble.startAdvertising(); |
ktownsend | 0:7613d21e5974 | 59 | |
Rohit Grover |
21:a61af863b273 | 60 | for (;;) { |
Protoneer | 48:6568db49629c | 61 | |
Protoneer | 48:6568db49629c | 62 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
Protoneer | 48:6568db49629c | 63 | * heavy-weight sensor polling from the main thread. */ |
Protoneer | 48:6568db49629c | 64 | if(triggerPayloadUpdatePolling) { |
Protoneer | 48:6568db49629c | 65 | triggerPayloadUpdatePolling = false; |
Protoneer | 48:6568db49629c | 66 | if(counter<100) { |
Protoneer | 48:6568db49629c | 67 | counter++; |
Protoneer | 48:6568db49629c | 68 | } else { |
Protoneer | 48:6568db49629c | 69 | counter = 0; |
Protoneer | 48:6568db49629c | 70 | } |
Protoneer | 48:6568db49629c | 71 | |
Protoneer | 48:6568db49629c | 72 | |
Protoneer | 48:6568db49629c | 73 | int32_t temperature = 0; |
Protoneer | 48:6568db49629c | 74 | |
Protoneer | 48:6568db49629c | 75 | NRF_TEMP->TASKS_START = 1; /* Start the temperature measurement */ |
Protoneer | 48:6568db49629c | 76 | while ((NRF_TEMP->EVENTS_DATARDY & TEMP_INTENSET_DATARDY_Msk) != (TEMP_INTENSET_DATARDY_Set << TEMP_INTENSET_DATARDY_Pos)) { |
Protoneer | 48:6568db49629c | 77 | } |
Protoneer | 48:6568db49629c | 78 | NRF_TEMP->EVENTS_DATARDY = 0; |
Protoneer | 48:6568db49629c | 79 | |
Protoneer | 48:6568db49629c | 80 | temperature = (uint8_t)(nrf_temp_read()/4); |
Protoneer | 48:6568db49629c | 81 | |
Rohit Grover |
31:93e50a3c3dc6 | 82 | |
Protoneer | 48:6568db49629c | 83 | NRF_TEMP->TASKS_STOP = 1; /* Stop the temperature measurement */ |
Protoneer | 48:6568db49629c | 84 | |
Protoneer | 48:6568db49629c | 85 | |
Protoneer | 48:6568db49629c | 86 | |
Protoneer | 48:6568db49629c | 87 | |
Protoneer | 48:6568db49629c | 88 | |
Protoneer | 48:6568db49629c | 89 | DEBUG("are we here %d \n ",counter); |
Protoneer | 48:6568db49629c | 90 | beaconPayload[21] = temperature; //Major value 2 |
Protoneer | 48:6568db49629c | 91 | beaconPayload[23] = counter; //Minor value 2 |
Protoneer | 48:6568db49629c | 92 | ble.clearAdvertisingPayload(); |
Protoneer | 48:6568db49629c | 93 | |
Protoneer | 48:6568db49629c | 94 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Protoneer | 48:6568db49629c | 95 | ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload, sizeof(beaconPayload)); |
Protoneer | 48:6568db49629c | 96 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
Protoneer | 48:6568db49629c | 97 | ble.setAdvertisingInterval(16000); /* 1s; in multiples of 0.625ms. */ |
Protoneer | 48:6568db49629c | 98 | ble.startAdvertising(); |
Protoneer | 48:6568db49629c | 99 | |
Protoneer | 48:6568db49629c | 100 | } else { |
Protoneer | 48:6568db49629c | 101 | |
Protoneer | 48:6568db49629c | 102 | ble.waitForEvent(); |
Protoneer | 48:6568db49629c | 103 | } |
Protoneer | 48:6568db49629c | 104 | |
Protoneer | 48:6568db49629c | 105 | } |
Protoneer | 48:6568db49629c | 106 | } |