Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822Copy
Fork of BLE_iBeacon by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "BLEDevice.h" 00019 #include "nrf_temp.h" 00020 00021 #define ADVTIME 1000 // advertise time in ms 00022 #define UPDTIME 2000 // update time in ms 00023 00024 BLEDevice ble; 00025 Timer msclock; 00026 DigitalOut activity(LED1); // blinks to show activity 00027 DigitalOut errLED(LED2); // comes on if error occurs 00028 DigitalIn btn1(BUTTON1); 00029 DigitalIn btn2(BUTTON2); 00030 00031 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; 00032 * it will have an impact on code-size and power consumption. */ 00033 00034 #if NEED_CONSOLE_OUTPUT 00035 Serial pc(USBTX, USBRX); 00036 #define DEBUG(...) { pc.printf(__VA_ARGS__); } 00037 #else 00038 #define DEBUG(...) /* nothing */ 00039 #endif /* #if NEED_CONSOLE_OUTPUT */ 00040 00041 // Try a BLE function and if failed, light LED 2 and halt 00042 // but don't catch fire.... 00043 00044 void errTry(ble_error_t fn,int n=1) 00045 { 00046 if (fn) 00047 { 00048 while (1) 00049 { 00050 for (int i=0;i<n*2;i++) 00051 { 00052 wait_ms(250); 00053 errLED=!errLED; 00054 } 00055 wait_ms(2000); 00056 } 00057 } 00058 } 00059 00060 00061 00062 /* 00063 * Reference: 00064 * Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18 00065 */ 00066 00067 /* 00068 * The Beacon payload (encapsulated within the MSD advertising data structure) 00069 * has the following composition: 00070 * 128-Bit UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00071 * Major/Minor = 0000 / 0000 (We steal this for data) 00072 * Tx Power = C8 00073 */ 00074 uint8_t beaconPayload[] = { 00075 0x4C, 0x00, // vendor ID 00076 0x02, // packet type (2) 00077 0x15, // length 00078 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // UUID 00079 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, 00080 0x00, 0x00, // Major 00081 0x00, 0x00, // Minor 00082 0xC8 // TXPower 00083 , 0x99 // one spare byte! 00084 }; 00085 00086 00087 // Read temperature 00088 int temp_read() 00089 { 00090 int t; 00091 NRF_TEMP->TASKS_START=1; 00092 while (NRF_TEMP->EVENTS_DATARDY==0); 00093 NRF_TEMP->EVENTS_DATARDY=0; 00094 t=nrf_temp_read(); 00095 NRF_TEMP->TASKS_STOP=1; 00096 return t; 00097 } 00098 00099 00100 // Grab the sensors and put 55AA in major 00101 // Put minor data for temperature reading 00102 // (which returns 0 on this board) 00103 // as well as switches 00104 void readSensors() 00105 { 00106 int temp; 00107 beaconPayload[20]=0x55; 00108 beaconPayload[21]=0xAA; 00109 temp=temp_read(); // (should read 0-1023) 00110 if (!btn1) temp|=0x8000; // switches are inverted sense (0=pressed) 00111 if (!btn2) temp|=0x4000; 00112 00113 beaconPayload[22]=temp>>8; 00114 beaconPayload[23]=temp&0xFF; 00115 00116 } 00117 00118 // Build up advertising packet including sensors 00119 void setupBLE() 00120 { 00121 ble.clearAdvertisingPayload(); 00122 readSensors(); 00123 errTry(ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE),5); 00124 errTry(ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload, sizeof(beaconPayload)),6); 00125 00126 } 00127 00128 00129 00130 00131 00132 int main(void) 00133 { 00134 char *devname="DDJ"; 00135 DEBUG("Init\n\r"); 00136 activity=errLED=0; 00137 nrf_temp_init(); 00138 errTry(ble.init(),1); 00139 NRF_TEMP->TASKS_START=1; 00140 00141 // Build advert 00142 setupBLE(); 00143 00144 // Set up general parameters 00145 errTry(ble.setDeviceName((uint8_t *)devname),2); 00146 #if 0 00147 errTry(ble.setAdvertisingType(GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED)); // this will give you one "ping" and no repeats 00148 #else 00149 ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); // This will give you a ping every interval 00150 #endif 00151 00152 ble.setAdvertisingInterval((16*ADVTIME)/10); /* 1s; in multiples of 0.625ms. is 1600 */ 00153 00154 // start timer for sensor reading 00155 msclock.start(); 00156 00157 while (true) { 00158 msclock.reset(); 00159 errTry(ble.startAdvertising(),3); // start advert 00160 wait_ms(UPDTIME); // wait awhile 00161 activity=!activity; // blink led 00162 errTry(ble.stopAdvertising(),4); // stop advert 00163 setupBLE(); // set up with new data 00164 } 00165 00166 }
Generated on Thu Jul 14 2022 21:12:32 by
1.7.2
