Go Down

Topic: connect 2 arduinos 101 with BLE (Read 504 times) previous topic - next topic

drkmr92

Mar 14, 2018, 04:21 am Last Edit: Mar 14, 2018, 04:30 am by drkmr92
Hi, i'm triying to make a simple program to communicate 2 arduinos as subject says, i already try LedControl Central example in the first arduino and CallbackLed peripheral example in the second, and it works, but i want something a little more complex for my application, i want to transmit and receive in continuous mode, so i write 2 sketch for central and peripheral role respectively based on the given examples, for peripheral role there's no problem, i tested with nrf connect for android and it works perfect, the problem is the central role im stuck with the transmit mode, and i have no idea how to receive data, here's what i have so far:

Code: [Select]
#include <CurieBLE.h>
#include <CurieTimerOne.h>

int time = 100000;
// variables for button
//#define buttonPin 4
#define buff_len 20

int oldButtonState = LOW;
uint8_t cont=0, buff[buff_len];

bool Cflag=false, Rflag=false;

BLEDevice peripheral;// create peripheral device

//BLECharacteristic ledCharacteristic("0000FFE2-0000-1000-8000-00805F9B34FB", BLEWrite, buff_len);
BLECharacteristic ledCharacteristic;// = peripheral.characteristic("0000FFE2-0000-1000-8000-00805F9B34FB");

void setup() {
  Serial.begin(115200);
  while(!Serial);

  // configure the button pin as input
  pinMode(buttonPin, INPUT);

  // initialize the BLE hardware
  BLE.begin();
  CurieTimerOne.start(time, &Timer_ISR);

  Serial.println("BLE Central - LED control");

  // start scanning for peripherals
  BLE.scanForUuid("0000FFE0-0000-1000-8000-00805F9B34FB");
  
 
}

void loop() {
 //BLECharacteristic ledCharacteristic;
  // check if a peripheral has been discovered
  peripheral.poll();
  if(Rflag){
  ledCharacteristic.writeByte(buff[0]);
  Serial.write(buff,buff_len);
  Rflag=false;
  }
  if(!Cflag){
 peripheral = BLE.available();

 if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
 Serial.print("Found ");
 Serial.print(peripheral.address());
 Serial.print(" '");
 Serial.print(peripheral.localName());
 Serial.print("' ");
 Serial.print(peripheral.advertisedServiceUuid());
 Serial.println();

    // stop scanning
 BLE.stopScan();
    
 Serial.println("Connecting ...");

 if (peripheral.connect()) {
 Serial.println("Connected");
 } else {
 Serial.println("Failed to connect!");
 return;
 }
 
 //ledCharacteristic = peripheral.characteristic("0000FFE2-0000-1000-8000-00805F9B34FB");
 ledCharacteristic = peripheral.characteristic("0000FFE2-0000-1000-8000-00805F9B34FB");
 if (!ledCharacteristic) {
 Serial.println("Peripheral does not have LED characteristic!");
 peripheral.disconnect();
 return;
 } else if (!ledCharacteristic.canWrite()) {
 Serial.println("Peripheral does not have a writable LED characteristic!");
 peripheral.disconnect();
 return;
 }
 Cflag=true;
    //controlLed(peripheral);

    // peripheral disconnected, start scanning again
    //BLE.scanForUuid("0000FFE0-0000-1000-8000-00805F9B34FB");
 }
 }
 if(!peripheral.connected())Cflag=false;
}

void Timer_ISR(){
 if(Cflag){
 /*for(i=0;i<buff_len;i++){
 buff1[i]=cont; cont++;
 }*/
 buff[0]=cont++;
 Rflag=true;
 }
 
}


if i run that sketch the ledcharacteristic doesn't work, it connect to the peripheral, but the central can't find any characteristics and it disconnect, i tried the original example but with my own UUID and it works, but with this changes it doesn't, i also give the code for the peripheral.


thanks in advance.

Go Up