Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Sun Jul 16 10:04:48 2017 +0000
Revision:
1:5f34885f5cff
Parent:
0:f8c1e0b2d473
Child:
2:5504b714c9ae
Commit before bigger change (dinamic change sleep period)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 1:5f34885f5cff 1 /*
jurica238814 0:f8c1e0b2d473 2 *
jurica238814 1:5f34885f5cff 3 * Made by Jurica Resetar @ aconno
jurica238814 1:5f34885f5cff 4 * aconno.de
jurica238814 1:5f34885f5cff 5 * All rights reserved
jurica238814 0:f8c1e0b2d473 6 *
jurica238814 0:f8c1e0b2d473 7 */
jurica238814 0:f8c1e0b2d473 8
jurica238814 0:f8c1e0b2d473 9 #include "mbed.h"
jurica238814 0:f8c1e0b2d473 10 #include "ble/BLE.h"
jurica238814 1:5f34885f5cff 11 #include "GapAdvertisingData.h"
jurica238814 0:f8c1e0b2d473 12 #include "acd52832_bsp.h"
jurica238814 1:5f34885f5cff 13
jurica238814 1:5f34885f5cff 14 #define MSD_SIZE (18)
jurica238814 1:5f34885f5cff 15 #define SLEEP_TIME 0.5 // Sleep time in seconds
jurica238814 1:5f34885f5cff 16 #define WAKE_UP_TIME 100 // Awake time in ms
jurica238814 1:5f34885f5cff 17 #define BUZZER (p31)
jurica238814 0:f8c1e0b2d473 18
jurica238814 1:5f34885f5cff 19 /* Static constants for the BLE example */
jurica238814 1:5f34885f5cff 20 #define MANUFACTURER_SPECIFIC_DATA_ID (0xFF)
jurica238814 1:5f34885f5cff 21 #define MSD_OFFSET (4)
jurica238814 1:5f34885f5cff 22 #define BUZZ_TIME (200) // Buzz time in ms
jurica238814 1:5f34885f5cff 23
jurica238814 0:f8c1e0b2d473 24
jurica238814 0:f8c1e0b2d473 25 bool SLEEP = true;
jurica238814 0:f8c1e0b2d473 26 int8_t txPower = 4;
jurica238814 1:5f34885f5cff 27 uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
jurica238814 1:5f34885f5cff 28 uint8_t mac_address[6] = {0xCF, 0x36, 0x23, 0x9A, 0x55, 0x96};
jurica238814 1:5f34885f5cff 29 uint8_t my_mac_address[6] = {};
jurica238814 1:5f34885f5cff 30 uint8_t LOOKING = 0;
jurica238814 0:f8c1e0b2d473 31
jurica238814 1:5f34885f5cff 32 void turnMeOff(void);
jurica238814 0:f8c1e0b2d473 33
jurica238814 1:5f34885f5cff 34 PwmOut buzzer(BUZZER);
jurica238814 1:5f34885f5cff 35 BLE &ble = BLE::Instance();
jurica238814 1:5f34885f5cff 36 Ticker ticker;
jurica238814 1:5f34885f5cff 37 Ticker ticker2;
jurica238814 1:5f34885f5cff 38
jurica238814 0:f8c1e0b2d473 39 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 40 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 0:f8c1e0b2d473 41 BLE::Instance().gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 42 }
jurica238814 0:f8c1e0b2d473 43
jurica238814 0:f8c1e0b2d473 44 void wakeMeUp(void){
jurica238814 1:5f34885f5cff 45 //ticker.detach();
jurica238814 1:5f34885f5cff 46 //ticker2.attach(turnMeOff, WAKE_UP_TIME);
jurica238814 1:5f34885f5cff 47 //LED = !LED;
jurica238814 1:5f34885f5cff 48 //SLEEP = !SLEEP;
jurica238814 1:5f34885f5cff 49 if(SLEEP) SLEEP = false;
jurica238814 1:5f34885f5cff 50 else SLEEP = true;
jurica238814 1:5f34885f5cff 51 }
jurica238814 1:5f34885f5cff 52
jurica238814 1:5f34885f5cff 53 void turnMeOff(void){
jurica238814 1:5f34885f5cff 54 //ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up
jurica238814 1:5f34885f5cff 55 //ticker2.detach();
jurica238814 1:5f34885f5cff 56 SLEEP = true;
jurica238814 1:5f34885f5cff 57 }
jurica238814 0:f8c1e0b2d473 58
jurica238814 0:f8c1e0b2d473 59 /**
jurica238814 0:f8c1e0b2d473 60 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 61 */
jurica238814 0:f8c1e0b2d473 62 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 63 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 64 (void) ble;
jurica238814 0:f8c1e0b2d473 65 (void) error;
jurica238814 0:f8c1e0b2d473 66 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 67 }
jurica238814 0:f8c1e0b2d473 68
jurica238814 0:f8c1e0b2d473 69 /**
jurica238814 0:f8c1e0b2d473 70 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 71 */
jurica238814 0:f8c1e0b2d473 72 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 73 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 74 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 75
jurica238814 0:f8c1e0b2d473 76 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 77 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 78 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 79 return;
jurica238814 0:f8c1e0b2d473 80 }
jurica238814 0:f8c1e0b2d473 81
jurica238814 0:f8c1e0b2d473 82 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 83 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 84 return;
jurica238814 0:f8c1e0b2d473 85 }
jurica238814 0:f8c1e0b2d473 86
jurica238814 0:f8c1e0b2d473 87 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 0:f8c1e0b2d473 88
jurica238814 1:5f34885f5cff 89 /* Get my MAC address */
jurica238814 1:5f34885f5cff 90 BLEProtocol::AddressType_t temp_address_type;
jurica238814 1:5f34885f5cff 91 ble.gap().getAddress(&temp_address_type, my_mac_address);
jurica238814 1:5f34885f5cff 92
jurica238814 1:5f34885f5cff 93
jurica238814 0:f8c1e0b2d473 94 /* setup advertising */
jurica238814 0:f8c1e0b2d473 95 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 1:5f34885f5cff 96 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
jurica238814 0:f8c1e0b2d473 97 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jurica238814 0:f8c1e0b2d473 98 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
jurica238814 0:f8c1e0b2d473 99 ble.gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 100 }
jurica238814 0:f8c1e0b2d473 101
jurica238814 1:5f34885f5cff 102 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 1:5f34885f5cff 103 uint8_t i=0;
jurica238814 1:5f34885f5cff 104 if ((params->advertisingData[MSD_OFFSET]) == MANUFACTURER_SPECIFIC_DATA_ID ){ /* !ALERT! Update this filter to suit your device. */
jurica238814 1:5f34885f5cff 105 // Follows Manufacturer Specific Data
jurica238814 1:5f34885f5cff 106 if ((params->advertisingData[MSD_OFFSET+1]) == 0x59){
jurica238814 1:5f34885f5cff 107 if ((params->advertisingData[MSD_OFFSET+2]) == 0x00){
jurica238814 1:5f34885f5cff 108 for(i=0; i<6; i++){
jurica238814 1:5f34885f5cff 109 if((params->advertisingData[MSD_OFFSET+i+3]) == my_mac_address[5-i]){
jurica238814 1:5f34885f5cff 110 continue;
jurica238814 1:5f34885f5cff 111 }
jurica238814 1:5f34885f5cff 112 else{
jurica238814 1:5f34885f5cff 113 return;
jurica238814 1:5f34885f5cff 114 }
jurica238814 1:5f34885f5cff 115 }
jurica238814 1:5f34885f5cff 116 buzzer.write(0.8F);
jurica238814 1:5f34885f5cff 117 wait_ms(200);
jurica238814 1:5f34885f5cff 118 buzzer.write(0.0F);
jurica238814 1:5f34885f5cff 119 }
jurica238814 1:5f34885f5cff 120 }
jurica238814 1:5f34885f5cff 121 }
jurica238814 1:5f34885f5cff 122 }
jurica238814 1:5f34885f5cff 123
jurica238814 0:f8c1e0b2d473 124 int main(void){
jurica238814 1:5f34885f5cff 125 //ticker2.attach(turnMeOff, WAKE_UP_TIME);
jurica238814 1:5f34885f5cff 126
jurica238814 0:f8c1e0b2d473 127 ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up
jurica238814 0:f8c1e0b2d473 128
jurica238814 0:f8c1e0b2d473 129 ble.init(bleInitComplete);
jurica238814 0:f8c1e0b2d473 130 ble.gap().setTxPower(txPower);
jurica238814 0:f8c1e0b2d473 131 GapAdvertisingData postavke = GapAdvertisingData();
jurica238814 0:f8c1e0b2d473 132
jurica238814 1:5f34885f5cff 133 ble.gap().setScanParams(500, 400);
jurica238814 1:5f34885f5cff 134 ble.gap().startScan(advertisementCallback);
jurica238814 1:5f34885f5cff 135
jurica238814 1:5f34885f5cff 136 buzzer.period(0.001F);
jurica238814 1:5f34885f5cff 137 buzzer.write(0.0F);
jurica238814 0:f8c1e0b2d473 138
jurica238814 0:f8c1e0b2d473 139 /* SpinWait for initialization to complete. This is necessary because the
jurica238814 0:f8c1e0b2d473 140 * BLE object is used in the main loop below. */
jurica238814 0:f8c1e0b2d473 141 while (ble.hasInitialized() == false) { /* spin loop */ }
jurica238814 1:5f34885f5cff 142
jurica238814 1:5f34885f5cff 143 while(true){
jurica238814 0:f8c1e0b2d473 144 if (SLEEP){
jurica238814 0:f8c1e0b2d473 145 ble.gap().stopAdvertising();
jurica238814 1:5f34885f5cff 146 //ble.waitForEvent();
jurica238814 1:5f34885f5cff 147 //__WFI();
jurica238814 0:f8c1e0b2d473 148 }
jurica238814 0:f8c1e0b2d473 149 else{
jurica238814 0:f8c1e0b2d473 150 ble.gap().startAdvertising();
jurica238814 1:5f34885f5cff 151 ble.waitForEvent();
jurica238814 0:f8c1e0b2d473 152 }
jurica238814 1:5f34885f5cff 153
jurica238814 0:f8c1e0b2d473 154 }
jurica238814 0:f8c1e0b2d473 155 }