esp32 to esp32 bluetooth communication

Hi, I was able to successfully transmit the code below via my rf HC-12 module.

I modified the code using the SerialBT command via BluetoothSerial.h.

However, now I get the errors below on the receiver side using an ESP32 in my serial monitor:

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_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:0x3fff0018,len:4
load:0x3fff001c,len:808
load:0x40078000,len:6084
load:0x40080000,len:6696
entry 0x400802e4
Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Core 0 register dump:
PC      : 0x40173900  PS      : 0x00060630  A0      : 0x8017512e  A1      : 0x3ffd0c80  
A2      : 0x3ffc890c  A3      : 0x00000001  A4      : 0x00000002  A5      : 0x00000007  
A6      : 0x00000007  A7      : 0x00000080  A8      : 0x801738fc  A9      : 0x3ffd0c50  
A10     : 0x00000062  A11     : 0x00000001  A12     : 0x00000003  A13     : 0x00000002  
A14     : 0x00000006  A15     : 0x00000000  SAR     : 0x0000001e  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000  

Backtrace: 0x40173900:0x3ffd0c80 0x4017512b:0x3ffd0ca0 0x40178660:0x3ffd0cd0 0x40144509:0x3ffd0d90 0x40144609:0x3ffd0dc0 0x4008198d:0x3ffd0df0 0x4016a6a1:0x3ffd0e10

Code is too long for receiver and transmitter so I’m splitting it in sections:

Transmitter:

//Special thanks to geobruce at instructables.com
//Special thanks to Robin2 and his code for parsing data, and others on arduino forums
//Special thanks to Ahmet Burkay Kirnik for his code for Measure Angle with a MPU-6050(GY-521)

#include<Wire.h>
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;

const int MPU_addr = 0x68;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;

int minVal = 85;
int maxVal = 402;

double x;
int EA; //elevation angle
double z;

const int sensorPinTL = 33; //top left sensor pin
const int sensorPinBL = 35;
const int sensorPinTR = 32;
const int sensorPinBRT = 34;

// The sensor value
int TL = 0;
int BL = 0;
int TR = 0;
int BRT = 0;

// As photoresistor approaches minimum sensor value more light is seen by it
int sensorMinTL = 0;
int sensorMinBL = 0;
int sensorMinTR = 0;
int sensorMinBRT = 0;

int sensorMaxTL = 4096;
int sensorMaxBL = 4096;
int sensorMaxTR = 4096;
int sensorMaxBRT = 4096;

void setup() {
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);

  Serial.begin(115200);
  SerialBT.begin("ESP32TransmitST");
}

void loop() {
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 14, true);

  AcX = Wire.read() << 8 | Wire.read();
  AcY = Wire.read() << 8 | Wire.read();
  AcZ = Wire.read() << 8 | Wire.read();

  int xAng = map(AcX, minVal, maxVal, -180, 180);
  int yAng = map(AcY, minVal, maxVal, -180, 180);
  int zAng = map(AcZ, minVal, maxVal, -180, 180);

  EA = RAD_TO_DEG * (atan2(xAng, zAng));

  // Calibrate during the first five seconds
  while (millis() < 5000) {
    TL = analogRead(sensorPinTL);
    BL = analogRead(sensorPinBL);
    TR = analogRead(sensorPinTR);
    BRT = analogRead(sensorPinBRT);

    // Record the maximum sensor value
    if (TL > sensorMaxTL) {
      sensorMaxTL = TL;
    }
    if (BL > sensorMaxBL) {
      sensorMaxBL = BL;
    }
    if (TR > sensorMaxTR) {
      sensorMaxTR = TR;
    }
    if (BRT > sensorMaxBRT) {
      sensorMaxBRT = BRT;
    }

    if (TL < sensorMinTL) {
      sensorMinTL = TL;
    }
    if (BL < sensorMinBL) {
      sensorMinBL = BL;
    }
    if (TR < sensorMinTR) {
      sensorMinTR = TR;
    }
    if (BRT < sensorMinBRT) {
      sensorMinBRT = BRT;
    }
  }
  // Signal the end of the calibration period

  // Read the sensor
  TL = analogRead(sensorPinTL); // Top left sensor
  BL = analogRead(sensorPinBL); // Bottom left sensor
  TR = analogRead(sensorPinTR); // Top right sensor
  BRT = analogRead(sensorPinBRT); // Bottom right sensor

  // Apply the calibration to the sensor reading
  TL = map(TL, sensorMinTL, sensorMaxTL, 0, 1023);
  BL = map(BL, sensorMinBL, sensorMaxBL, 0, 1023);
  TR = map(TR, sensorMinTR, sensorMaxTR, 0, 1023);
  BRT = map(BRT, sensorMinBRT, sensorMaxBRT, 0, 1023);

  // In case the sensor value is outside the range seen during calibration
  TL = constrain(TL, 0, 1023);
  BL = constrain(BL, 0, 1023);
  TR = constrain(TR, 0, 1023);
  BRT = constrain(BRT, 0, 1023);

  //Sends analog values in this format: i.e. <380,148,224,260,45>
  Serial.print("<");
  Serial.print(TL);
  Serial.print(",");
  Serial.print(BL);
  Serial.print(",");
  Serial.print(TR);
  Serial.print(",");
  Serial.print(BRT);
  Serial.print(",");
  Serial.print(EA);
  Serial.print(">");

  Serial.println();

  SerialBT.print("<");
  SerialBT.print(TL);
  SerialBT.print(",");
  SerialBT.print(BL);
  SerialBT.print(",");
  SerialBT.print(TR);
  SerialBT.print(",");
  SerialBT.print(BRT);
  SerialBT.print(",");
  SerialBT.print(EA);
  SerialBT.print(">");

  SerialBT.println();

  delay(400);
}

I have example code of two ESP32 communication to one another so let me know if this helps. I got the code from the examples provided and changed it a bit.
If you go under Files>Examples>BluetoothSerial>SerialToSerialBTM and made the change to where the name is set to the name you choose for the other ESP32, and then upload it, it will be compatiable to the Files>Examples>BluetoothSerial>SerialToSerialBT code.

I have both of them below:

SerialToSerialBTM

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Victor Tchistiak - 2019
//
//This example demostrates master mode bluetooth connection and pin 
//it creates a bridge between Serial and Classical Bluetooth (SPP)
//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018
//

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
int i = 0;


String MACadd = "AA:BB:CC:11:22:33";
uint8_t address[6]  = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
//uint8_t address[6]  = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "ESP32test"; //                  <------- set this to be the name of the other ESP32!!!!!!!!!
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;

void setup() {
  Serial.begin(115200);
  //SerialBT.setPin(pin);
  SerialBT.begin("ESP32testm", true); 
  //SerialBT.setPin(pin);
  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(name);
  //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();
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

SerialToSerialBT

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#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 // <------- set this to be the same as the name you chose above!!!!!
 Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

I hope this helps!

Hello, thank you for your code.

For the first time, I got a result ...

My problem is that I send an angular value (from 1 to 360 °) but I just receive a value from 0 to 255 (8 bits)

How to send / receive the 16 bit value of the sensor (magnetometer)?

Thank you for your help

Serial.print instead of Serial.write

The solution below is not Bluetooth it uses esp now protocol which can communicate with less energy consumption than Bluetooth and it can communicate faster and further. It solved my problem.
You can send receive larger values with it and even float values.

Thanks to Rui Santos.