DIY Drone - ESP32 [Version 2] #2 (How to connect ESP32 to ESP32 by Bluetooth) Source Code

Check MACadd

void setup(void) {
  Serial.begin(115200);
  Serial.println("-----------------");
  uint8_t macBT[6];
  esp_read_mac(macBT, ESP_MAC_BT);
}

void loop() {
  uint8_t macBT[6];
  esp_read_mac(macBT, ESP_MAC_BT);
  Serial.printf("%02X:%02X:%02X:%02X:%02X:%02X\r\n", macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]);
  delay(10000);  
}

Controller Side

#include "BluetoothSerial.h"
#define Right_VRX_PIN 12
#define Right_VRY_PIN 13
#define Left_VRX_PIN 25
#define Left_VRY_PIN 26

int left_x_val;
int left_y_val;
int right_x_val;
int right_y_val;  

BluetoothSerial SerialBT;

String MACadd = "9C:9C:1F:D0:05:4E";//Write Drone side MAC address
uint8_t address[6]  = {0x9C, 0x9C, 0x1F, 0xD0, 0x05, 0x4E};//Write Drone side MAC address in HEX
bool connected;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test", true); 
  Serial.println("The device started in master mode, make sure remote BT device is on!");
  
  // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
  // to resolve name to address first, but it allows to connect to different devices with the same name.
  // Set CoreDebugLevel to Info to view devices bluetooth address and device names
  connected = SerialBT.connect(address);
  
  if(connected) {
    Serial.println("Connected Succesfully!");
  } else {
    while(!SerialBT.connected(10000)) {
      Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); 
    }
  }
  // disconnect() may take upto 10 secs max
  if (SerialBT.disconnect()) {
    Serial.println("Disconnected Succesfully!");
  }
  // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
  SerialBT.connect();

  pinMode(Left_VRX_PIN, INPUT);
  pinMode(Left_VRY_PIN, INPUT);
  pinMode(Right_VRX_PIN, INPUT);
  pinMode(Right_VRX_PIN, INPUT);
  
}

uint8_t calculate_checksum(uint8_t *data) {
  uint8_t checksum = 0;
  checksum |= 0b11000000 & data[1];
  checksum |= 0b00110000 & data[2];
  checksum |= 0b00001100 & data[3];
  checksum |= 0b00000011 & data[4];
  return checksum;
}

void loop() {

  uint8_t send_data[6];
  
  left_x_val = analogRead(Left_VRX_PIN) >> 4;//value 0-255 (">> 4" convert maximum value from 4095 to 255 )
  left_y_val = analogRead(Left_VRY_PIN) >> 4;//value 0-255  
  right_x_val = analogRead(Right_VRX_PIN) >> 4;//value 0-255
  right_y_val = analogRead(Right_VRY_PIN) >> 4;//value 0-255

  send_data[0] = 'T';
  send_data[1] = left_x_val;
  send_data[2] = left_y_val;
  send_data[3] = right_x_val;
  send_data[4] = right_y_val;
  send_data[5] = calculate_checksum(send_data);
  SerialBT.write(send_data, 6);

  delay(20);
}

Receiver Side

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

uint8_t calculate_checksum(uint8_t *data) {
  uint8_t checksum = 0;
  checksum |= 0b11000000 & data[1];
  checksum |= 0b00110000 & data[2];
  checksum |= 0b00001100 & data[3];
  checksum |= 0b00000011 & data[4];
  return checksum;
}

void loop() {
  uint8_t recv_data[6];
  if (SerialBT.available()) {
    SerialBT.readBytes(recv_data, 6);
    
    if (recv_data[0] != 'T') {
      Serial.print("Receive error!");
      return;
    }

    if (recv_data[5] != calculate_checksum(recv_data)) {
      Serial.print("Decode error!");
      return;
    }
    Serial.printf("left_x: %d, left_y: %d, right_x: %d, right_y: %d\n", recv_data[1], recv_data[2], recv_data[3], recv_data[4]);
  }
  delay(20);
}