HC05 bluetooth pairing questions

Status
Not open for further replies.

Brion Sohn

Well-known member
Ok so I am having an impossible time finding information on what I am hoping to do so I thought I would just ask to find out if anyone has done it or has any ideas of how to do it

Anyway what I am trying to do is make a teensy powered master that is connected to the computer that can pair with multiple wireless external slave teensy devices. That would send and receive information over the Bluetooth via serial.

I have found all of the information about being able to pair and bind one set of HC05 06 modules but what I would like to do is be able to have the master connected to the computer scan and pair with different HC05/06s.

From what I understand the HC05 in master mode will automatically try to pair with the strongest local device to it but what I want to do is have a little control in that it will only connect with devices that maybye are sending a pairing command or something to that effect.

I’m basically wanting it to reject trying to pair with say a cell phone but try to only pair with certain devices that I have built using the teensy.

I have thought about using the At commands to reprogram via AT on startup by teensy controlled power programming but at the same time it won’t know the address of the slave every time so that probably would t do any good.

I hope I am explaining this well in That I would like the master unit to be able to pair with different devices but only one pairing at a time and only one per startup.

Anyway if anyone knows of any resources or what to do or if it can even be done please help if possible

Thank you. Brion
 
OK So with a ton of more looking around I think I have figured out what I need to do ignorer to get this to work.. HOWEVER.. I have that pesky HC-05 v3.0.. Does anyone Know if anyone has figured out how to get the INQ modes and things working.. I have found a few things regarding possibilities but none of this is working..

I am currently testing with the following basic code to try to figure out all of the AT commands and I am commenting out different things here and there because I have not figured out how to be able to actually send though the Serial Monitor to the HC05 (with either board).

NOTE: This code isn't actually working properly on the TEENSY LC but it is working on a Pro Micro (Leonardo) just fine.

Code:
void setup(){
 Serial.begin(115200);     // Serial to PC
 Serial1.begin(38400);     // Serial to HC-05 BlueTooth module in "AT" mode always @38k4


 Serial1.print("AT+CLASS=0\r\n");
 Serial1.print("AT+ROLE=1\r\n");
 Serial1.print("AT+CMODE=1\r\n");
 Serial1.print("AT+NAME=TEST\r\n");
 Serial1.print("AT+INQM=1,9,48\r\n");
 //Serial1.print("AT+INQC\r\n");
 //Serial1.print("AT+RESET=1\r\n");

}

void loop(){
// Serial.println("TEENSY");
 Serial.println("AT+STATE");
 Serial1.print("AT+STATE\r\n");      // Send an AT command and \r\n to the BT - must be in this sequence
 delay(200);

// Serial.println("AT+CLASS?");
// Serial1.print("AT+CLASS?\r\n");      // Send an AT command and \r\n to the BT - must be in this sequence
// delay(200);

// Serial.println("AT+NAME?");
// Serial1.print("AT+NAME?\r\n");      // Send an AT command and \r\n to the BT - must be in this sequence
// delay(200);

// Serial.println("AT+ROLE?");
// Serial1.print("AT+ROLE?\r\n");      // Send an AT command and \r\n to the BT - must be in this sequence
// delay(200);

 Serial.println("AT+INQ");
 Serial1.print("AT+INQ\r\n");      // Send an AT command and \r\n to the BT - must be in this sequence
 delay(200);



  
 while (Serial1.available()) {                 // read the response from the BT module

   delay(0);  //delay to allow buffer to fill 
   if (Serial1.available() >0) {
     char c = Serial1.read();     // gets one byte from serial buffer
     Serial.print(c);             // Print Character
   } 
 }

 Serial.println(); Serial.println();
 delay(3000);
}

All of the set commands in set-up that are not commented out do work except INQ which returns the (1F) error.

Anyway with this code I am not getting response on the Teensy in the Serial Monitor.

