Open treysblackwell opened 2 years ago
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: https://github.com/espressif/arduino-esp32/issues/5053
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 https://github.com/espressif/arduino-esp32/issues/6847 for connecting ESP32 BluetoothSerial with Windows.
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:
And BluetoothSerial.cpp with this one:
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!
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?
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.
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.
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
Thanks a lot @RedCatTrix for the post! @PilnyTomas - Maybe this information may help in the investigation you are working on.
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
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide