This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHM_13_SW.ino
210 lines (184 loc) · 6.1 KB
/
HM_13_SW.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Bluetooth HM13 Demo Code
2014 Copyright (c) Seeed Technology Inc. All right reserved.
Author: Jacky Zhang
This demo code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
For more details about the product please check http://www.seeedstudio.com/depot/
*/
/* Upload this sketch into Arduino Uno and press reset*/
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 2
#define TxD 3
#define MASTER 1 //change this macro to define the Bluetooth as Master or not
SoftwareSerial blueToothSerial(RxD,TxD);//the software serial port
char recv_str[100];
void setup()
{
Serial.begin(115200); //Serial port for debugging
pinMode(RxD, INPUT); //UART pin for Bluetooth
pinMode(TxD, OUTPUT); //UART pin for Bluetooth
Serial.println("\r\nPower on!!");
if(setupBlueToothConnection() != 0) while(1); //initialize Bluetooth
//this block is waiting for connection was established.
while(1)
{
if(recvMsg(1000) == 0)
{
if(strcmp((char *)recv_str, (char *)"OK+CONB") == 0)
{
Serial.println("connected\r\n");
break;
}
}
delay(200);
}
}
void loop()
{
#if MASTER //central role
//in master mode, the bluetooth send message periodically.
delay(400);
Serial.println("send: hi");
blueToothSerial.print("hi");
delay(100);
//get any message to print
if(recvMsg(1000) == 0)
{
Serial.print("recv: ");
Serial.print((char *)recv_str);
Serial.println("");
}
#else //peripheral role
delay(200);
//the slave role only send message when received one.
if(recvMsg(1000) == 0)
{
Serial.print("recv: ");
Serial.print((char *)recv_str);
Serial.println("");
Serial.println("send: hello");
blueToothSerial.print("hello");//return back message
}
#endif
}
//used for compare two string, return 0 if one equals to each other
int strcmp(char *a, char *b)
{
unsigned int ptr = 0;
while(a[ptr] != '\0')
{
if(a[ptr] != b[ptr]) return -1;
ptr++;
}
return 0;
}
//configure the Bluetooth through AT commands
int setupBlueToothConnection()
{
#if MASTER
Serial.println("this is MASTER\r\n");
#else
Serial.println("this is SLAVE\r\n");
#endif
Serial.print("Setting up Bluetooth link\r\n");
delay(3500);//wait for module restart
//send command to module in different baud rate
while(1)
{
delay(500);
blueToothSerial.begin(9600);
delay(500);
Serial.print("try 9600\r\n");
if(sendBlueToothCommand("AT") == 0)
break;
delay(500);
blueToothSerial.begin(115200);
delay(500);
Serial.print("try 115200\r\n");
if(sendBlueToothCommand("AT") == 0)
break;
}
//we have to set the baud rate to 9600, since the soft serial is not stable at 115200
sendBlueToothCommand("AT+RENEW");//restore factory configurations
sendBlueToothCommand("AT+BAUD2");//reset the module's baud rate
sendBlueToothCommand("AT+AUTH1");//enable authentication
sendBlueToothCommand("AT+RESET");//restart module to take effect
blueToothSerial.begin(9600);//reset the Arduino's baud rate
delay(3500);//wait for module restart
//configure parameters of the module
sendBlueToothCommand("AT+VERS?");//get firmware version
sendBlueToothCommand("AT+ADDE?");//get EDR MAC
sendBlueToothCommand("AT+ADDB?");//get BLE MAC
sendBlueToothCommand("AT+NAMEHM-13-EDR");//set EDR name
sendBlueToothCommand("AT+NAMBHM-13-BLE");//set BLE name
sendBlueToothCommand("AT+PINE123451");//set EDR password
sendBlueToothCommand("AT+PINB123451");//set BLE password
sendBlueToothCommand("AT+SCAN0");//set module visible
sendBlueToothCommand("AT+NOTI1");//enable connect notifications
//sendBlueToothCommand("AT+NOTP1");//enable address notifications
sendBlueToothCommand("AT+PIO01");//enable key function
#if MASTER
sendBlueToothCommand("AT+ROLB1");//set to master mode
#else
sendBlueToothCommand("AT+ROLB0");//set to slave mode
#endif
sendBlueToothCommand("AT+RESET");//restart module to take effect
delay(3500);//wait for module restart
if(sendBlueToothCommand("AT") != 0) return -1;//detect if the module exists
Serial.print("Setup complete\r\n\r\n");
return 0;
}
//send command to Bluetooth and return if there is a response
int sendBlueToothCommand(char command[])
{
Serial.print("send: ");
Serial.print(command);
Serial.println("");
blueToothSerial.print(command);
delay(200);
if(recvMsg(200) != 0) return -1;
Serial.print("recv: ");
Serial.print(recv_str);
Serial.println("");
return 0;
}
//receive message from Bluetooth with time out
int recvMsg(unsigned int timeout)
{
//wait for feedback
unsigned int time = 0;
unsigned char num;
unsigned char i;
//waiting for the first character with time out
i = 0;
while(1)
{
delay(50);
if(blueToothSerial.available())
{
recv_str[i] = char(blueToothSerial.read());
i++;
break;
}
time++;
if(time > (timeout / 50)) return -1;
}
//read other characters from uart buffer to string
while(blueToothSerial.available() && (i < 100))
{
recv_str[i] = char(blueToothSerial.read());
i++;
}
recv_str[i] = '\0';
return 0;
}