Red Duo and iBeacon with Particle Cloud


#1

Hi there,

I need some help here with code that I need to create in order to read the temperature and humidity readings from array of ibeacons in a room.
I’m using particle cloud to publish readings for all the ibeacons. ( 5 ibeacons)

sample code

[code]#include
#include

SYSTEM_MODE(SEMI_AUTOMATIC);

static uint16_t conn_handle = 0xFFFF;
uint8_t discSuccess;
std::vector<bd_addr_t> macArray;
static gatt_client_service_t discovered_service;
static gatt_client_characteristic_t discovered_char;
static gatt_client_characteristic_descriptor_t char_descriptor;
static uint16_t service_uuid_16 = 0xFFF0;
static uint16_t characteristic_uuid_16 = 0xFFF3;

static void gattReadCallback(BLEStatus_t status, uint16_t conn_handle, uint16_t value_handle, uint8_t *value, uint16_t length) {
Serial.println(“readValue callback " + String(status));
uint8_t index;
if (status == BLE_STATUS_OK) {
Serial.println(” ");
Serial.println(“Reads characteristic value successfully”);

Serial.print("Characteristic value attribute handle: ");
Serial.println(value_handle, HEX);
    
Serial.print("Characteristic value : ");
for (index = 0; index < length; index++) {
  Serial.print(value[index], HEX);
  Serial.print(" ");
}
Serial.println(" ");

}
else if (status != BLE_STATUS_DONE) {
Serial.println(“Reads characteristic value failed.”);
}
}

static void charsDiscoveredCallback(BLEStatus_t status, uint16_t conn_handle, gatt_client_characteristic_t *characteristic) {
Serial.println("char disc callback " + String(status));
if (status == BLE_STATUS_OK) { // Found a characteristic.
discovered_char = *characteristic;

Serial.println("found a characteristic");
Serial.println(String(characteristic->uuid16));

}
else if (status == BLE_STATUS_DONE) {
Serial.println(“Discovers characteristic completed.”);
Serial.println(String(characteristic->uuid16));

 ble.readValue(conn_handle, &discovered_char);

}
}

static void serviceDiscoveredCallback(BLEStatus_t status, uint16_t conn_handle, gatt_client_service_t *service) {
Serial.println("service disc callback " + String(status));
if (status == BLE_STATUS_OK) { // Found a service.
discovered_service = *service;
Serial.println(“service discovered”);
Serial.println(String(service->uuid16));

}
else if (status == BLE_STATUS_DONE) {
Serial.println(“Discovers service completed”);
ble.discoverCharacteristics(conn_handle, &discovered_service,characteristic_uuid_16);
}
}

static void deviceConnectedCallback(BLEStatus_t status, uint16_t handle) {
Serial.println("conn callback " + String(status));
switch (status) {
case BLE_STATUS_OK:
Serial.print("BLE device connection established! Connection handle: ");
Serial.println(handle, HEX);
conn_handle = handle;
discSuccess = ble.discoverPrimaryServices(conn_handle, service_uuid_16);
Serial.println(String(discSuccess));
break;
default:
Serial.println(“Failed to establish connection with peer device!”);
break;
}
}

void setup() {
Serial.begin(115200);
ble.init();
ble.onConnectedCallback(deviceConnectedCallback);
ble.onServiceDiscoveredCallback(serviceDiscoveredCallback);
ble.onCharacteristicDiscoveredCallback(charsDiscoveredCallback);
ble.onGattCharacteristicReadCallback(gattReadCallback);
}

void loop() {

Serial.printf(“loop”);
bd_addr_type_t addType = BD_ADDR_TYPE_LE_PUBLIC;
bd_addr_t mac;
String macEndString = “F8DE93E1”; // ending of MAC addresses we want to connect to (4 digits by 4 digits)
int nbrMACaddress = macEndString.length()/4;
Serial.println("nbr of MAC addresses " + String(nbrMACaddress));

// bd_addr_t mac2 = {0xA0, 0xE6, 0xF8, 0x69, 0xF8, 0xDE};
// ble.connect(mac2, addType);
// delay(2000);

mac[0] = 0xA0;
mac[1] = 0xE6;
mac[2] = 0xF8;
mac[3] = 0x69;

int s = 0;

for (int i = 0 ; i<nbrMACaddress ; i++){
String macEnd = macEndString.substring(s,s+4);
Serial.println("macEnd is " + String(macEnd));
s += 4;
mac[4] = strtoul(macEnd.substring(0,2), NULL, 16);
mac[5] = strtoul(macEnd.substring(2,4), NULL, 16);

ble.connect(mac, addType);
delay(2000);

}
delay(2000);
}
© 2017 GitHub, Inc.[/code]