This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).
Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn
The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/
Diff: main.cpp
- Revision:
- 2:b894b3508057
- Parent:
- 1:8950e6a891df
diff -r 8950e6a891df -r b894b3508057 main.cpp
--- a/main.cpp Wed Apr 26 11:20:41 2017 +0900
+++ b/main.cpp Tue Sep 05 11:56:13 2017 +0900
@@ -29,7 +29,7 @@
#include "security.h"
-#include "mbed.h"
+//#include "mbed.h"
#include "rtos.h"
#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
@@ -76,9 +76,9 @@
Ticker status_ticker;
// Ren, begin
enum charType {
- PRESSURE,
- TEMPERATURE,
- HUMIDITY
+ PRESSURE,
+ TEMPERATURE,
+ HUMIDITY
};
const char * dbg_CharType[HUMIDITY+1]={"Pressure","Temperature","Humidity"};
const char * objName[HUMIDITY+1]={"3323","3303","3304"};
@@ -91,11 +91,12 @@
class BME280Resource {
public:
BME280Resource() {
- // create Pressure object '3323'.
+ // create Pressure object '3323'.
for(int m=0; m<HUMIDITY+1; m++) {
+ M2MObjectInstance* tmp_inst;
bme280[m] = M2MInterfaceFactory::create_object(objName[m]);
- tmp_inst[m] = bme280[m] ->create_object_instance();
- tmp_res[m] = tmp_inst[m]->create_dynamic_resource("5700", dbg_CharType[m],
+ tmp_inst = bme280[m] ->create_object_instance();
+ tmp_res[m] = tmp_inst->create_dynamic_resource("5700", dbg_CharType[m],
M2MResourceInstance::STRING, true /* observable */);
tmp_res[m]->set_operation(M2MBase::GET_ALLOWED);
tmp_res[m]->set_value((uint8_t*)"0.0", 3);
@@ -127,12 +128,13 @@
}
private:
M2MObject* bme280[HUMIDITY+1];
- M2MObjectInstance* tmp_inst[HUMIDITY+1];
+// M2MObjectInstance* tmp_inst[HUMIDITY+1];
M2MResource* tmp_res[HUMIDITY+1];
};
static BME280Resource *demo1;
-// Ren, end
+// end of BME280 resource definition
+
/************************************************************BLE Stuff from here *********************************/
BLE &ble = BLE::Instance();
@@ -152,7 +154,7 @@
// byte 1: The key, it is the type of the data
// byte [2..N] The value. N is equal to byte0 - 1
- //printf("Starting advertisementCallback...\r\n");
+ //printf("Starting advertisementCallback...\r\n");
for (uint8_t i = 0; i < params->advertisingDataLen; ++i) {
const uint8_t record_length = params->advertisingData[i];
@@ -217,8 +219,8 @@
printf(" is_active[PRESSURE] = true\r\n");
}
else if (tmp_uuid == GattCharacteristic::UUID_TEMPERATURE_CHAR) {
- bme280Characteristic[TEMPERATURE] = *characteristicP; is_active[TEMPERATURE] = true;
- printf(" is_active[TEMPERATURE] = true\r\n");
+ bme280Characteristic[TEMPERATURE] = *characteristicP; is_active[TEMPERATURE] = true;
+ printf(" is_active[TEMPERATURE] = true\r\n");
} else {
bme280Characteristic[HUMIDITY] = *characteristicP; is_active[HUMIDITY] = true;
printf(" is_active[HUMIDITY] = true\r\n");
@@ -316,7 +318,7 @@
// Ren, clear discovered Characteristic at beginning
for(int i=0; i<HUMIDITY+1; i++) {
- dataprint[i]=0.0; is_active[i]=false;
+ dataprint[i]=0.0; is_active[i]=false;
}
ble.gap().onDisconnection(disconnectionCallback);
@@ -362,6 +364,7 @@
volatile bool clicked = false;
osThreadId mainThread;
+
#ifdef TARGET_K64F
// Set up Hardware interrupt button.
InterruptIn obs_button(SW2);
@@ -574,9 +577,10 @@
BLE_thread.start(BLE_thread_init);
// waiting for completion of BLE_thread_init
Thread::wait(2000);
+
while (true) {
- printf("inside main for client\r\n");
+ printf("inside main for client\r\n");
triggerLedCharacteristic = false;
updates.wait(25000);