Projet IOT EI2I 4 2015/2016

Dependencies:   BLE_API BMP180 mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Committer:
Kemix
Date:
Sun Jan 03 12:38:52 2016 +0000
Revision:
23:8cccf9ededdb
Parent:
22:406127954d1f
End

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:cd5b6733aeb1 1 #include "mbed.h"
Kemix 23:8cccf9ededdb 2 #include "BMP180.h"
andresag 19:477567297aac 3 #include "ble/BLE.h"
mbedAustin 1:94152e7d8b5c 4
Kemix 23:8cccf9ededdb 5 BMP180 baro(D14, D15);
Kemix 23:8cccf9ededdb 6 uint8_t* bufTemp;
Kemix 23:8cccf9ededdb 7 uint8_t* bufPress;
Kemix 23:8cccf9ededdb 8 uint8_t* bufHum;
Kemix 23:8cccf9ededdb 9 float tmpTemp=0.0;
Kemix 23:8cccf9ededdb 10 float tmpPress=0.0;
Kemix 23:8cccf9ededdb 11 float tmpHum = 0.0;
Kemix 23:8cccf9ededdb 12
Kemix 23:8cccf9ededdb 13 Ticker updateValue;
Kemix 23:8cccf9ededdb 14 bool flagCapt = true;
Kemix 23:8cccf9ededdb 15 AnalogIn Pot(A0);
Kemix 23:8cccf9ededdb 16
mbedAustin 9:b33f42191584 17 uint16_t customServiceUUID = 0xA000;
Kemix 23:8cccf9ededdb 18 uint16_t TempCharUUID = 0xA001;
Kemix 23:8cccf9ededdb 19 uint16_t PressCharUUID = 0xA002;
Kemix 23:8cccf9ededdb 20 uint16_t HumCharUUID = 0xA003;
mbedAustin 1:94152e7d8b5c 21
Kemix 23:8cccf9ededdb 22 const static char DEVICE_NAME[] = "Capteur"; // change this
mbedAustin 8:60ede963dfe2 23 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
mbedAustin 1:94152e7d8b5c 24
andresag 22:406127954d1f 25 /* Set Up custom Characteristics */
Kemix 23:8cccf9ededdb 26 static uint8_t TempValue[4] = {0};
Kemix 23:8cccf9ededdb 27 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(TempValue)> TempChar(TempCharUUID, TempValue);
mbedAustin 2:e84c13abc479 28
Kemix 23:8cccf9ededdb 29 static uint8_t PressValue[4] = {0};
Kemix 23:8cccf9ededdb 30 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(PressValue)> PressChar(PressCharUUID, PressValue);
Kemix 23:8cccf9ededdb 31
Kemix 23:8cccf9ededdb 32 static uint8_t HumValue[4] = {0};
Kemix 23:8cccf9ededdb 33 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(HumValue)> HumChar(HumCharUUID, HumValue);
mbedAustin 2:e84c13abc479 34
andresag 22:406127954d1f 35 /* Set up custom service */
Kemix 23:8cccf9ededdb 36 GattCharacteristic *characteristics[] = {&TempChar, &PressChar, &HumChar};
mbedAustin 9:b33f42191584 37 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
mbedAustin 2:e84c13abc479 38
Kemix 23:8cccf9ededdb 39 /*
Kemix 23:8cccf9ededdb 40 * Callback when the redbearlab has to update its values
Kemix 23:8cccf9ededdb 41 */
Kemix 23:8cccf9ededdb 42 void callbackUpdate() {
Kemix 23:8cccf9ededdb 43 flagCapt = true;
Kemix 23:8cccf9ededdb 44 }
mbedAustin 2:e84c13abc479 45
mbedAustin 8:60ede963dfe2 46 /*
mbedAustin 8:60ede963dfe2 47 * Restart advertising when phone app disconnects
andresag 19:477567297aac 48 */
andresag 19:477567297aac 49 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbedAustin 1:94152e7d8b5c 50 {
andresag 22:406127954d1f 51 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
mbedAustin 1:94152e7d8b5c 52 }
mbedAustin 0:cd5b6733aeb1 53
andresag 19:477567297aac 54 /*
andresag 22:406127954d1f 55 * Initialization callback
andresag 22:406127954d1f 56 */
andresag 22:406127954d1f 57 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 22:406127954d1f 58 {
andresag 22:406127954d1f 59 BLE &ble = params->ble;
andresag 22:406127954d1f 60 ble_error_t error = params->error;
andresag 22:406127954d1f 61
andresag 22:406127954d1f 62 if (error != BLE_ERROR_NONE) {
andresag 22:406127954d1f 63 return;
andresag 22:406127954d1f 64 }
mbedAustin 0:cd5b6733aeb1 65
andresag 19:477567297aac 66 ble.gap().onDisconnection(disconnectionCallback);
mbedAustin 2:e84c13abc479 67
andresag 22:406127954d1f 68 /* Setup advertising */
andresag 19:477567297aac 69 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
andresag 19:477567297aac 70 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
andresag 19:477567297aac 71 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
andresag 19:477567297aac 72 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
andresag 19:477567297aac 73 ble.gap().setAdvertisingInterval(100); // 100ms.
mbedAustin 2:e84c13abc479 74
andresag 22:406127954d1f 75 /* Add our custom service */
mbedAustin 13:62b1d32745ac 76 ble.addService(customService);
mbedAustin 2:e84c13abc479 77
andresag 22:406127954d1f 78 /* Start advertising */
andresag 19:477567297aac 79 ble.gap().startAdvertising();
andresag 22:406127954d1f 80 }
andresag 19:477567297aac 81
andresag 22:406127954d1f 82 /*
andresag 22:406127954d1f 83 * Main loop
andresag 22:406127954d1f 84 */
andresag 22:406127954d1f 85 int main(void)
andresag 22:406127954d1f 86 {
Kemix 23:8cccf9ededdb 87 updateValue.attach_us(&callbackUpdate,100000);
Kemix 23:8cccf9ededdb 88
andresag 22:406127954d1f 89 /* initialize stuff */
andresag 22:406127954d1f 90 printf("\n\r********* Starting Main Loop *********\n\r");
andresag 22:406127954d1f 91
andresag 22:406127954d1f 92 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 22:406127954d1f 93 ble.init(bleInitComplete);
andresag 22:406127954d1f 94
andresag 22:406127954d1f 95 /* SpinWait for initialization to complete. This is necessary because the
andresag 22:406127954d1f 96 * BLE object is used in the main loop below. */
andresag 22:406127954d1f 97 while (ble.hasInitialized() == false) { /* spin loop */ }
andresag 22:406127954d1f 98
Kemix 23:8cccf9ededdb 99 /* Infinite loop waiting for BLE*/
mbedAustin 2:e84c13abc479 100 while (true) {
Kemix 23:8cccf9ededdb 101 /* Check if update needed*/
Kemix 23:8cccf9ededdb 102 if(flagCapt == true){
Kemix 23:8cccf9ededdb 103 flagCapt = false;
Kemix 23:8cccf9ededdb 104
Kemix 23:8cccf9ededdb 105 /* Get the new values*/
Kemix 23:8cccf9ededdb 106 baro.normalize();
Kemix 23:8cccf9ededdb 107 tmpTemp = baro.read_temperature();
Kemix 23:8cccf9ededdb 108 tmpPress = baro.read_pressure();
Kemix 23:8cccf9ededdb 109 tmpHum = Pot;
Kemix 23:8cccf9ededdb 110
Kemix 23:8cccf9ededdb 111 /* Convert float into arrays of 4 uint8_t*/
Kemix 23:8cccf9ededdb 112 bufTemp = reinterpret_cast<uint8_t*>(&tmpTemp);
Kemix 23:8cccf9ededdb 113 bufPress = reinterpret_cast<uint8_t*>(&tmpPress);
Kemix 23:8cccf9ededdb 114 bufHum = reinterpret_cast<uint8_t*>(&tmpHum);
Kemix 23:8cccf9ededdb 115
Kemix 23:8cccf9ededdb 116 /* Update values into GattCharacteristic*/
Kemix 23:8cccf9ededdb 117 ble.updateCharacteristicValue(TempChar.getValueAttribute().getHandle(), bufTemp, 4);
Kemix 23:8cccf9ededdb 118 ble.updateCharacteristicValue(PressChar.getValueAttribute().getHandle(), bufPress, 4);
Kemix 23:8cccf9ededdb 119 ble.updateCharacteristicValue(HumChar.getValueAttribute().getHandle(), bufHum, 4);
Kemix 23:8cccf9ededdb 120 }else{
Kemix 23:8cccf9ededdb 121 ble.waitForEvent(); /* Save power */
Kemix 23:8cccf9ededdb 122 }
mbedAustin 2:e84c13abc479 123 }
andresag 20:fcc752d401ec 124 }