Basically I believe the INQ command is the one I need as I need to read in the response and then have the HC)% retro the Names of the devices.. Once that is done I will know which I want to connect to and then can have it Pair to that specific device. Hopefully would allow me to add additional connectable devices (1 at a time) and be able to filter out the devices that I don't want to connect to. Yes there is the issue with this in that if I have two of the devices on at the same time it will just choose one to connect to.

Anyway nay help in figuring out what is going on with he code for the teensy and if anyone has found anything regarding the HC05v3 and INQ.

The Last thing I am having happen which is weird but I think is a buffer overflow is if I have too many of my inquires uncommented the Serial response is truncated, cutting off the display of everything after a certain point unit the next loop (where it redispays).. Granted I don't know what the Teensy LC does with that as I haven't YET been able to get it to display the response.
 
OK So..

I have finally MADE myself an acceptable AT Programmer as I couldn't fine any online out of the many that are out there to be responsive and or WORK at all.. This one was honestly based off of one that I found on the Web that Partially worked so I can't take full credit BUT here it is and hopefully it will help out others..

It works Flawlessly as-is on BOTH the TeensyLC and Arduino Pro MICRO (Leonardo) using hardware serial and serial1.

Code:
String readString;      // String contains the response from the BT module
String sendString;      // String contains the command for the BT module

void setup(){
  Serial.begin(115200);  // Serial to Terminal
  Serial1.begin(38400);  // Serial to HC-05 BlueTooth module in "AT" mode always @38k4
  delay(3000);
  Serial.println ("Bluetooth AT COMMAND MODE Ready: ");
  Serial.println ();
}


void loop(){
 
  while (Serial.available()) {                          // reads User Terminal Input
    delay(3);                                           // delay to allow buffer to fill 
    if (Serial.available() >0) {
      char s = Serial.read();                           // reads one byte from serial buffer
      sendString += s;                                  // makes the string "sendString"
    } 
  }
  
  if (sendString != "") {
    Serial1.println(sendString);                        // Send to BT
    Serial.print ("SENT: "); Serial.print(sendString);  // echo Sent String
    sendString = "";                                    // clear sendString
  }

  while (Serial1.available()) {                         // reads BT module response
    delay(3);                                           //delay to allow buffer to fill 
    if (Serial1.available() >0) {
      char c = Serial1.read();                          // reads one byte from serial1 buffer
      readString += c;                                  // makes the string "readString"
    } 
  }
  
  if (readString != "") {
    Serial.print ("RETURNED: "); Serial.println(readString);  // Print response from BT Module to Terminal
    readString = "";                                          // clear readString
  }
}

Anyway I thought this could be useful to others possibly..

PS: I am not sure if Project Guidance is any longer the place for this to be so if a moderator wants to move this thread please do so..

I STILL though have not figured out the HC05 v3 INQ issues but have found something that explains things and in my playing I DID have the Master HC05 automatically Pair with the Slave UNIT however I have NO CLUE as to why It chose that over my phone or computer which both could locate it.
 
NOTE on the above:

That sketch actually worked perfectly for full 2-Way communication as long as you set the HC-05/06 to a Baud rate of 38400 via AT or you could alter the sketch to run serial1.begin (9600);
 
OK I found one issue with the above and I am not sure how to fix it.. IF i send RAW HEX Byte data via a terminal that can with the sketch when the first BYTE is 0x00 the system will HANG and not allow additional sends without restarting.
 
Just a quick note: your "delay(3)" works for 115200 data speed; but it's not going to work for different speeds.

With my HC-05 (v2 and I only use it as slave, so cannot help with INQ) I have to use 3ms for 115200, 2ms for 230400, 1ms for 460800 and no delay for 921600 (won't work at all at higher speeds).

Another quick note: it's fine at 115200 and actually won't gain much real-world speed at higher bauds.
I get about 8KB/s at 115200, 14KB/s at 230400, 20 KB/s at 460800 and 24 KB/s at 921600; this is with an Android smartphone at about 1m.
 
Status
Not open for further replies.
Back
Top