1 | /*
|
2 | BluetoothShield Demo Code Slave.pde. This sketch could be used with
|
3 | Master.pde to establish connection between two Arduino. It can also
|
4 | be used for one slave bluetooth connected by the device(PC/Smart Phone)
|
5 | with bluetooth function.
|
6 | 2011 Copyright (c) Seeed Technology Inc. All right reserved.
|
7 |
|
8 | Author: Steve Chang
|
9 |
|
10 | This demo code is free software; you can redistribute it and/or
|
11 | modify it under the terms of the GNU Lesser General Public
|
12 | License as published by the Free Software Foundation; either
|
13 | version 2.1 of the License, or (at your option) any later version.
|
14 |
|
15 | This library is distributed in the hope that it will be useful,
|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
18 | Lesser General Public License for more details.
|
19 |
|
20 | You should have received a copy of the GNU Lesser General Public
|
21 | License along with this library; if not, write to the Free Software
|
22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
23 |
|
24 |
|
25 | */
|
26 |
|
27 |
|
28 | /* Upload this sketch into Seeeduino and press reset*/
|
29 |
|
30 | #include <SoftwareSerial.h> //Software Serial Port
|
31 | #define RxD 0
|
32 | #define TxD 1
|
33 |
|
34 | #define DEBUG_ENABLED 1
|
35 |
|
36 | SoftwareSerial blueToothSerial(RxD,TxD);
|
37 |
|
38 | void setup()
|
39 | {
|
40 | Serial.begin(115200);
|
41 | pinMode(RxD, INPUT);
|
42 | pinMode(TxD, OUTPUT);
|
43 | setupBlueToothConnection();
|
44 |
|
45 | }
|
46 |
|
47 | void loop()
|
48 | {
|
49 | char recvChar;
|
50 | while(1){
|
51 | if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
|
52 | recvChar = blueToothSerial.read();
|
53 | Serial.print(recvChar);
|
54 | }
|
55 | if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
|
56 | recvChar = Serial.read();
|
57 | blueToothSerial.print(recvChar);
|
58 | }
|
59 | }
|
60 | }
|
61 |
|
62 | void setupBlueToothConnection()
|
63 | {
|
64 | blueToothSerial.begin(115200); //Set BluetoothBee BaudRate to default baud rate 38400
|
65 | blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
|
66 | blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
|
67 | blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
|
68 | blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
|
69 | delay(2000); // This delay is required.
|
70 | blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
|
71 | Serial.println("The slave bluetooth is inquirable!");
|
72 | delay(2000); // This delay is required.
|
73 | blueToothSerial.flush();
|
74 | }
|