HC-05 automated connection using HC05 Library

Hi everyone,

I'll appreciate any help possible... I'm quite stumped...

For a project I'm working on, I'm trying to setup two HC-05s to connect without having to input AT commands manually. For this, I also need to be able to set the HC-05 into AT command mode through code, rather than hardware changing the hardware.
To do this, I'm using the HC-05 Library (GitHub - jdunmire/HC05: Arduino Library for the HC-05 bluetooth transciever), as it is able to change the HC-05 between AT mode and non-AT easily. I attempt to search for devices using the Master, and for now store the name of all devices. (In the future I will check the name of each device and create the connection)

However, I've run into an issue. Sometimes, the code "sorta" works (aka I can at least get the slave device address in the String... I could sort through the string to get that address), but many times it doesn't work at all.

Below is my code for the master and a Fritzing diagram for both devices:

#include "HC05.h"

#ifdef HC05_SOFTWARE_SERIAL
#include <SoftwareSerial.h>
HC05 btSerial = HC05(A2, A5, A3, A4);  // key, state, rx, tx
#else
HC05 btSerial = HC05(3, 2);  // cmd, state
#endif

#ifdef DEBUG_HC05
#ifdef DEBUG_SW_PORT
  extern SoftwareSerial DEBUG_PORT;      //This will allow me to print to serial without sending to BT
#endif
#endif

String BTs;

void setup() {
 DEBUG_BEGIN(38400);
 DEBUG_PRINTLN("Setup");
  delay(3000);  // this delay is for debugging convenience only
 DEBUG_PRINTLN("DelayComplete");

  //Let's startup the device and clear it from past searching
  btSerial.findBaud();
  delay(2000);
  btSerial.setTimeout(1000);
  btSerial.cmd("AT+ORGL"); //factory reset
  btSerial.setBaud(38400);
  btSerial.cmd("AT+INQC");

  //Setup the bluetooth as a master
  btSerial.cmd("AT+ROLE=1"); //Master
  btSerial.cmd("AT+NAME=CHILD"); //Just a name given to the device
  btSerial.cmd("AT+PSWD=1234"); //Password shared with Slave device
  btSerial.cmd("AT+CMODE=0");
  DEBUG_PRINTLN(" ");
  DEBUG_PRINTLN(" ");
  DEBUG_PRINTLN(" ");

  //Let's setup a connection
  DEBUG_PRINTLN(" ");
  btSerial.cmd("AT+INIT"); //initialize SPP 
  DEBUG_PRINTLN(" ");
  btSerial.cmd("AT+INQM=0,5,9"); //search for 5 devices, timeout 9 sec
  DEBUG_PRINTLN(" ");
  btSerial.cmd("AT+INQ"); //Search for devices
    BTs+=btSerial.readString(); 
  DEBUG_PRINTLN(" ");
  DEBUG_PRINTLN(" ");
  DEBUG_PRINTLN("Here come BT addresses:");
  DEBUG_PRINTLN(BTs);
}

void loop() {
}

Below I will attach two copies of the Serial Monitor's response... The first one shows how sometimes it works. The second one shows when it doesn't work.... specifically take a look at the bolded parts, as I'm not sure what they mean or why they're appearing.

Working example:

Setup
DelayComplete
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... x
Trying 38400... Found.
AT+ORGL
[b]timeout 1[/b]
AT+UART=38400,0,0
⸮⸮AT+RESET
OK
AT+INQC
OK
AT+ROLE=1
OK
AT+ADDR?
[b]timeout 1[/b]
AT+NAME=CHILD
+ADDR:14:3:63000
OK
AT+PSWD=1234
FAIL
OK
AT+CMODE=0
OK
 
 
 
 
AT+INIT
FAIL
OK
 
AT+INQM=0,5,9
OK
 
AT+INQ
FAIL
OK
 
 
Here come BT addresses:
[b]⸮H⸮OK
FAIL
ERROR:(17)
FAIL[/b]
+INQ:14:3:62EF4,0,OK
[b]+INQ:14:3:[/b]

Not working example:

Setup
DelayComplete
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... x
Trying 38400... Found.
AT+ORGL
[b]timeout 1[/b]
AT+UART=38400,0,0
OK
AT+RESET
OK
AT+INQC
OK
AT+ROLE=1
OK
AT+ADDR?
[b]timeout 1[/b]
AT+NAME=CHILD
+ADDR:14:3:63000
OK
AT+PSWD=1234
OK
AT+CMODE=0
OK
 
 
 
 
AT+INIT
FAIL
OK
 
AT+INQM=0,5,9
FAIL
[b]ERROR:(17)
FAIL
timeout 1[/b]
 
AT+INQ
[b]timeout 1[/b]
 
 
Here come BT addresses:

Between these two examples nothing changed, I just reuploaded the code to the UNO and tried it again.

If you could give any advice, I would greatly appreciate it!
I believe I'm using this HC-05: DSD TECH Official Website: DSD TECH HC-06 Wireless Bluetooth Serial Transceiver Support Module Slave and Master Mode For Arduino + 4PIN DuPont Cable
And an Arduino UNO

Your code does not appear to agree with your diagram. If the latter is true, you are probably not getting a result at all, you just think you are. It is good practice to use a 1k/2k voltage divider on Arduino Tx. I don't know why you would speculate that Bluetooth would be on analogue pins, and the only baud rate that would work for configuration is 38400.

Nick_Pyner:
Your code does not appear to agree with your diagram. If the latter is true, you are probably not getting a result at all, you just think you are. It is good practice to use a 1k/2k voltage divider on Arduino Tx. I don't know why you would speculate that Bluetooth would be on analogue pins, and the only baud rate that would work for configuration is 38400.

So sorry! I uploaded the wrong Fritzing Diagram.

I'm using analog pins as recommended by the library creator (in his/her example code).

I'm just unsure of where this "timeout 1" comes from, as I can't find it anywhere on google. When this doesn't occur, everything works fine :frowning:

EDIT: After experimenting more, it seems that as expected, using btSerial.setTimeout to change the timeout period of all commands does change the time before this "timeout 1" appears. I'm just unsure as to why its timing out.

scrimarc:
I'm using analog pins as recommended by the library creator

God only knows why the analogue pins are recommended, I guess you can do that, even if nobody else does, but I think the code is rubbish from end to end. The mere fact that you are using any sort of library at all rings the alarm bell. Since you are only talking about using two HC-05, there are well-established methods without having to go through this mess. This might help you
http://www.martyncurrey.com/arduino-to-arduino-by-bluetooth/#more-2922

The issue is that I'm trying to automate AT commands and automatically setup new connections... The person that wrote your tutorial attached a link to another guide to connect two HC05's (Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link | Martyn Currey) ; however, they do it by changing hardware and manually inputting AT commands, which is not what I want. This is why I need to use this library, or so I believe.

You can make new connections to various slaves by controlling the power supply to master, thereby breaking the connection, and then engaging AT mode to reconfigure. This should be easy enough with the plain-vanilla JY-HCU board, but may involve a bit of soldering if you have a board with a button on it.

Martyn Currey has several papers on bluetooth<>bluetooth connection. The power-on auto-connect is what you need. I don't think he actually addresses polling several devices but the procedure is the same.

Nick_Pyner:
You can make new connections to various slaves by controlling the power supply to master, thereby breaking the connection, and then engaging AT mode to reconfigure. This should be easy enough with the plain-vanilla JY-HCU board, but may involve a bit of soldering if you have a board with a button on it.

Martyn Currey has several papers on bluetooth<>bluetooth connection. The power-on auto-connect is what you need. I don't think he actually addresses polling several devices but the procedure is the same.

Unfortunately my HC-05 model does have a button.
Turns out I found a way online to do it without any library.

Would you mind taking a look at my new thread I made about an issue I'm having regarding Inquiring mode (And it not shutting off after inputting "AT+INQC")