Connecting Bluetooth Device to ESP32

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 but so far no luck.. im wondering if someone might be able to identify where its going wrong ?

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 gimbals 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

Welcome to the forum

Unless you post your sketch then providing help is going to be difficult

It looks like you are using BLE rather than classic Bluetooth. I don't think that the ESP32 itself supports BLE, but I could be wrong, so are you using an external BLE device attached to the ESP32 ?

thanks for the reply...

i might be wrong but im pretty sure the ESP32 i bought has bluetooth.. i'll double check the ebay listing.. i tried to get the exact same one as the person who made it work previously...

here is the video they made to get it working.. id ask that person if i could but it appears they arent active on their youtube page now...

i'll post the sketch below... i have the joystick connected to the corresponding pins at the top of the sketch so i think thats OK.. appreciate any insight..

#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

The ESP32 does support Bluetooth but what I was asking was does it support BLE ?

the exact board i purchased is ESP32S.. on the ebay listing from the seller it just says "bluetooth" but for the same model board (ESP32S) on other ebay listings it specifies Bluetooth LE.. there is a lot of ESP32 variants so i dont know if there is some differences ? i would assume "ESP32S" means the same thing from one board to another ? i guess its possible this board doesnt have bluetooth LE but id rather not buy another one without confirming thats the problem somehow first..

If this board only has bluetooth and not bluetooth LE.. would i be likely to get the status window message i did ? that appears to suggest it made a connection ? also would the sketch compile and upload if the board didnt support something in the sketch ? other than that would you know any other way to confirm what version of bluetooth my specific board supports ?

Sorry, but BLE is something that I know little or nothing about so I cannot offer any more help

the function connectToServer() is supposed to return a bool after these lines here

does this look like an error in the code or could it just be the gimbal isnt returning the expected characteristic data ? if its an error in the sketch would you know what it should look like ? if its a problem with the gimbal response im not sure how to troubleshoot further ?

i think it should work as someone else with the same gimbal seems to have gotten it working but my coding skills are basic and deciphering bluetooth protocol like ive been trying is hit and miss.. i use node red a bit and i started trying on that but was getting nowhere so i bought the ESP32 thinking it would be easier.. appreciate any help you could offer..

The response to this was

ets Jul 29 2019 12:21:46

Are you certain you have the correct characteristic you are trying to read?
Do you have any documentation on the gimble?
When you use LightBlue or nrfConnect to connect to the gimble what are all the services and characteristics you see?
Can you use on of those apps to see what you are expecting from the gimble?

i havent tried to manually send the commands to control the gimbal from nrfConnect but i have connected using that app and i can see the service uuid and the characteristic uuid of the gimbal match with the ESP32 sketch.. the person who provided the sketch has the same gimbal so i expected it would be the same but i did check and they do match.. hence its puzzling why its not working ?

do you think that error from the sketch is something i would see if the ESP32 i have here doesnt support bluetooth LE ? it seems like a fundamental problem of some sort.. also should the message i see in status monitor keep repeating ? would that occur because the sketch loops and therefore expect to see the message every 50ms ?

it sorta seems like the gimbal is closing the connection and the sketch retries to reconnect each loop ? but maybe im just misunderstanding how it works.. thanks for the help..

No. AFIK all the versions support BLE. It was only the original version which supports Classic Bluetooth.

My advice is to get the gimble to connect and respond to Light Blue or nrfConnect.

also should the message i see in status monitor keep repeating ? would that occur because the sketch loops and therefore expect to see the message every 50ms ?

I'm actually not clear why the central is reading anything from the gimble. The code is only writing.

it sorta seems like the gimbal is closing the connection and the sketch retries to reconnect each loop ?

Because of the control based on doConnect == true or false I think you will only try and connect once. The sketch could probably use some code to reconnect if the connection gets dropped.

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