arduino-libraries / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
291 stars 198 forks source link

problems connecting to a device #329

Closed Nocentinia closed 8 months ago

Nocentinia commented 8 months ago

I'm having a huge problem with the library using the Arduino GIGA R1 WiFi.

I have a system made up of GPS, IMU and SD card and I would like to connect to a peripheral. if I load the example program the device is easily detected and I can connect to it but as soon as I insert the code in the example into the code of my project everything stops working!

I use the function: BLE.scanForAddress("");

every maybe 10 minutes or more it detects the device but via the function: 'peripheral.connect()` I can never connect.

If I load the LedControl example, and change the parameters with what I need, everything works!

This is the part of code that I use to manage the BLE device:

#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <Wire.h>
#include <SparkFun_u-blox_GNSS_Arduino_Library.h>
#include <SPI.h>
#include <SD.h>
#include <ArduinoBLE.h>
#include "Arduino_GigaDisplay_GFX.h"
#define BIANCO 0xffff
#define NERO 0x0000

#define mySerial Serial1

/*#define rosso = 86   //staz meteo rosso
#define verde = 87   //GPS verde
#define blu  = 88  //blu
*/

SFE_UBLOX_GNSS myGNSS;
GigaDisplay_GFX display;
Adafruit_BNO055 bno = Adafruit_BNO055(-1, 0x28, &Wire);
void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);  // wait for serial port to open!

  Serial.println("SparkFun u-blox Example");
  do {
    Serial.println("GNSS: trying 38400 baud");
    mySerial.begin(38400);
    if (myGNSS.begin(mySerial) == true) break;

    delay(100);
    Serial.println("GNSS: trying 9600 baud");
    mySerial.begin(9600);
    if (myGNSS.begin(mySerial) == true) {
      Serial.println("GNSS: connected at 9600 baud, switching to 38400");
      myGNSS.setSerialRate(38400);
      delay(100);
    } else {
      //myGNSS.factoryReset();
      delay(2000);  //Wait a bit before trying again to limit the Serial output
    }
  } while (1);
  Serial.println("GNSS serial connected");

  myGNSS.setUART1Output(COM_TYPE_UBX);  //Set the UART port to output UBX only
  myGNSS.setI2COutput(COM_TYPE_UBX);    //Set the I2C port to output UBX only (turn off NMEA noise)
  myGNSS.setNavigationFrequency(1);
  myGNSS.setAutoPVT(true);
  myGNSS.saveConfiguration();  //Save the current settings to flash and BBR

  if (!bno.begin()) {
    /* There was a problem detecting the BNO055 ... check your connections */
    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
    while (1)
      ;
  }
  bno.setExtCrystalUse(true);

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    while (1)
      ;
    sd = false;
  }

  File dataFile = SD.open(numerazione + ".CSV", FILE_WRITE);
  if (dataFile) {  //TWA angolo vento reale, TWS velocita vento reale, heel rollio
    Serial.println("Creo data");
    //dataFile.println("ISODateTimeUTC,Lat,Lon,BoatSpeed,TWA,TWS,ax,ay,az,gx,gy,gz,mx,my,mz,Heading,rollio,beccheggio,rottagps,Sys,Gyro,Acc,Mag,");
    dataFile.println("ISODateTimeUTC,Lat,Lon,rottagps,BoatSpeed,AWA,AWS,TWA,TWS,Sys,Gyro,Acc,Mag,");
    dataFile.close();
  }

  if (SD.exists(numerazione + ".CSV")) {
    Serial.println("Creazione completata");
  }

  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy failed!");
    while (1)
      ;
  }
  BLE.scanForAddress("d3:82:53:e0:a9:72");

  pinMode(86, OUTPUT);  //rosso
  pinMode(87, OUTPUT);  //verde
  pinMode(88, OUTPUT);  //blu

  digitalWrite(86, LOW);
  delay(2000);
  digitalWrite(86, HIGH);
  delay(1000);

  display.begin();
  display.fillScreen(NERO);
  display.setTextSize(3);
  display.setTextColor(BIANCO);
  display.setRotation(1);
}

void loop() {
  BLEDevice 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();

    if (peripheral.localName() != "ULTRASONIC") {
      return;
    }
    Serial.println("hi");

    BLE.stopScan();
    Ble(peripheral);
    BLE.scanForAddress("d3:82:53:e0:a9:72");
  }
  digitalWrite(87, HIGH);
  Main();

  delay(1);
}

void Ble(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }
}

// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
  Serial.println("Attributes discovered");
} else {
  Serial.println("Attribute discovery failed!");
  peripheral.disconnect();
  return;
}

BLECharacteristic A_W_I_Caratteristic = peripheral.characteristic("2A72");
BLECharacteristic A_W_D_Caratteristic = peripheral.characteristic("2A73");
A_W_I_Caratteristic.subscribe();
A_W_D_Caratteristic.subscribe();

if (!A_W_I_Caratteristic) {
  peripheral.disconnect();
  return;
} else if (!A_W_I_Caratteristic.canRead()) {
  peripheral.disconnect();
  return;
}
Serial.println("ok");
digitalWrite(87, LOW);
while (peripheral.connected()) {
  //Serial.println("culo");
  if (A_W_I_Caratteristic.valueUpdated()) {
    A_W_I_Caratteristic.readValue(W_Intensity_raw);
    AWS = W_Intensity_raw / 100.0;
  }
  if (A_W_D_Caratteristic.valueUpdated()) {
    A_W_D_Caratteristic.readValue(W_Dir_raw);
    AWA = W_Dir_raw / 100;
    Wind = true;
  }

  Main();
}
}
per1234 commented 8 months ago

Hi @Nocentinia. Thanks for your interest in this open source project. This issue tracker is only to be used to report bugs or feature requests specific to the project. This topic is more appropriate for the Arduino Forum. I'm sure we will be able to help you out over there:

https://forum.arduino.cc/