This project contains two type of beacons: Eddystone and iBeacon.
Dependencies: microbit-iBeacon
Fork of BTW_ComboBeacon_Solution by
beacon_src.txt@16:2a71c1b5fd71, 2018-07-31 (annotated)
- Committer:
- krenbluetoothsig
- Date:
- Tue Jul 31 13:25:48 2018 +0000
- Revision:
- 16:2a71c1b5fd71
- Child:
- 18:eeea3e01140c
1. NEW: add EULA. ; 2. NEW: add main.cpp to beacon_src.txt.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krenbluetoothsig | 16:2a71c1b5fd71 | 1 | #include "MicroBit.h" |
krenbluetoothsig | 16:2a71c1b5fd71 | 2 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 3 | /****************************************************** |
krenbluetoothsig | 16:2a71c1b5fd71 | 4 | * Constants |
krenbluetoothsig | 16:2a71c1b5fd71 | 5 | ******************************************************/ |
krenbluetoothsig | 16:2a71c1b5fd71 | 6 | #define ADV_ON 1 |
krenbluetoothsig | 16:2a71c1b5fd71 | 7 | #define ADV_OFF 0 |
krenbluetoothsig | 16:2a71c1b5fd71 | 8 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 9 | /****************************************************** |
krenbluetoothsig | 16:2a71c1b5fd71 | 10 | * Variables |
krenbluetoothsig | 16:2a71c1b5fd71 | 11 | ******************************************************/ |
krenbluetoothsig | 16:2a71c1b5fd71 | 12 | MicroBit uBit; |
krenbluetoothsig | 16:2a71c1b5fd71 | 13 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 14 | //Initial USB-to-Serial, baud rate is 9600-8-N-1 |
krenbluetoothsig | 16:2a71c1b5fd71 | 15 | Serial pcCom(USBTX, USBRX); |
krenbluetoothsig | 16:2a71c1b5fd71 | 16 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 17 | // TODO: global data and functions |
krenbluetoothsig | 16:2a71c1b5fd71 | 18 | char URL[] = "https://www.bluetooth.com"; |
krenbluetoothsig | 16:2a71c1b5fd71 | 19 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 20 | // lvl : Pwr@ 1m : Pwr@ 0m |
krenbluetoothsig | 16:2a71c1b5fd71 | 21 | // 0 : -90 : -49 |
krenbluetoothsig | 16:2a71c1b5fd71 | 22 | // 1 : -78 : -37 |
krenbluetoothsig | 16:2a71c1b5fd71 | 23 | // 2 : -74 : -33 |
krenbluetoothsig | 16:2a71c1b5fd71 | 24 | // 3 : -69 : -28 |
krenbluetoothsig | 16:2a71c1b5fd71 | 25 | // 4 : -66 : -25 |
krenbluetoothsig | 16:2a71c1b5fd71 | 26 | // 5 : -61 : -20 |
krenbluetoothsig | 16:2a71c1b5fd71 | 27 | // 6 : -56 : -15 |
krenbluetoothsig | 16:2a71c1b5fd71 | 28 | // 7 : -51 : -10 |
krenbluetoothsig | 16:2a71c1b5fd71 | 29 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 30 | const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; |
krenbluetoothsig | 16:2a71c1b5fd71 | 31 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 32 | uint8_t advertising = 0; |
krenbluetoothsig | 16:2a71c1b5fd71 | 33 | uint8_t tx_power_level = 6; |
krenbluetoothsig | 16:2a71c1b5fd71 | 34 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 35 | BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE |
krenbluetoothsig | 16:2a71c1b5fd71 | 36 | // proximity UUID MSB first |
krenbluetoothsig | 16:2a71c1b5fd71 | 37 | const uint8_t iBeaconProximityUUIDraw[] = { |
krenbluetoothsig | 16:2a71c1b5fd71 | 38 | 0x01, 0xD9, 0xC8, 0x00, 0x45, 0xF3, 0x40, 0x24, 0x93, 0xB0, 0xB0, 0xCD, 0x4A, 0x50, 0x2A, 0xF7 |
krenbluetoothsig | 16:2a71c1b5fd71 | 39 | }; // 01D9C800-45F3-4024-93B0-B0CD4A502AF7 use this UUID including dashes in mobile application |
krenbluetoothsig | 16:2a71c1b5fd71 | 40 | const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw); |
krenbluetoothsig | 16:2a71c1b5fd71 | 41 | const int16_t iBeaconMajor = 1234; |
krenbluetoothsig | 16:2a71c1b5fd71 | 42 | const int16_t iBeaconMinor = 5678; |
krenbluetoothsig | 16:2a71c1b5fd71 | 43 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 44 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 45 | /****************************************************** |
krenbluetoothsig | 16:2a71c1b5fd71 | 46 | * Functions |
krenbluetoothsig | 16:2a71c1b5fd71 | 47 | ******************************************************/ |
krenbluetoothsig | 16:2a71c1b5fd71 | 48 | void startAdvertising() { |
krenbluetoothsig | 16:2a71c1b5fd71 | 49 | switch(beacon_type) |
krenbluetoothsig | 16:2a71c1b5fd71 | 50 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 51 | case BEACON_TYPE_EDDYSTONE: |
krenbluetoothsig | 16:2a71c1b5fd71 | 52 | uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false); |
krenbluetoothsig | 16:2a71c1b5fd71 | 53 | uBit.display.print("E"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 54 | pcCom.printf("eddystone\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 55 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 56 | case BEACON_TYPE_IBEACON: |
krenbluetoothsig | 16:2a71c1b5fd71 | 57 | uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[0]); |
krenbluetoothsig | 16:2a71c1b5fd71 | 58 | uBit.display.print("i"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 59 | pcCom.printf("iBeacon\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 60 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 61 | case BEACON_TYPE_NONE: |
krenbluetoothsig | 16:2a71c1b5fd71 | 62 | default: |
krenbluetoothsig | 16:2a71c1b5fd71 | 63 | pcCom.printf("unknow\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 64 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 65 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 66 | uBit.bleManager.setTransmitPower(tx_power_level); |
krenbluetoothsig | 16:2a71c1b5fd71 | 67 | advertising = 1; |
krenbluetoothsig | 16:2a71c1b5fd71 | 68 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 69 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 70 | void stopAdvertising() { |
krenbluetoothsig | 16:2a71c1b5fd71 | 71 | pcCom.printf("ADV OFF\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 72 | uBit.bleManager.stopAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 73 | advertising = 0; |
krenbluetoothsig | 16:2a71c1b5fd71 | 74 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 75 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 76 | void onButtonA(MicroBitEvent) |
krenbluetoothsig | 16:2a71c1b5fd71 | 77 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 78 | // toggle advertising |
krenbluetoothsig | 16:2a71c1b5fd71 | 79 | switch(advertising) |
krenbluetoothsig | 16:2a71c1b5fd71 | 80 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 81 | case 0: |
krenbluetoothsig | 16:2a71c1b5fd71 | 82 | pcCom.printf("ADV ON "); |
krenbluetoothsig | 16:2a71c1b5fd71 | 83 | uBit.display.scroll("ON"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 84 | startAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 85 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 86 | case 1: |
krenbluetoothsig | 16:2a71c1b5fd71 | 87 | pcCom.printf("ADV OFF"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 88 | stopAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 89 | uBit.display.scroll("OFF"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 90 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 91 | default: |
krenbluetoothsig | 16:2a71c1b5fd71 | 92 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 93 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 94 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 95 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 96 | // ButtonB is used to toggle beacon types, switch beacons types |
krenbluetoothsig | 16:2a71c1b5fd71 | 97 | // between iBeacon and Eddystone. |
krenbluetoothsig | 16:2a71c1b5fd71 | 98 | void onButtonB(MicroBitEvent) |
krenbluetoothsig | 16:2a71c1b5fd71 | 99 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 100 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 101 | stopAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 102 | switch(beacon_type) |
krenbluetoothsig | 16:2a71c1b5fd71 | 103 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 104 | case BEACON_TYPE_EDDYSTONE: |
krenbluetoothsig | 16:2a71c1b5fd71 | 105 | beacon_type = BEACON_TYPE_IBEACON; |
krenbluetoothsig | 16:2a71c1b5fd71 | 106 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 107 | case BEACON_TYPE_IBEACON: |
krenbluetoothsig | 16:2a71c1b5fd71 | 108 | beacon_type = BEACON_TYPE_EDDYSTONE; |
krenbluetoothsig | 16:2a71c1b5fd71 | 109 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 110 | case BEACON_TYPE_NONE: |
krenbluetoothsig | 16:2a71c1b5fd71 | 111 | default: |
krenbluetoothsig | 16:2a71c1b5fd71 | 112 | beacon_type = BEACON_TYPE_EDDYSTONE; |
krenbluetoothsig | 16:2a71c1b5fd71 | 113 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 114 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 115 | startAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 116 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 117 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 118 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 119 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 120 | int main() |
krenbluetoothsig | 16:2a71c1b5fd71 | 121 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 122 | // Serial output after a reset |
krenbluetoothsig | 16:2a71c1b5fd71 | 123 | pcCom.printf("mBit Beacon.\n\r"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 124 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 125 | // TODO: main processing code |
krenbluetoothsig | 16:2a71c1b5fd71 | 126 | // Initialize the micro:bit runtime. |
krenbluetoothsig | 16:2a71c1b5fd71 | 127 | uBit.init(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 128 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 129 | // Register the interrupt and callback for ButtonA and ButtonB |
krenbluetoothsig | 16:2a71c1b5fd71 | 130 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); |
krenbluetoothsig | 16:2a71c1b5fd71 | 131 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); |
krenbluetoothsig | 16:2a71c1b5fd71 | 132 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 133 | //First Advertisment, adv name only |
krenbluetoothsig | 16:2a71c1b5fd71 | 134 | uBit.bleManager.advertise(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 135 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 136 | release_fiber(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 137 | } |