Skip to content

Commit

Permalink
reset Demo code for bluetooth sheild V1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobyan committed Jul 29, 2014
1 parent c19d6a1 commit e33ae04
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 62 deletions.
97 changes: 73 additions & 24 deletions examples/Master_button/Master_button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#define DEBUG_ENABLED 1

String retSymb = "+RTINQ="; // start symble when there's any return
String slaveName = ";SeeedBTSlave"; // caution that ';'must be included, and make sure the slave name is right.

int nameIndex = 0;
int addrIndex = 0;

String recvBuf;
String slaveAddr;

String connectCmd = "\r\n+CONN=";

SoftwareSerial blueToothSerial(RxD,TxD);

Expand Down Expand Up @@ -78,29 +87,69 @@ void loop()

void setupBlueToothConnection()
{


blueToothSerial.begin(9600);

blueToothSerial.print("AT");
delay(400);

blueToothSerial.print("AT+NAMESeeedMaster"); // set the bluetooth name as "SeeedMaster" ,the length of bluetooth name must less than 12 characters.
delay(400);

blueToothSerial.print("AT+ROLEM"); // set the bluetooth work in slave mode
delay(400);

blueToothSerial.print("AT+PIN0000"); // set the pair code to connect
delay(400);

blueToothSerial.print("AT+AUTH1"); // Auto-connection should be forbidden here
delay(400);

blueToothSerial.print("AT+SEARCH"); // Auto-search
delay(400);

blueToothSerial.begin(38400); // Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=1\r\n"); // set the bluetooth work in master mode
blueToothSerial.print("\r\n+STNA=SeeedBTMaster\r\n"); // set the bluetooth name as "SeeedBTMaster"
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection is forbidden here
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.

//find the target slave
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 get the 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 of slave 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);
}
26 changes: 9 additions & 17 deletions examples/Slave_led/Slave_led.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,14 @@ void loop()

void setupBlueToothConnection()
{
blueToothSerial.begin(9600);

blueToothSerial.print("AT");
delay(400);

blueToothSerial.print("AT+NAMESeeedBTSlave"); // set the bluetooth name as "SeeedBTSlave" ,the length of bluetooth name must less than 12 characters.
delay(400);

blueToothSerial.print("AT+ROLES"); // set the bluetooth work in slave mode
delay(400);

blueToothSerial.print("AT+PIN0000"); // set the pair code to connect
delay(400);

blueToothSerial.print("AT+AUTH1"); // Auto-connection should be forbidden here
delay(400);

blueToothSerial.begin(38400); // Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); // set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); // set the bluetooth name as "SeeedBTSlave"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); // make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}
34 changes: 13 additions & 21 deletions examples/Slave_temperature/Slave_temperature.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ For more details about the product please check http://www.seeedstudio.com/depot
/* Upload this sketch into Seeeduino and press reset*/

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

#define DEBUG_ENABLED 1

Expand Down Expand Up @@ -77,7 +77,7 @@ void loop()
}
}
if(Serial.available())
{//check if there's any data sent from t he local serial terminal, you can add the other applications here
{//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);
}
Expand All @@ -89,23 +89,15 @@ void loop()


void setupBlueToothConnection()
{
blueToothSerial.begin(9600);

blueToothSerial.print("AT");
delay(400);

blueToothSerial.print("AT+NAMESeeedBTSlave"); // set the bluetooth name as "SeeedBTSlave" ,the length of bluetooth name must less than 12 characters.
delay(400);

blueToothSerial.print("AT+ROLES"); // set the bluetooth work in slave mode
delay(400);

blueToothSerial.print("AT+PIN0000"); // set the pair code to connect
delay(400);

blueToothSerial.print("AT+AUTH1"); // Auto-connection should be forbidden here
delay(400);

{
blueToothSerial.begin(38400); // Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); // set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); // set the bluetooth name as "SeeedBTSlave"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); // make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}

0 comments on commit e33ae04

Please sign in to comment.