Projet IOT EI2I 4 2015/2016

Dependencies:   BLE_API BMP180 mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Revision:
23:8cccf9ededdb
Parent:
22:406127954d1f
--- a/main.cpp	Mon Nov 09 17:08:47 2015 +0000
+++ b/main.cpp	Sun Jan 03 12:38:52 2016 +0000
@@ -1,25 +1,47 @@
 #include "mbed.h"
+#include "BMP180.h"
 #include "ble/BLE.h"
 
-DigitalOut led(LED1, 1);
+BMP180 baro(D14, D15);
+uint8_t* bufTemp;
+uint8_t* bufPress;
+uint8_t* bufHum;
+float tmpTemp=0.0;
+float tmpPress=0.0;
+float tmpHum = 0.0;
+
+Ticker updateValue;
+bool flagCapt = true;
+AnalogIn Pot(A0);
+
 uint16_t customServiceUUID  = 0xA000;
-uint16_t readCharUUID       = 0xA001;
-uint16_t writeCharUUID      = 0xA002;
+uint16_t TempCharUUID       = 0xA001;
+uint16_t PressCharUUID      = 0xA002;
+uint16_t HumCharUUID        = 0xA003;
 
-const static char     DEVICE_NAME[]        = "ChangeMe!!"; // change this
+const static char     DEVICE_NAME[]        = "Capteur"; // change this
 static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development
 
 /* Set Up custom Characteristics */
-static uint8_t readValue[10] = {0};
-ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
+static uint8_t TempValue[4] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(TempValue)> TempChar(TempCharUUID, TempValue);
 
-static uint8_t writeValue[10] = {0};
-WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
+static uint8_t PressValue[4] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(PressValue)> PressChar(PressCharUUID, PressValue);
+
+static uint8_t HumValue[4] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(HumValue)> HumChar(HumCharUUID, HumValue);
 
 /* Set up custom service */
-GattCharacteristic *characteristics[] = {&readChar, &writeChar};
+GattCharacteristic *characteristics[] = {&TempChar, &PressChar, &HumChar};
 GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
 
+/*
+ *  Callback when the redbearlab has to update its values
+*/
+void callbackUpdate() {
+    flagCapt = true;
+}
 
 /*
  *  Restart advertising when phone app disconnects
@@ -30,30 +52,6 @@
 }
 
 /*
- *  Handle writes to writeCharacteristic
-*/
-void writeCharCallback(const GattWriteCallbackParams *params)
-{
-    /* Check to see what characteristic was written, by handle */
-    if(params->handle == writeChar.getValueHandle()) {
-        /* toggle LED if only 1 byte is written */
-        if(params->len == 1) {
-            led = params->data[0];
-            (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle
-        }
-        /* Print the data if more than 1 byte is written */
-        else {
-            printf("Data received: length = %d, data = 0x",params->len);
-            for(int x=0; x < params->len; x++) {
-                printf("%x", params->data[x]);
-            }
-            printf("\n\r");
-        }
-        /* Update the readChar with the value of writeChar */
-        BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
-    }
-}
-/*
  * Initialization callback
  */
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
@@ -66,7 +64,6 @@
     }
 
     ble.gap().onDisconnection(disconnectionCallback);
-    ble.gattServer().onDataWritten(writeCharCallback);
 
     /* Setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
@@ -87,6 +84,8 @@
 */
 int main(void)
 {
+    updateValue.attach_us(&callbackUpdate,100000);
+    
     /* initialize stuff */
     printf("\n\r********* Starting Main Loop *********\n\r");
     
@@ -97,8 +96,29 @@
      * BLE object is used in the main loop below. */
     while (ble.hasInitialized()  == false) { /* spin loop */ }
 
-    /* Infinite loop waiting for BLE interrupt events */
+    /* Infinite loop waiting for BLE*/
     while (true) {
-        ble.waitForEvent(); /* Save power */
+        /* Check if update needed*/
+        if(flagCapt == true){
+            flagCapt = false;
+            
+            /* Get the new values*/
+            baro.normalize();
+            tmpTemp = baro.read_temperature();
+            tmpPress = baro.read_pressure();
+            tmpHum = Pot;
+            
+            /* Convert float into arrays of 4 uint8_t*/
+            bufTemp = reinterpret_cast<uint8_t*>(&tmpTemp);
+            bufPress = reinterpret_cast<uint8_t*>(&tmpPress);
+            bufHum = reinterpret_cast<uint8_t*>(&tmpHum);
+            
+            /* Update values into GattCharacteristic*/
+            ble.updateCharacteristicValue(TempChar.getValueAttribute().getHandle(), bufTemp, 4);
+            ble.updateCharacteristicValue(PressChar.getValueAttribute().getHandle(), bufPress, 4);
+            ble.updateCharacteristicValue(HumChar.getValueAttribute().getHandle(), bufHum, 4);
+        }else{    
+            ble.waitForEvent(); /* Save power */
+        }
     }
 }
\ No newline at end of file