This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Stable connection BLEnano

Hi everybody! I want to do a connection between two BLEnano, one is the mater and the second the slave.

I have programmed the two BLEnano using the Arduino IDE and libraries; the peripheral send data over an characteristic notification and the central read the characteristics using a callback function. The problem is that sometimes the central don't receive notifications and disconnect from the peripheral. I'm testing the two modules nearest.

I'm trying to send every 10 millisecond 3 uint8_t values and it works but after some time the there is a disconnection.

This is my code:

#include <BLE_API.h>
#include <Wire.h>
#include <ArduinoNunchuk.h>

BLE                       ble;
Ticker                                    ticker;
#define BUF_LEN           20
#define DEVICE_NAME       "RCSSTeam"

// The Nordic UART Service
static const uint8_t service1_uuid[]                = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
static const uint8_t service1_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};

uint8_t rx_value[BUF_LEN] = {0,};
uint8_t le;
String a;
unsigned char buf[BUF_LEN];

GattCharacteristic  NCKdata(service1_rx_uuid, rx_value, 1, BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);

GattCharacteristic *uartChars[] = {&NCKdata};

GattService         uartService(service1_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));


ArduinoNunchuk nunchuk = ArduinoNunchuk();

bool BLEconnection = false;

typedef union dataBLE {
  uint8_t dataI[3];
  unsigned char dataC[sizeof(uint8_t) * 3];
};

dataBLE dataToSend;

void disconnectionCallBack(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
  ble.startAdvertising();
  BLEconnection = false;
  digitalWrite(13, HIGH);
}

void connectionCallBack(const Gap::ConnectionCallbackParams_t *param)
{
  BLEconnection = true;
  digitalWrite(13, LOW);
}

// Task handle
void sendData(void)
{
  if (BLEconnection)
  {
    nunchuk.update();
    dataToSend.dataI[0] = nunchuk.analogY;
    dataToSend.dataI[1] = nunchuk.cButton;
    dataToSend.dataI[2] = nunchuk.zButton;
    ble.updateCharacteristicValue(NCKdata.getValueAttribute().getHandle(), dataToSend.dataC, sizeof(uint8_t) * 3);
  }
}

void setup() {
  //LED
  pinMode(13, OUTPUT);

  ticker.attach(sendData, 0.1);

  // Nunchuck
  nunchuk.init();
  // BLE
  ble.init();
  ble.onDisconnection(disconnectionCallBack);
  ble.onConnection(connectionCallBack);

  // setup adv_data and srp_data
  ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
  ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid_rev));

  ble.accumulateScanResponse(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
  ble.accumulateScanResponse(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid_rev));

  // get a struct with preferences to pass to setPreferredConnectionParams()
  Gap::ConnectionParams_t connParams;

  // welcome to the Gap (get a reference)
  Gap & gap = ble.gap();


  // set the minimum to ... the minimum allowable
  connParams.minConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN;

  // set the maximum we'd prefer such that we can get 3 packets accross in time
  // 1000 ms * 4 samples/packet * 3 packets per interval / (1.25 ms units * Freq)
  // at 300 Hz, this gives 32 units of 1.25ms (so 40ms, or 25 connection events/s)
  connParams.maxConnectionInterval = 32;

  // other stuff... whatevs
  connParams.slaveLatency = BLE_GAP_CP_SLAVE_LATENCY_MAX;
  connParams.connectionSupervisionTimeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MAX / 2;

  // now, actually set your preferences
  gap.setPreferredConnectionParams(&connParams);


  // set adv_type
  ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
  // add service
  ble.addService(uartService);
  // set device name
  ble.setDeviceName((const uint8_t *)DEVICE_NAME);
  // set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4
  ble.setTxPower(4);
  // set adv_interval, 100ms in multiples of 0.625ms.
  ble.setAdvertisingInterval(160);
  // set adv_timeout, in seconds
  ble.setAdvertisingTimeout(0);
  // start advertising
  ble.startAdvertising();
}

void loop() {
  ble.waitForEvent();
}

Any suggestions is appreciated! Thank, U.

Parents Reply Children
No Data
Related