Bluetooth connection with an esp32

I have this code that I uploaded to an esp32 wroom featured in a custom PCB that also contains an LSM6DSMTR (gyroscope/accelerometer). I want to pass the data from the LSM6DSMTR via Bluetooth to an app that I am developing in vscode. I made the code below and the device appears on my pc's Bluetooth list. However, when click connect it appears connected for like 2 seconds and then appears as not connected. I need help fixing this problem s well as some guidance on what to do on the vscode side, so I can retrieve this data. I've never worked with Bluetooth before.

#include "SparkFunLSM6DS3.h"
#include "Wire.h"
#include "BLEDevice.h"
#include "BLEServer.h"
#include "BLEUtils.h"
#include "BLE2902.h"

BLECharacteristic *pCharacteristic;
BLEServer *pServer;
BLEService *pService;

uint16_t errorsAndWarnings = 0;

LSM6DS3Core myIMU(I2C_MODE, 0x6A);

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

  BLEDevice::init("ESP32_BT");
  pServer = BLEDevice::createServer();
  pService = pServer->createService(BLEUUID("0000ffe0-0000-1000-8000-00805f9b34fb"));
  pCharacteristic = pService->createCharacteristic(
                      BLEUUID("0000ffe1-0000-1000-8000-00805f9b34fb"),
                      BLECharacteristic::PROPERTY_READ |
                      BLECharacteristic::PROPERTY_WRITE
                    );
  pCharacteristic->setValue("Hello World");
  pService->addCharacteristic(pCharacteristic);
  pService->start();
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(pService->getUUID());
  pAdvertising->start();
  
  delay(1000);
  Serial.println("Processor came out of reset.\n");

  if (myIMU.beginCore() != 0) {
    Serial.print("Error at beginCore().\n");
  } else {
    Serial.print("\nbeginCore() passed.\n");
  }

  uint8_t dataToWrite = 0;

  // Setup the accelerometer
  dataToWrite = 0;
  dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_100Hz;
  dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_8g;
  dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_104Hz;
  errorsAndWarnings += myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);

  // Setup the gyroscope
  dataToWrite = 0;
  dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_2000dps; // Adjust this based on your requirements
  dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_104Hz;
  errorsAndWarnings += myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL2_G, dataToWrite);
}

void loop() {
  int16_t accelTemp[3], gyroTemp[3];

  // Read accelerometer
  for (int i = 0; i < 3; ++i) {
    if (myIMU.readRegisterInt16(&accelTemp[i], LSM6DS3_ACC_GYRO_OUTX_L_XL + 2 * i) != 0) {
      errorsAndWarnings++;
    }
  }

  // Read gyroscope
  for (int i = 0; i < 3; ++i) {
    if (myIMU.readRegisterInt16(&gyroTemp[i], LSM6DS3_ACC_GYRO_OUTX_L_G + 2 * i) != 0) {
      errorsAndWarnings++;
    }
  }

  // Print data to Serial
  Serial.print("Accel_X:");
  Serial.print(accelTemp[0]);
  Serial.print(", Accel_Y:");
  Serial.print(accelTemp[1]);
  Serial.print(", Accel_Z:");
  Serial.print(accelTemp[2]);
  Serial.print(", Gyro_X:");
  Serial.print(gyroTemp[0]);
  Serial.print(", Gyro_Y:");
  Serial.print(gyroTemp[1]);
  Serial.print(", Gyro_Z:");
  Serial.println(gyroTemp[2]);

  // Print data to BLE
  String dataToSend = String(accelTemp[0]) + "," + String(accelTemp[1]) + "," + String(accelTemp[2]) + ","
                      + String(gyroTemp[0]) + "," + String(gyroTemp[1]) + "," + String(gyroTemp[2]);
  pCharacteristic->setValue(dataToSend.c_str());

  Serial.print("Total_reported_Errors_and_Warnings:");
  Serial.println(errorsAndWarnings);

  // Ensure there is a linebreak character at the end
  Serial.println();

  delay(1000);
}

I moved your topic to a more appropriate forum category @luissilva123456.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.