[SOLVED] Pairing two HC-05 bluetooths with each other using arduino mega

Status
Not open for further replies.

robosonix

Newbie level 1
Joined
Feb 14, 2015
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
6
With the following codes I can connect to HC-05 from computer or android devices either in master mode or slave mode.However I can not make two HC-05 connect to each other.What should I do ? :bang:

here is the code for master mode

Code:
/*
Copyright Chris Samuelson 2012</pre>
*/
 
#include <SoftwareSerial.h> //Software Serial Port
 #define RxD 5 // TxD of BT_Master
 #define TxD 4 // RxD of BT_Master
 #define Reset 3 // Master
 #define PIO11 6 // EN of BT_Master
 #define Led 13 //Master
 #define RTS 2 //Master



 
SoftwareSerial blueToothSerial(RxD,TxD);
 
void setup(){
 Serial.begin(9600);
 pinMode(RxD, INPUT);
 pinMode(TxD, OUTPUT);
 pinMode(PIO11, OUTPUT);
 digitalWrite(PIO11, HIGH);
 pinMode(Reset, OUTPUT);
 digitalWrite(Reset, LOW);
 pinMode(RTS, INPUT);
 setupBlueToothConnection();
 pinMode(Led, OUTPUT);
}
 
void loop(){
 char recvChar;
 boolean rtsStatus = true;
 while(1){
 blueToothSerial.print("a");
 delay(500);
 blueToothSerial.print("b");
 delay(500);
 if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
 recvChar = blueToothSerial.read();
 if(recvChar == 'a'){
 digitalWrite(Led, HIGH);
 }
 if(recvChar == 'b'){
 digitalWrite(Led, LOW);
 }
 Serial.print(recvChar);
 }
 if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
 recvChar = Serial.read();
 blueToothSerial.print(recvChar);
 }
 if(digitalRead(RTS)){
 if(!rtsStatus){
 rtsStatus = true;
 Serial.print("rts changed true\n");
 
 }else{
 if(rtsStatus){
 rtsStatus = false;
 Serial.print("rts changed false\n");
 }
 }
 }
}}
 
void setupBlueToothConnection(){
 enterATMode();
 sendATCommand();
 sendATCommand("ORGL");
 //sendATCommand("RMAAD");
 sendATCommand("CMODE=0");
 sendATCommand("ROLE=1");
 sendATCommand("PSWD=1234");
 //sendATCommand("INIT");
 //sendATCommand("INQ");
 //sendATCommand();
 // sendATCommand("LINK=98D3,31,B44BF9"); // (hc-05-F9) (RX-E2)
 // sendATCommand("UART=9600,0,0");
 // sendATCommand("PAIR=98D3,31,B44BF9");
 // sendATCommand("BIND=98D3,31,B44BF9");
 //enterComMode();

 
}
 
void resetBT(){
 digitalWrite(Reset, LOW);
 delay(2000);
 digitalWrite(Reset, HIGH);
}
 
void enterComMode(){
 blueToothSerial.flush();
 delay(500);
 digitalWrite(PIO11, LOW);
 resetBT();
 delay(500);
 blueToothSerial.begin(9600);
}
 
void enterATMode(){
 blueToothSerial.flush();
 delay(500);
 digitalWrite(PIO11, HIGH);
 resetBT();
 delay(500);
 blueToothSerial.begin(9600);
 
}
 
void sendATCommand(char *command){
 blueToothSerial.print("AT");
 if(strlen(command) > 1){
 blueToothSerial.print("+");
 blueToothSerial.print(command);
 delay(100);
 }
 blueToothSerial.print("\r\n");
}
 
void sendATCommand(){
 blueToothSerial.print("AT\r\n");
 delay(100);
}
//<pre>
this is the code for slave mode
Code:
/*
Copyright Chris Samuelson 2012
*/
 
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 10 // TXD of BT_Slave
#define TxD 9 // RXD of BT_Slave
#define Reset 8 //Slave
#define PIO11 11 // EN of BT_Slave
#define Led 13  //Slave
#define RTS 7 //Slave
 


SoftwareSerial blueToothSerial(RxD,TxD);
 
void setup(){
 Serial.begin(9600);
 pinMode(RxD, INPUT);
 pinMode(TxD, OUTPUT);
 pinMode(PIO11, OUTPUT);
 digitalWrite(PIO11, HIGH);
 pinMode(Reset, OUTPUT);
 digitalWrite(Reset, LOW);
 pinMode(RTS, INPUT);
 setupBlueToothConnection();
 pinMode(Led, OUTPUT);
}
 
void loop(){
 int recvChar;
 
 while(1){
 if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
 recvChar = blueToothSerial.read();
 if(recvChar == '1'){
 digitalWrite(Led, HIGH);
 }
 if(recvChar == '0'){
 digitalWrite(Led, LOW);
 }
 Serial.print(recvChar);
 }
 if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
 recvChar = Serial.read();
 blueToothSerial.print(recvChar);
 }
 }
}
 
void setupBlueToothConnection(){
 enterATMode();
 sendATCommand();
 sendATCommand("ORGL");
 //sendATCommand("UART=9600,0,0");
 sendATCommand("ROLE=0");
 sendATCommand("PSWD=1234");
 sendATCommand("CMODE=1");
 sendATCommand("LINK=98D3,31,B44BF9");
 //sendATCommand("PAIR=98D3,31,B44BE2");
 //sendATCommand("BIND=98D3,31,B44BE2");
}
 
void resetBT(){
 digitalWrite(Reset, LOW);
 delay(2000);
 digitalWrite(Reset, HIGH);
}

void enterComMode(){
 blueToothSerial.flush();
 delay(500);
 digitalWrite(PIO11, LOW);
 resetBT();
 delay(500);
 blueToothSerial.begin(9600);
}
 
void enterATMode(){
 blueToothSerial.flush();
 delay(500);
 digitalWrite(PIO11, HIGH);
 resetBT();
 delay(500);
 blueToothSerial.begin(9600);
 
}
 
void sendATCommand(char *command){
 blueToothSerial.print("AT");
 if(strlen(command) > 1){
 blueToothSerial.print("+");
 blueToothSerial.print(command);
 delay(100);
 }
 blueToothSerial.print("\r\n");
}
 
void sendATCommand(){
 blueToothSerial.print("AT\r\n");
 delay(100);
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…