Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connecting ESP32 to HC-06 module #6876

Open
1 task done
treysblackwell opened this issue Jun 16, 2022 · 8 comments
Open
1 task done

Connecting ESP32 to HC-06 module #6876

treysblackwell opened this issue Jun 16, 2022 · 8 comments
Assignees
Labels
Area: BT&Wifi BT & Wifi related issues Status: Needs investigation We need to do some research before taking next steps on this issue

Comments

@treysblackwell
Copy link

Board

ESP32 Dev Module

Device Description

ESP32 Dev Module

Hardware Configuration

Nothing

Version

v2.0.3

IDE Name

Arduino IDE, Visual Micro

Operating System

Windows 11

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

921600

Description

I'm trying to pair and connect my ESP32 Dev Module to an HC-06. The goal is to be able to send serial commands from the ESP32 to the HC-06.

I started with the SerialToSerialBTM.ino example and cannot get the two to pair or connect. I have tried using both the name and the address as methods of connecting, and neither is working. I have verified correctness of the address and spelling of the name, as well as the PIN (I've tried running the sketch with the line both commented and not commented).

I can pair my phone with the HC-06 no problem, and can also pair my phone with the ESP32 no problem, just can't get the two to pair together.

I've been doing lots of digging online and found issue #3916, which looks like it is the same issue, and the solution was to downgrade a few versions, but at this point it was many versions ago. It was with version 1.0.4, and we are now on version 2.0.3. I tried downgrading to version 1.0.2, but was unable to get it to work (but I won't claim I did it 100% correctly).

I've copied the SerialToSerialBTM.ino in its original form and then added comments next to the things I changed to try to get it to work.

I also set core debug level to "info" in the IDE and copied some of printout. The scanned device that is shown is the MAC address of the HC-06 I am trying to pair with, so it's there and ready, but the ESP32 just can't do it for some reason.

This is my first attempt at a project with an ESP32, so I would really appreciate any suggestions that might help me get this working!

Sketch

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

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

String MACadd = "AA:BB:CC:11:22:33"; //CHANGE - replaced with MAC address of my HC-06
uint8_t address[6]  = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; //CHANGE - replaced with MAC address of my HC-06
//uint8_t address[6]  = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "OBDII"; //CHANGE - replaced with name of my HC-06
const char *pin = "1234"; //<- standard pin would be provided by default //CHANGE - replaced with PIN of my HC-06
bool connected;

void setup() {
  Serial.begin(115200); //CHANGE - baud rate changed to 9600 to match the HC-06
  //SerialBT.setPin(pin);
  SerialBT.begin("ESP32test", true); //CHANGE - changed string to the desired name of my ESP32 module
  //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);
}

Debug Message

[ 20073][I][BluetoothSerial.cpp:423] esp_bt_gap_cb(): ESP_BT_GAP_DISC_RES_EVT properties=2
[ 20073][I][BluetoothSerial.cpp:426] esp_bt_gap_cb(): Scanned device: 00:14:06:05:59:dc
[ 20224][I][BluetoothSerial.cpp:423] esp_bt_gap_cb(): ESP_BT_GAP_DISC_RES_EVT properties=2
[ 20224][I][BluetoothSerial.cpp:426] esp_bt_gap_cb(): Scanned device: 00:14:06:05:59:dc
[ 20645][I][BluetoothSerial.cpp:423] esp_bt_gap_cb(): ESP_BT_GAP_DISC_RES_EVT properties=2
[ 20645][I][BluetoothSerial.cpp:426] esp_bt_gap_cb(): Scanned device: 00:14:06:05:59:dc
[ 20765][I][BluetoothSerial.cpp:423] esp_bt_gap_cb(): ESP_BT_GAP_DISC_RES_EVT properties=2
[ 20794][I][BluetoothSerial.cpp:426] esp_bt_gap_cb(): Scanned device: 00:14:06:05:59:dc
[ 21206][I][BluetoothSerial.cpp:423] esp_bt_gap_cb(): ESP_BT_GAP_DISC_RES_EVT properties=2
[ 21206][I][BluetoothSerial.cpp:426] esp_bt_gap_cb(): Scanned device: 00:14:06:05:59:dc
[ 26462][I][BluetoothSerial.cpp:423] esp_bt_gap_cb(): ESP_BT_GAP_DISC_RES_EVT properties=1
[ 26462][I][BluetoothSerial.cpp:426] esp_bt_gap_cb(): Scanned device: 00:14:06:05:59:dc
[ 26516][I][BluetoothSerial.cpp:503] esp_bt_gap_cb(): ESP_BT_GAP_DISC_STATE_CHANGED_EVT stopped
Failed to connect. Make sure remote device is available and in range, then restart app.
Failed to connect. Make sure remote device is available and in range, then restart app.
Failed to connect. Make sure remote device is available and in range, then restart app.
Failed to connect. Make sure remote device is available and in range, then restart app.
Failed to connect. Make sure remote device is available and in range, then restart app.

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@treysblackwell treysblackwell added the Status: Awaiting triage Issue is waiting for triage label Jun 16, 2022
@VojtechBartoska VojtechBartoska added Area: Peripherals API Relates to peripheral's APIs. Type: Question Only question Area: BT&Wifi BT & Wifi related issues Status: Needs investigation We need to do some research before taking next steps on this issue and removed Status: Awaiting triage Issue is waiting for triage Area: Peripherals API Relates to peripheral's APIs. Type: Question Only question labels Jun 17, 2022
@SuGlider
Copy link
Collaborator

