Connecting BT LE device

EaBulb32
Posts: 2
Joined: Thu Mar 07, 2024 7:46 am

Connecting BT LE device

Postby EaBulb32 » Thu Mar 07, 2024 8:03 am

hi all.. I recently purchased an ESP32 and am looking to try and connect it to a gimbal using bluetooth.. Someone else has already made this work using the same brand gimbal and they provided their sketch.. so i bought an ESP32 thinking it should work fairly easily butso far no luck.. im wondering if someone might be able to identify where its failing ?

i have a small analog joystick connected which i know is working as i tried it with another sketch.. when i upload the bluetooth sketch and move the joystick i get nothing... the status monitor shows a message that looks like its trying to connect but it repeats over and over, which makes me wonder if the gimbal is disconnecting and the ESP32 keeps trying to reconnect ? here is the message i see in status window..

rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1344
load:0x40078000,len:13964
load:0x40080400,len:3600
entry 0x400805f0
E (10) Starting Arduino BLE Client application...
BLE Advertised Device found: Name: , Address: XX:XX:XX:XX:XX:XX, manufacturer data: 09050005, serviceUUID: 0000fee9-0000-1000-8000-00805f9b34fb, serviceUUID: 0000180a-0000-1000-8000-00805f9b34fb, serviceUUID: 0000fee8-0000-1000-8000-00805f9b34fb, rssi: -78
Found our device! address: Forming a connection to ac:9a:22:88:7e:81
- Created client
- Connected to server
- Found our service
- Found our characteristic
The characteristic value was:
ets Jul 29 2019 12:21:46

the joystick pins align with whats in the sketch and i also used nrf app on my tablet to verify all the BT characteristics match so the sketch should work with my gimbal but i get nothing ? i can post the sketch here if needed also... if anyone has an idea where it might be failing id appreciate any help.. thanks

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: Connecting BT LE device

Postby chegewara » Thu Mar 07, 2024 5:40 pm

Idea:
what is the first line of code after this one
The characteristic value was:
Its where the code fails

EaBulb32
Posts: 2
Joined: Thu Mar 07, 2024 7:46 am

Re: Connecting BT LE device

Postby EaBulb32 » Fri Mar 08, 2024 12:09 am

thanks for the reply... i'll post the code below..

if that line you mentioned is a problem, would it be because the code has an error ? or because my gimbal just isn't responding with the expected characteristic data ? i havent changed anything from the original code except for the pin numbers where the joystick are connected... appreciate any insight you might have..





Code: Select all

#include "BLEDevice.h"
//#include "BLEScan.h"
const int SW_pin = 34; // digital pin connected to switch output
const int X_pin = 32; // analog pin connected to X output
const int Y_pin = 35; // analog pin connected to Y output
int Y_axis = 1800;
int X_axis = 1800;

// The remote service we wish to connect to.
static BLEUUID serviceUUID("0000fee9-0000-1000-8000-00805f9b34fb");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("d44bc439-abfd-45a2-b575-925416129600");

static BLEAddress *pServerAddress;
static boolean doConnect = false;
static boolean connected = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    Serial.print("Notify callback for characteristic ");
    Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
    Serial.print(" of data length ");
    Serial.println(length);
}

bool connectToServer(BLEAddress pAddress) {
    Serial.print("Forming a connection to ");
    Serial.println(pAddress.toString().c_str());

    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    // Connect to the remove BLE Server.
    pClient->connect(pAddress);
    Serial.println(" - Connected to server");

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      return false;
    }
    Serial.println(" - Found our service");

    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    std::string value = pRemoteCharacteristic->readValue();
    Serial.print("The characteristic value was: ");
    Serial.println(value.c_str());

    //pRemoteCharacteristic->registerForNotify(notifyCallback);
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {

      // 
      Serial.print("Found our device!  address: "); 
      advertisedDevice.getScan()->stop();

      pServerAddress = new BLEAddress(advertisedDevice.getAddress());
      doConnect = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 30 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  pBLEScan->start(30);

} // End of setup.

// This is the Arduino main loop function.
void loop() {

  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer(*pServerAddress)) {
      Serial.println("We are now connected to the BLE Server.");
      connected = true;
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }

  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {

    int Y_axis = analogRead(X_pin);
    int X_axis = analogRead(Y_pin);

          //Up
     if (X_axis < 1665){
     const uint8_t onPacket[] = {0x06, 0x10, 0x01, 0x0e, 0x89, 0xc2, 0xbc, 0x06, 0x10, 0x02, 0x08, 0x00, 0x31, 0xeb};
     pRemoteCharacteristic->writeValue((uint8_t*)onPacket, 14, true);
     }

     //Down
     if (X_axis > 1900){
     const uint8_t onPacket[] = {0x06, 0x10, 0x03, 0x08, 0x00, 0x06, 0xdb, 0x06, 0x10, 0x01, 0x01, 0x76, 0xcc, 0x72};
     pRemoteCharacteristic->writeValue((uint8_t*)onPacket, 14, true);
     }

     //Left
     if (Y_axis < 1700){
     const uint8_t onPacket[] = {0x06, 0x10, 0x02, 0x08, 0x00, 0x31, 0xeb, 0x06, 0x10, 0x03, 0x01, 0x76, 0xa2, 0x12};
     pRemoteCharacteristic->writeValue((uint8_t*)onPacket, 14, true);
     }

     //Right
     if (Y_axis > 1935){
     const uint8_t onPacket[] = {0x06, 0x10, 0x02, 0x08, 0x00, 0x31, 0xeb, 0x06, 0x10, 0x03, 0x0d, 0xd9, 0xa3, 0x7a};
     pRemoteCharacteristic->writeValue((uint8_t*)onPacket, 14, true);
     }

  }

  delay(50); // Delay a second between loops.
} // End of loop

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: Connecting BT LE device

Postby chegewara » Fri Mar 08, 2024 11:39 am

This line may crash for 2 reasons:

Code: Select all

    Serial.print("The characteristic value was: ");
    Serial.println(value.c_str()); <----
    
- characteristic is not readable
- characteristic does not have any value yet, to the .c_ctr() will return NULL

Who is online

Users browsing this forum: No registered users and 44 guests