This is a simple program to work with RedBearLab BLE Controller App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal.

Dependencies:   BLE_API BME280 DHT mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Revision:
4:86bf080e3453
Parent:
3:b3f6c612b603
--- a/main.cpp	Thu Jan 07 02:46:54 2016 +0000
+++ b/main.cpp	Mon Jan 29 10:58:48 2018 +0000
@@ -1,159 +1,138 @@
-/*
-
-Copyright (c) 2012-2014 RedBearLab
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
-and associated documentation files (the "Software"), to deal in the Software without restriction, 
-including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
-and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
-subject to the following conditions:
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
-FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
-ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-*/
-
-/*
- *    The application works with the BLEController iOS/Android App.
- *    Type something from the Terminal to send
- *    to the BLEController App or vice verse.
- *    Characteristics received from App will print on Terminal.
- */
- 
 #include "mbed.h"
 #include "ble/BLE.h"
-
-
-#define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
-#define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
-#define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
-
-#define TXRX_BUF_LEN                     20
+#include "BME280.h"
 
 BLE  ble;
 
-Serial pc(USBTX, USBRX);
+// Serial pc(USBTX, USBRX);
+
+BME280 bme(P0_3, P0_4);
+
+const static uint16_t environment_ser_uuid = 0xA000;
 
+const static uint16_t temperature_chr_uuid = 0xA001;
+uint8_t             tempPayload[3] = { 0, 0, 0 };
+GattCharacteristic temp_chr   ( temperature_chr_uuid, tempPayload, sizeof(tempPayload), sizeof(tempPayload),
+                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
+                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 
-// The Nordic UART Service
-static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
-static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
-static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
-static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
+const static uint16_t humidity_chr_uuid = 0xA002;
+uint8_t             humidityPayload[3] = { 0, 0, 0 };
+GattCharacteristic humidity_chr   ( humidity_chr_uuid, humidityPayload, sizeof(humidityPayload), sizeof(humidityPayload),
+                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
+                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
+                                   
+const static uint16_t pressure_chr_uuid = 0xA003;
+uint8_t             pressurePayload[3] = { 0, 0, 0 };
+GattCharacteristic pressure_chr   ( pressure_chr_uuid, pressurePayload, sizeof(pressurePayload), sizeof(pressurePayload),
+                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
+                                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 
 
-uint8_t txPayload[TXRX_BUF_LEN] = {0,};
-uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
-
-static uint8_t rx_buf[TXRX_BUF_LEN];
-static uint8_t rx_len=0;
-
-
-GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
-                                      
-GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
-                                      
-GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
-
-GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
+GattCharacteristic *envChars[] = { &temp_chr, &humidity_chr, &pressure_chr };
+GattService        envService(environment_ser_uuid, envChars, sizeof(envChars) / sizeof(GattCharacteristic *));
 
 
 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    pc.printf("Disconnected \r\n");
-    pc.printf("Restart advertising \r\n");
-    ble.startAdvertising();
-}
-
-void WrittenHandler(const GattWriteCallbackParams *Handler)
-{   
-    uint8_t buf[TXRX_BUF_LEN];
-    uint16_t bytesRead, index;
-    
-    if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
-    {
-        ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
-        memset(txPayload, 0, TXRX_BUF_LEN);
-        memcpy(txPayload, buf, TXRX_BUF_LEN);       
-        pc.printf("WriteHandler \r\n");
-        pc.printf("Length: ");
-        pc.putc(bytesRead);
-        pc.printf("\r\n");
-        pc.printf("Data: ");
-        for(index=0; index<bytesRead; index++)
-        {
-            pc.putc(txPayload[index]);        
-        }
-        pc.printf("\r\n");
-    }
-}
+const static uint16_t count_ser_uuid = 0xB000;
+const static uint16_t count_chr_uuid = 0xB001;
+uint8_t             counter[1] = { 0 };
+GattCharacteristic count_chr   ( count_chr_uuid, counter, sizeof(counter), sizeof(counter),
+                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
+                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
+GattCharacteristic *count_chrs[] = {&count_chr, };
+GattService        countService(count_ser_uuid, count_chrs, sizeof(count_chrs) / sizeof(GattCharacteristic *));
 
-void uartCB(void)
-{   
-    while(pc.readable())    
-    {
-        rx_buf[rx_len++] = pc.getc();    
-        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
-        {
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
-            pc.putc(rx_len);
-            pc.printf("\r\n");
-            rx_len = 0;
-            break;
-        }
-    }
-}
+
 
-int main(void)
-{
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    ble.onDataWritten(WrittenHandler);  
-    
-    pc.baud(9600);
-    pc.printf("SimpleChat Init \r\n");
-    
-    pc.attach( uartCB , pc.RxIrq);
-   // setup advertising 
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                    (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
-    // 100ms; in multiples of 0.625ms. 
-    ble.setAdvertisingInterval(160);
-
-    ble.addService(uartService);
-    
-    ble.startAdvertising(); 
-    pc.printf("Advertising Start \r\n");
-    
-    while(1)
-    {
-        ble.waitForEvent(); 
-    }
-}
+static const uint16_t uuid16_list[] = {environment_ser_uuid, count_ser_uuid};
 
 
 
 
 
 
-
+static volatile bool triggerSensorPolling = false;
 
-
+void periodicCallback(void);
+void update_values(void);
 
 
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    // pc.printf("Disconnected \r\n");
+    // pc.printf("Restart advertising \r\n");
+    ble.startAdvertising();
+}
+void tare(void);
+int main(void)
+{
+    Ticker ticker;
+    ticker.attach(periodicCallback, 2.0 );
 
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+    // pc.printf("hello\r\n");
+    // pc.printf("hi\r\n");
+    // pc.baud(9600);
+    // pc.printf("SimpleChat Init \r\n");
+
+    // setup advertising
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                     (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+
+    // 100ms; in multiples of 0.625ms.
+    ble.setAdvertisingInterval(160);
+
+    ble.addService(countService);
+    ble.addService(envService);
+
+    ble.startAdvertising();
+    // pc.printf("Advertising Start \r\n");
+
+    while(1) {
+        if (triggerSensorPolling) {
+            triggerSensorPolling = false;
+            update_values();
+        } else {
+            ble.waitForEvent();
+        }
+    }
+}
+
+void periodicCallback(void)
+{
+    triggerSensorPolling = true;
+}
 
 
+void update_values(void) {    
+    float temp = bme.getTemperature();
+    float humidity = bme.getHumidity();
+    float pressure = bme.getPressure();
+    
+    if (ble.getGapState().connected ) {
+        uint32_t temp_ = temp * 10000;
+        tempPayload[0] = (uint8_t)((temp_ & 0xFF0000) >> 16);
+        tempPayload[1] = (uint8_t)((temp_ & 0x00FF00) >> 8);
+        tempPayload[2] = (uint8_t)(temp_ & 0x0000FF);
+        ble.updateCharacteristicValue(temp_chr.getValueAttribute().getHandle(), tempPayload, sizeof(tempPayload));
+        
+        uint32_t humidity_ = humidity * 10000;
+        humidityPayload[0] = (uint8_t)((humidity_ & 0xFF0000) >> 16);
+        humidityPayload[1] = (uint8_t)((humidity_ & 0x00FF00) >> 8);
+        humidityPayload[2] = (uint8_t)(humidity_ & 0x0000FF);
+        ble.updateCharacteristicValue(humidity_chr.getValueAttribute().getHandle(), humidityPayload, sizeof(humidityPayload));
+        
+        uint32_t pressure_ = pressure * 10000;
+        pressurePayload[0] = (uint8_t)((pressure_ & 0xFF0000) >> 16);
+        pressurePayload[1] = (uint8_t)((pressure_ & 0x00FF00) >> 8);
+        pressurePayload[2] = (uint8_t)(pressure_ & 0x0000FF);
+        ble.updateCharacteristicValue(pressure_chr.getValueAttribute().getHandle(), pressurePayload, sizeof(pressurePayload));
+    }
+    
+}
 
-