It seems that there is an issue with IDF 4.3+ Bluetooth layer and SPP (BluetoothSerial).
My suggestion is to downgrade to Arduino Core 1.0.4 and try to use HC-06.
Core 1.0.4 uses IDF 3.x, which seems to work fine with pairing (using Pin) and BluetoothSerial.

Read this issue: #5053

Related issues:

#6061
espressif/esp-idf#8394

@SuGlider
Copy link
Collaborator

By other hand, it works perfectly fine when connecting to a PC or Smartphone....
The issue seems to be with HC-06 BT modules.
Check #6847 for connecting ESP32 BluetoothSerial with Windows.

@treysblackwell
Copy link
Author

Downgrading to 1.0.4 did not work, I had to go all the way down to 1.0.2 and replace the BluetoothSerial.h file with this one:

https://raw.githubusercontent.com/espressif/arduino-esp32/f0e2e2a62fe98ec9657f3935c41c88a0fd0e7acd/libraries/BluetoothSerial/src/BluetoothSerial.h

And BluetoothSerial.cpp with this one:

https://github.com/espressif/arduino-esp32/blob/f0e2e2a62fe98ec9657f3935c41c88a0fd0e7acd/libraries/BluetoothSerial/src/BluetoothSerial.cpp

Then it worked. It would be great if there could be compatibility for this type of connection to an HC-06 module in one of your next releases that includes the more up to date content in your more recent versions.

Thank you for your help!

@SuGlider
Copy link
Collaborator

Thanks for checking it!
So with those 2 files it worked under Core 2.0.0+ with IDF 4.4?
Or it only works under Core 1.0.2?

@SuGlider
Copy link
Collaborator

I don't have a HC-06 here to test, but I think that the issue may be in this Line (v2.0.0+)
https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/src/BluetoothSerial.cpp#L934
I think that it doesn't connect because HC-06 firmware can't deal with encryption, which runs fine with Windows, for instance.

Maybe replacing it by a non-encrypted connection parameter may make it work with the latest Core version.

@treysblackwell
Copy link
Author

Thanks for checking it! So with those 2 files it worked under Core 2.0.0+ with IDF 4.4? Or it only works under Core 1.0.2?

With those two files, it works under Core 1.0.2.

I don't have a HC-06 here to test, but I think that the issue may be in this Line (v2.0.0+) https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/src/BluetoothSerial.cpp#L934 I think that it doesn't connect because HC-06 firmware can't deal with encryption, which runs fine with Windows, for instance.

Maybe replacing it by a non-encrypted connection parameter may make it work with the latest Core version.

That is very possible, I see that the encryption is not there in 1.0.2, which is when it works. I can give it a try later. It may be as simple as replacing ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE with ESP_SPP_SEC_NONE.

@RedCatTrix
Copy link

RedCatTrix commented Feb 10, 2023

Seems, I found solution working for me and possible reason. I use 2.0.6 version core.

On first power on ESP32 can find and connect HC-06 by Address, I never seen peer name in debug, it never resolved (dont care, have MAC).

Once restart ESP32, it no longer able to re-connect HC-06 then. If you discover devices using standard example from library you probably find HC-06. However you couldn't connect it using code from example SerialToSerialBTM.

Next, if you reset HC-06 by RST pin or power, and then force ESP32 to re-connect HC-06, it succeeds till next connection lose

I figured out that HC-06 after lose connection stays in specific state on channel 1 and cant accept connection on any other channel untill reset. On other hand, ESP32 by default tries to re-connect using auto-channel configuration or using previously discovered data (that's not clear for me). Hence, instead

connected = SerialBT.connect(address);

I use alternative method with explicit channel number

connected = SerialBT.connect(address, 1, ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER);

I implemented both solutions to my rig. Reset HC-06 by pin from host controller and use explicit connect() method on ESP32 side.

And it works stable.

HC-06 available from smartphone anytime, even after smartphone lose connection it re-connects successfully without HC-06 reset. So I beleive that the problem in ESP32 library logic, or HAL may be. Hope, my post will help to fix this known annoying bug

p.s. sorry for my English, not my native language

@SuGlider
Copy link
Collaborator

Thanks a lot @RedCatTrix for the post!
@PilnyTomas - Maybe this information may help in the investigation you are working on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues Status: Needs investigation We need to do some research before taking next steps on this issue
Projects
Development

No branches or pull requests

4 participants