Projet IOT EI2I 4 2015/2016
Dependencies: BLE_API BMP180 mbed nRF51822
Fork of BLE_GATT_Example by
main.cpp
00001 #include "mbed.h" 00002 #include "BMP180.h" 00003 #include "ble/BLE.h" 00004 00005 BMP180 baro(D14, D15); 00006 uint8_t* bufTemp; 00007 uint8_t* bufPress; 00008 uint8_t* bufHum; 00009 float tmpTemp=0.0; 00010 float tmpPress=0.0; 00011 float tmpHum = 0.0; 00012 00013 Ticker updateValue; 00014 bool flagCapt = true; 00015 AnalogIn Pot(A0); 00016 00017 uint16_t customServiceUUID = 0xA000; 00018 uint16_t TempCharUUID = 0xA001; 00019 uint16_t PressCharUUID = 0xA002; 00020 uint16_t HumCharUUID = 0xA003; 00021 00022 const static char DEVICE_NAME[] = "Capteur"; // change this 00023 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development 00024 00025 /* Set Up custom Characteristics */ 00026 static uint8_t TempValue[4] = {0}; 00027 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(TempValue)> TempChar(TempCharUUID, TempValue); 00028 00029 static uint8_t PressValue[4] = {0}; 00030 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(PressValue)> PressChar(PressCharUUID, PressValue); 00031 00032 static uint8_t HumValue[4] = {0}; 00033 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(HumValue)> HumChar(HumCharUUID, HumValue); 00034 00035 /* Set up custom service */ 00036 GattCharacteristic *characteristics[] = {&TempChar, &PressChar, &HumChar}; 00037 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00038 00039 /* 00040 * Callback when the redbearlab has to update its values 00041 */ 00042 void callbackUpdate() { 00043 flagCapt = true; 00044 } 00045 00046 /* 00047 * Restart advertising when phone app disconnects 00048 */ 00049 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) 00050 { 00051 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); 00052 } 00053 00054 /* 00055 * Initialization callback 00056 */ 00057 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00058 { 00059 BLE &ble = params->ble; 00060 ble_error_t error = params->error; 00061 00062 if (error != BLE_ERROR_NONE) { 00063 return; 00064 } 00065 00066 ble.gap().onDisconnection(disconnectionCallback); 00067 00068 /* Setup advertising */ 00069 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT 00070 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type 00071 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name 00072 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet 00073 ble.gap().setAdvertisingInterval(100); // 100ms. 00074 00075 /* Add our custom service */ 00076 ble.addService(customService); 00077 00078 /* Start advertising */ 00079 ble.gap().startAdvertising(); 00080 } 00081 00082 /* 00083 * Main loop 00084 */ 00085 int main(void) 00086 { 00087 updateValue.attach_us(&callbackUpdate,100000); 00088 00089 /* initialize stuff */ 00090 printf("\n\r********* Starting Main Loop *********\n\r"); 00091 00092 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00093 ble.init(bleInitComplete); 00094 00095 /* SpinWait for initialization to complete. This is necessary because the 00096 * BLE object is used in the main loop below. */ 00097 while (ble.hasInitialized() == false) { /* spin loop */ } 00098 00099 /* Infinite loop waiting for BLE*/ 00100 while (true) { 00101 /* Check if update needed*/ 00102 if(flagCapt == true){ 00103 flagCapt = false; 00104 00105 /* Get the new values*/ 00106 baro.normalize(); 00107 tmpTemp = baro.read_temperature(); 00108 tmpPress = baro.read_pressure(); 00109 tmpHum = Pot; 00110 00111 /* Convert float into arrays of 4 uint8_t*/ 00112 bufTemp = reinterpret_cast<uint8_t*>(&tmpTemp); 00113 bufPress = reinterpret_cast<uint8_t*>(&tmpPress); 00114 bufHum = reinterpret_cast<uint8_t*>(&tmpHum); 00115 00116 /* Update values into GattCharacteristic*/ 00117 ble.updateCharacteristicValue(TempChar.getValueAttribute().getHandle(), bufTemp, 4); 00118 ble.updateCharacteristicValue(PressChar.getValueAttribute().getHandle(), bufPress, 4); 00119 ble.updateCharacteristicValue(HumChar.getValueAttribute().getHandle(), bufHum, 4); 00120 }else{ 00121 ble.waitForEvent(); /* Save power */ 00122 } 00123 } 00124 }
Generated on Wed Jul 13 2022 01:10:51 by 1.7.2