Trouble coding for bluetooth RC Car

Hello everyone. I am not great at coding and without much luck I have been trying to implement specific libraries + code to an existing sketch for an Arduino FPV RC Car that is supposed to be App-controlled via Bluetooth.


The board I'm using in my project above is the new XIAO ESP32S3 Sense camera development board by Seeed Studio. It supports WiFi and BLE 5.0. The circuit so far has worked fine, testing peripherals individually. However, I don't know how to modify the following code that was intended to work on an Arduino micro car with an HC-05 Bluetooth receiver to now be compatible with the integrated Bluetooth that works on an ESP32...

Below is the unmodified code I got from GitHub for controlling the motors of a micro car remotely via BT: GitHub - TheHLab/Bluetooth-RC-Diecast-Car: Convert diecast car to Bluetooth RC car

#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial BTserial(4, 5); // RX | TX

#define TURN_PIN 2
#define GO_PIN 3
#define MIN_TURN 700
#define MAX_TURN 2000
#define STOP_SPEED 1500
#define MAX_SPEED_GO 2000
#define MAX_SPEED_BACK 1000
Servo turn;
Servo sgo;
char command;
String string;
int svangle = 0;
int slideBarValue = 50;
int index = 0;
String aCmd;

int speeds = STOP_SPEED;
int gear = 0;

void carGo(int st){
  sgo.writeMicroseconds(st);
  delay(10);
}

void setup()
{
  //Serial.begin( 9600 );//115200
  BTserial.begin( 38400 );
  pinMode(LED_BUILTIN, OUTPUT);
  turn.attach(TURN_PIN);
  svangle = map(slideBarValue, 0, 100, MIN_TURN, MAX_TURN);
  turn.writeMicroseconds(svangle);

  sgo.attach(GO_PIN);
  sgo.writeMicroseconds(speeds);
  //Serial.println("Setup done!");
}

void ledOn()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(10);
}

void ledOff()
{
  digitalWrite(LED_BUILTIN, LOW);
  delay(10);
}

void writeString(String stringData) { // Used to serially push out a String with Serial.write()
  for (int i = 0; i < stringData.length(); i++)
  {
    BTserial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }
}

void sendAck(String toSend){
   char payload[toSend.length()+1];
   toSend.toCharArray(payload, sizeof(payload));
   BTserial.write((uint8_t *)payload,sizeof(payload));
}

void loop()
{
  string = "";
  while(BTserial.available() > 0)
  {
    command = ((byte)BTserial.read());
    if(command == ':')
    {
      break;
    }
    else
    {
      string += command;
    }
    delay(1);
  }
  //if(string != "")  Serial.println(string);
  
  while( string.length() >= 3 ){
      aCmd = string.substring(0, 3);
      string = string.substring(3);
      //Serial.println(" " + aCmd);

      index = aCmd.lastIndexOf("T");
      if( aCmd == "GOO"  ){
        // Move the car 
        carGo(MAX_SPEED_GO);
      } else if( aCmd == "STG" ){
        carGo(STOP_SPEED);
        // Stop the car
      } else if( aCmd == "BAC" ){
        // Move the car back
        carGo(MAX_SPEED_BACK);
      } else if( aCmd == "STB" ){
        // Stop the car
        carGo(STOP_SPEED);
      } else if( index == 0 ){
        // Turn left/right: cmd = "T<value from 0 to 100>"
          slideBarValue = aCmd.substring(index+1).toInt();
          //Serial.println(slideBarValue );
          if( slideBarValue > 0 ){
            //turn.attach(TURN_PIN);
            svangle = map(slideBarValue, 0, 100, MIN_TURN, MAX_TURN);
            turn.writeMicroseconds(svangle);
          }
      } else if ( aCmd.lastIndexOf("S") == 0 ){
        speeds = aCmd.substring(1).toInt();
        if( speeds > 0 ){
          speeds -= 15;
          if( gear == 3 ){
            sgo.writeMicroseconds( map(speeds, 0, 100, STOP_SPEED, MAX_SPEED_GO) );
          } else if( gear == 1 ){
            sgo.writeMicroseconds( map(speeds, 0, 100, STOP_SPEED, MAX_SPEED_BACK) );
          }
          delay(10);
        }
      } else if ( aCmd.lastIndexOf("G") == 0 ){
        gear = aCmd.substring(1).toInt();
      }
  }
}

And here is the code I'm now struggling with (including my changes):

#include "BLEServer.h"
#include "BLEClient.h"
#include "BLEUtils.h"
#include "BLE2904.h"

#include <ESP32Servo.h>

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

#define TURN_PIN 1
#define GO_PIN 2
#define MIN_TURN 700
#define MAX_TURN 2000
#define STOP_SPEED 1500
#define MAX_SPEED_GO 2000
#define MAX_SPEED_BACK 1000
Servo turn;
Servo sgo;
char command;
String string;
int svangle = 0;
int slideBarValue = 50;
int currentIndex = 0;
String aCmd;

int speeds = STOP_SPEED;
int gear = 0;

void carGo(int st) {
  sgo.writeMicroseconds(st);
  delay(10);
}

void setup()
{
  Serial.begin(115200);
  Serial.println("Starting BLE work!");
  BLEDevice::init("MyESP32Car");

  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
  

  pinMode(LED_BUILTIN, OUTPUT);
  turn.attach(TURN_PIN);
  svangle = map(slideBarValue, 0, 100, MIN_TURN, MAX_TURN);
  turn.writeMicroseconds(svangle);

  sgo.attach(GO_PIN);
  sgo.writeMicroseconds(speeds);
  //Serial.println("Setup done!");
}

void ledOn()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(10);
}

void ledOff()
{
  digitalWrite(LED_BUILTIN, LOW);
  delay(10);
}

void writeString(String stringData) { // Used to serially push out a String with Serial.write()
  for (int i = 0; i < stringData.length(); i++)
  {
    BTserial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }
}

void sendAck(String toSend) {
  char payload[toSend.length() + 1];
  toSend.toCharArray(payload, sizeof(payload));
  BTserial.write((uint8_t *)payload, sizeof(payload));
}

void loop()
{
  string = "";
  while (BTserial.available() > 0)
  {
    command = ((byte)BTserial.read());
    if (command == ':')
    {
      break;
    }
    else
    {
      string += command;
    }
    delay(1);
  }
  //if(string != "")  Serial.println(string);

  while ( string.length() >= 3 ) {
    aCmd = string.substring(0, 3);
    string = string.substring(3);
    //Serial.println(" " + aCmd);

    currentIndex = aCmd.lastIndexOf("T");
    if ( aCmd == "GOO"  ) {
      // Move the car
      carGo(MAX_SPEED_GO);
    } else if ( aCmd == "STG" ) {
      carGo(STOP_SPEED);
      // Stop the car
    } else if ( aCmd == "BAC" ) {
      // Move the car back
      carGo(MAX_SPEED_BACK);
    } else if ( aCmd == "STB" ) {
      // Stop the car
      carGo(STOP_SPEED);
    } else if (currentIndex == 0 ) {
      // Turn left/right: cmd = "T<value from 0 to 100>"
      slideBarValue = aCmd.substring(currentIndex + 1).toInt();
      //Serial.println(slideBarValue );
      if ( slideBarValue > 0 ) {
        //turn.attach(TURN_PIN);
        svangle = map(slideBarValue, 0, 100, MIN_TURN, MAX_TURN);
        turn.writeMicroseconds(svangle);
      }
    } else if ( aCmd.lastIndexOf("S") == 0 ) {
      speeds = aCmd.substring(1).toInt();
      if ( speeds > 0 ) {
        speeds -= 15;
        if ( gear == 3 ) {
          sgo.writeMicroseconds( map(speeds, 0, 100, STOP_SPEED, MAX_SPEED_GO) );
        } else if ( gear == 1 ) {
          sgo.writeMicroseconds( map(speeds, 0, 100, STOP_SPEED, MAX_SPEED_BACK) );
        }
        delay(10);
      }
    } else if ( aCmd.lastIndexOf("G") == 0 ) {
      gear = aCmd.substring(1).toInt();
    }
  }
}

If any of you have possible suggestions or solutions to where in the code I need to change something so it makes the ESP32 board not only turn up as a BT device in my phone but also take inputs from the app I'm using, it would be great: https://play.google.com/store/apps/details?id=com.thehlab.pc.rccar&pli=1

Any help would be much appreciated!

The HC-05 Is "Classic" Bluetooth SPP - effectively, a serial port over Classic BT.

BLE is very different - so it'll probably be a case of starting again from scratch.

There must be BLE examples for the ESP32 ...