Working with a Bluetooth HC-06 module

Status
Not open for further replies.

mconsidine

Active member
I've got a feeling I'm overlooking something very simple. But for the life of me I can't figure it out. My specific question is :

- Has anyone worked with an HC-06 (not an HC-05) Bluetooth module with a Teensy (3.1 in my case) and been able to change its ID using an AT command? Or at least pass it AT+VERSION and get a reply back? If so, could you share the code you used to do this?

I have one of these modules and can successfully connect to it with a Teensy (GND->GND, 3.3v->VCC, RX->TX, TX->RX). Code that takes input from Serial and sends it to Serial1 and vice-versa works fine. That is, I can type text in the Serial Monitor and have it show up on a Bluetooth dumb terminal app running on an Android phone. (So, obviously I am also able to pair up with the module).

The all takes place while the module is paired, though. When it is not paired, my understanding is that it is supposed to be in AT pass-thru mode. (There's a little LED on the module that blinks rapidly when its not paired up.) However, I can't seem to get any code running on the Teensy that can then talk to the module and send info back.

My initial reaction is that any text the module receive via its antenna will be interpreted as AT commands. But that would mean that it needs to be paired, I would think. And it's at this point that I also think I'm missing something obvious.

Any help would be appreciated - thanks in advance.
Matt

PS Apologies if this is in the wrong forum area.
 
Finally figured it out. What is key is that there is a 1 second delay between commands, even if its just sending "AT". So the following hacked-together kludge works :
Code:
String command = ""; // Stores response of the HC-06 Bluetooth device

void setup()
{
  delay(5000);
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Sending AT commands :");
  Serial.println("Command : AT");
  delay(1000);
  Serial1.print("AT");
  delay(1000);

    while(Serial1.available()>0) { // While there is more to be read, keep reading.
      command += (char)Serial1.read();
    }
    
    Serial.println(command);
    command = ""; // No repeats 
    
  Serial.println("Command : AT+VERSION");  
  Serial1.print("AT+VERSION");
    delay(1000);
    while(Serial1.available()>0) { // While there is more to be read, keep reading.
      command += (char)Serial1.read();
    }
    
    Serial.println(command);
    command = ""; // No repeats 

  Serial.println("Command : AT+BAUD4");  
  Serial1.print("AT+BAUD4");
    delay(1000);
    while(Serial1.available()>0) { // While there is more to be read, keep reading.
      command += (char)Serial1.read();
    }
    
    Serial.println(command);
    command = ""; // No repeats 
     
  Serial.println("Command : AT+NAMEHC-06");  
  Serial1.print("AT+NAMEHC-06");
    delay(1000);
    while(Serial1.available()>0) { // While there is more to be read, keep reading.
      command += (char)Serial1.read();
    }
    
    Serial.println(command);
    command = ""; // No repeats      
}

void loop()
{
//everything is done is setup
}

No doubt there are more elegant ways.

Matt
 
Status
Not open for further replies.
Back
Top