I am connecting one master hc-05 to two slaves
one of the slaves (at+link) response is always fail
and the other slave sometimes fail sometimes not.
master code
/*
This code is in the public domain.
written by Itamar Eduardo Gonçalves de Oliveira
*/
#include <SoftwareSerial.h>
#define bt_power 3
#define bt_key_power 2
SoftwareSerial BT(10, 11); // RX | TX
void setup()
{
// set the pins to OUTPUT
pinMode(bt_power, OUTPUT);
pinMode(bt_key_power, OUTPUT);
// set the pins to LOW
digitalWrite(bt_power, LOW);
digitalWrite(bt_key_power, LOW);
/************************************************
Setting the pins to low is important because
in order for us to get into AT mode the key pin
has to be set to Ground FIRST. Many tutorials out
there fail to mention this important fact and
therefore many people have problems with getting
into the AT mode of the HC-05
************************************************/
// make sure the key has been LOW for a bit
delay(100);
// set the key pin to High
digitalWrite(bt_key_power, HIGH);
// small delay
delay(100);
// now power on the BT
digitalWrite(bt_power, HIGH);
// start our serial so we can send and recieve
// information from the BT module
Serial.begin(9600);
// initiate the BT serial at 38400 which is the default
// speed at which the BT AT mode operates at
BT.begin(38400);
// self explanatory
Serial.write("For a list of commands, visit: \n");
Serial.write("Type AT commands \n\n");
// Send an "AT" command to the AT (without quotes)
// if response is OK, then we are connected
// and ready to program the BT module
delay(3000);
BT.write("AT+ROLE=1\r\n");
Serial.write("AT+ROLE=1\r\n");
delay(1000);
BT.write("AT+CMODE=1\r\n");
Serial.write("AT+CMODE=1\r\n");
delay(1000);
}
void loop()
{
BT.write("AT+BIND=98d3,32,71057e\r\n");
Serial.write("AT+BIND=98d3,32,71057e\r\n");
delay(2000);
while (BT.available())
{ Serial.println(BT.readString());}
BT.write("AT+INIT\r\n");
Serial.write("AT+INIT\r\n");
delay(3000);
while (BT.available())
{ Serial.println(BT.readString());}
BT.write("AT+INQ\r\n");
Serial.write("AT+INQ\r\n");
delay(4000);
while (BT.available())
{ Serial.println(BT.readString());}
BT.write("AT+LINK=98d3,32,71057e\r\n");
Serial.write("AT+LINK=98d3,32,71057e\r\n");
delay(5000);///////////////////////////////////////////////////////////////////////////////////
while (BT.available())
{ Serial.println(BT.readString());}
BT.write("a");
delay(2000);
BT.write("b");
delay(4000);
digitalWrite(bt_power, LOW);
digitalWrite(bt_key_power, LOW);
delay(100);
digitalWrite(bt_key_power, HIGH);
delay(100);
digitalWrite(bt_power, HIGH);
Serial.write("Desconectando\r\n");
delay(4000);
BT.write("AT+BIND=98d3,32,710530\r\n");
Serial.write("AT+BIND=98d3,32,710530\r\n");
delay(2000);
while (BT.available())
{ Serial.println(BT.readString());}
BT.write("AT+INIT\r\n");
Serial.write("AT+INIT\r\n");
delay(3000);
while (BT.available())
{ Serial.println(BT.readString());}
BT.write("AT+INQ\r\n");
Serial.write("AT+INQ\r\n");
delay(5000);////////////////////////////////////////////////
while (BT.available())
{ Serial.println(BT.readString());}
BT.write("AT+LINK=98d3,32,710530\r\n");
Serial.write("AT+LINK=98d3,32,710530\r\n");
delay(4000);
while (BT.available()){
Serial.println(BT.readString());}
BT.write("a");
delay(2000);
BT.write("b");
delay(4000);
digitalWrite(bt_power, LOW);
digitalWrite(bt_key_power, LOW);
delay(100);
digitalWrite(bt_key_power, HIGH);
delay(100);
digitalWrite(bt_power, HIGH);
Serial.write("Desconectando\r\n");
delay(4000);
// listen for a response from the HC-05 and write it to the serial monitor
if (BT.available())
Serial.write(BT.read());
// listen for user input and send it to the HC-05
if (Serial.available())
BT.write(Serial.read());
}
slave code
#include <SoftwareSerial.h>
//defining bluetooth pins
#define bt_power 12
#define bt_key_power 9
SoftwareSerial BT(5, 6); // RX | TX
//defining pin numbers
const int vib = 2;
//ultrasonic sensor
const int trigPin = 7;
const int echoPin = 8;
//defining variables
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(vib, OUTPUT);
/////////////////////////////////////////////////////////
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT_PULLUP); // Sets the echoPin as an INput
/////////////////////////////////////////////////////////
// set the pins to OUTPUT
pinMode(bt_power, OUTPUT);
pinMode(bt_key_power, OUTPUT);
// set the pins to LOW
digitalWrite(bt_power, LOW);
digitalWrite(bt_key_power, LOW);
//////////////////////////////////////////////////////////
// make sure the key has been LOW for a bit
delay(100);
// set the key pin to High
digitalWrite(bt_key_power, HIGH);
// small delay
delay(100);
// now power on the BT
digitalWrite(bt_power, HIGH);
// start our serial so we can send and recieve
// information from the BT module
Serial.begin(9600);
// initiate the BT serial at 38400 which is the default
// speed at which the BT AT mode operates at
BT.begin(38400);
// self explanatory
Serial.write("For a list of commands, visit: \n");
Serial.write("Type AT commands \n\n");
// Send an "AT" command to the AT (without quotes)
// if response is OK, then we are connected
// and ready to program the BT module
delay(3000);
BT.write("AT+ROLE=0\r\n");
Serial.write("AT+ROLE=0\r\n");
delay(1000);
// Serial.print(BT.readString());
BT.write("AT+CMODE=1\r\n");
Serial.write("AT+CMODE=1\r\n");
delay(1000);
BT.write("AT+INIT\r\n");
Serial.write("AT+INIT\r\n");
delay(3000);
BT.write("AT+INQ\r\n");
Serial.write("AT+INQ\r\n");
delay(3000);
Serial.println("initial setup done");
while(BT.available()){
Serial.print(BT.readString());}
}
void loop() {
// Write a pulse to the HC-SR04 Trigger Pin
digitalWrite(trigPin, LOW);// Clears the trigPin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds to generate the ultrasound wave
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the response from the HC-SR04 Echo Pin
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Determine distance from duration// Calculating the distance
// Use 343 metres per second as speed of sound
distance = (duration / 2) * 0.0343;
// Prints the distance on the Serial Monitor
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
safetyDistance = distance;
if (distance <= 30){
digitalWrite(vib, HIGH);delay(1000);digitalWrite(vib, LOW); }
else{
digitalWrite(vib, LOW);}
Serial.print("\n");
delay(1000);
Serial.println(digitalRead(echoPin));
// listen for a response from the HC-05 and write it to the serial monitor
while(BT.available()){
char te = BT.read();
Serial.write(te);
/*
if(te=='a'){
digitalWrite(vib,HIGH);
}
else if(te=='b'){
digitalWrite(vib,LOW);*/
while(BT.available()){
Serial.print(BT.readString());Serial.println("\n");}
if((digitalRead(echoPin) == HIGH)||(distance==0))
{
Serial.println("Sensor is faulty");
BT.write('z');BT.flush();
}
}}
please help me