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:

ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
mode:DIO, clock div:1
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:


//Special thanks to geobruce at
//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 "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() {


void loop() {
  Wire.requestFrom(MPU_addr, 14, true);

  AcX = << 8 |;
  AcY = << 8 |;
  AcZ = << 8 |;

  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>





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:


//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() {
  SerialBT.begin("ESP32testm", 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(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).

void loop() {
  if (Serial.available()) {
  if (SerialBT.available()) {


//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"

#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it

BluetoothSerial SerialBT;

void setup() {
 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()) {
  if (SerialBT.available()) {

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.