Bleutoothcommunication

hello all,

iam working with microduino BT i want a master to get data from multiple Bluetooth device.my master code is below. the program is complied without any errors, but when i power the master, the just display "Master is inquiring! and nothing more than tat. if anyone could help please.

also can anyone plese tell me how to program the slaves to send data to a master

mastercode:

MASTER CODE

#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7

String retSymb = “+RTINQ=”;//start symble when there’s any return
String slaveName = “;SeeedBTSlave”;//Set the Slave name ,caution that ';'mustbe included
int nameIndex = 0;

int addrIndex = 0;
String recvBuf;
String slaveAddr;
String connectCmd = “\r\n+CONN=”;
SoftwareSerial blueToothSerial(6,7);
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
//wait 1s and flush the serial buffer
delay(1000);
Serial.flush();
blueToothSerial.flush();
}
void loop()
{
char recvChar;
while(1){
if(blueToothSerial.available()){//check if there’s any data sent from theremote bluetooth shield
recvChar = blueToothSerial.read();
Serial.print(recvChar);
}
if(Serial.available()){//check if there’s any data sent from the localserial terminal, you can add the other applications here
recvChar = Serial.read();
blueToothSerial.print(recvChar);
}
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baudrate 38400
blueToothSerial.print("\r\n+STWMOD=1\r\n");//set the bluetooth work inmaster mode
blueToothSerial.print("\r\n+STNA=SeeedBTMaster\r\n");//set the bluetoothname as “SeeedBTMaster”
blueToothSerial.print("\r\n+STPIN=0000\r\n");//Set Master pincode"0000",itmust be same as Slave pincode
blueToothSerial.print("\r\n+STAUTO=0\r\n");// Auto-connection is forbiddenhere
delay(2000); // This delay is required.
blueToothSerial.flush();
blueToothSerial.print("\r\n+INQ=1\r\n");//make the master inquire
Serial.println(“Master is inquiring!”);

delay(2000); // This delay is required.

char recvChar;
while(1){
if(blueToothSerial.available()){
recvChar = blueToothSerial.read();
recvBuf += recvChar;
nameIndex = recvBuf.indexOf(slaveName);//get the position of slave name
//nameIndex -= 1;//decrease the ‘;’ in front of the slave name, to getthe position of the end of the slave address
if ( nameIndex != -1 ){
//Serial.print(recvBuf);
addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()-
18) ) + retSymb.length());//get the start position of slave address"

slaveAddr = recvBuf.substring(addrIndex, nameIndex);//get the string ofslave address
break;
}
}
}
//form the full connection command
connectCmd += slaveAddr;
connectCmd += “\r\n”;
int connectOK = 0;
Serial.print(“Connecting to slave:”);
Serial.print(slaveAddr);
Serial.println(slaveName);
//connecting the slave till they are connected
do{
blueToothSerial.print(connectCmd);//send connection command
recvBuf = “”;
while(1){
if(blueToothSerial.available()){
recvChar = blueToothSerial.read();
recvBuf += recvChar;
if(recvBuf.indexOf(“CONNECT:OK”) != -1){
connectOK = 1;
Serial.println(“Connected!”);
blueToothSerial.print(“Connected!”);
break;
}else if(recvBuf.indexOf(“CONNECT:FAIL”) != -1){
Serial.println(“Connect again!”); break;
}
}
}
}while(0 == connectOK);
}

SLAVE CODE

#include <SoftwareSerial.h> //Software Serial Port
SoftwareSerial blueToothSerial(4, 5);

void setup()
{
Serial.begin(9600);
pinMode(4, INPUT);
pinMode(5, OUTPUT);
setupBlueToothConnection();
}
void loop()
{
char recvChar;
while(1){
if(blueToothSerial.available()){//check if there’s anydata sent
recvChar = blueToothSerial.read();
Serial.print(recvChar);
}
if(Serial.available()){//check if there’s any data sentfrom the local serial terminal, you can add the other applications here
recvChar = Serial.read();
blueToothSerial.print(recvChar);
}
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(9600);
blueToothSerial.print("\r\n+ROLE\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+NAMESlave\r\n"); //set the bluetooth name as “Slave”

blueToothSerial.print("\r\n+STOAUT1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO0\r\n"); // Autoconnection cannot happen here
delay(1000); // This delay is required.
blueToothSerial.print("\r\n+INQ1\r\n"); //make the slave bluetooth inquirable
Serial.println(“The slave bluetooth is inquirable!”);
delay(2000); // This delay is required.
blueToothSerial.flush();
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long to study quickly without copying to a text editor.

…R

MASTER MODE
#include <SoftwareSerial.h> //Software Serial Port

String retSymb = "+RTINQ=";//start symble when there's any return
String slaveName = ";Slave";//Set the Slave name ,caution that ';'must be included
//int nameIndex = 0;

//int addrIndex = 0;
String indata;
String slaveAddr;
String connectCmd = "\r\n+CONN=";//intiate a connection
SoftwareSerial blueToothSerial(4,5);
void setup()
{
 Serial.begin(9600);
 pinMode(4, INPUT);
 pinMode(5, OUTPUT);
 setupBlueToothConnection();
 
 //wait 1s and flush the serial buffer
 delay(1000);
 Serial.flush();
 blueToothSerial.flush();
}
void loop()
{
 char recived;
 while(1){
 if(blueToothSerial.available()){ //check if there's any data sent from the  bluetooth 
 recived = blueToothSerial.read();
 Serial.print(recived);
 }
 if(Serial.available()){
recived = Serial.read();
 blueToothSerial.print(recived);
 }
}
}
void setupBlueToothConnection()//starts the AT connection mode
{
 blueToothSerial.begin(9600); //
 blueToothSerial.print("\r\n+ROLE1\r\n");//set the bluetooth work in master mode
 blueToothSerial.print("\r\n+NAMEBTMaster\r\n");//set the bluetooth name as "BTMaster"
 blueToothSerial.print("\r\n+STAUT0\r\n");// Auto-connection cannot be done
 delay(1000); // This delay is required.
 blueToothSerial.flush();
 blueToothSerial.print("\r\n+INQ1\r\n");//make the master inquire
 Serial.println("Master is inquiring!");
 delay(1000);
 Serial.println("SLAVE1");// This delay is required.

 char recived;
 while(1){
 if(blueToothSerial.available()){
 recived = blueToothSerial.read();
 indata += recived;
 //nameIndex = recvBuf.indexOf(";Slave");//get the position of slave name
 //nameIndex -= 1;//decrease the ';' in front of the slave name, to get the position of the end of the slave address
 if ( recived == '\n' ){
 //Serial.print(recvBuf);
//addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()-
//18) ) + retSymb.length());//get the start position of slave address

//slaveAddr = recvBuf.substring(addrIndex, nameIndex);//get the string of slave Add
}
 }
}
//form the full connection command
 //connectCmd += slaveAddr;
 //connectCmd += "\r\n";//AT MODE
 int connectOK = 0;
 Serial.print("Connecting to slave:");
 //Serial.print(slaveAddr);
 //Serial.println(slaveName);
 //connecting the slave till they are connected
 do{
 blueToothSerial.print(connectCmd);//send connection command
indata = "";
 while(1){
 if(blueToothSerial.available()){
 recived = blueToothSerial.read();
indata += recived;
if(indata.indexOf("CONNECT:OK") != -1){
 connectOK = 1;
 Serial.println("Connected!");
 blueToothSerial.print("Connected!");
 Serial.println(indata);
 break;
}else 
if(indata.indexOf("CONNECT:FAIL") != -1){
 Serial.println("Connect again!");
 break;
}
}
}
}while(0 == connectOK);

 }
SLAVE MODE

#include <SoftwareSerial.h> //Software Serial Port
SoftwareSerial blueToothSerial(4, 5);

void setup()
{
Serial.begin(9600);
pinMode(4, INPUT);
pinMode(5, OUTPUT);
setupBlueToothConnection();
}
void loop()
{
char recvChar;
while(1){
if(blueToothSerial.available()){//check if there's anydata sent 
 recvChar = blueToothSerial.read();
Serial.print(recvChar);
}
if(Serial.available()){//check if there's any data sentfrom the local serial terminal, you can add the other applications here
 recvChar = Serial.read();
 blueToothSerial.print(recvChar);
}
}
}
void setupBlueToothConnection()
{
 blueToothSerial.begin(9600); 
 blueToothSerial.print("\r\n+ROLE\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+NAMESlave\r\n"); //set the bluetooth name as "Slave"

 blueToothSerial.print("\r\n+STOAUT1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO0\r\n"); // Autoconnection cannot happen here
delay(1000); // This delay is required.
 blueToothSerial.print("\r\n+INQ1\r\n"); //make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
 blueToothSerial.flush();
}

I'm not familiar with the "microduino BT" perhaps you can post a link to its datasheet?

Is this code you have written your self or is it an example you have got from the internet - if the latter please provide a link to the source.

You say

the just display "Master is inquiring! and nothing more than tat

but I think it should also print the data from line 50

Serial.println("SLAVE1");

If you use the AutoFormat tool it will indent your code and make it much easier to read.

...R