关于如何与Arduino接线、进入AT模式,请看:http://swf.com.tw/?p=712
这是HC-06所有AT指令(网上参考资料或多或少会漏,这是我找到的比较齐全的)
Command | Response | Comment |
---|---|---|
AT | OK | Used to verify communication 验证 |
AT+VERSION | OKlinvorV1.8 | The firmware version (version might depend on firmware) |
AT+NAMExyz | OKsetname | 设置设备名称 |
AT+PIN1234 | OKsetPIN | 设置PIN码 |
AT+BAUD1 | OK1200 | Sets the baud rate to 1200 波特率 下同 |
AT+BAUD2 | OK2400 | Sets the baud rate to 2400 |
AT+BAUD3 | OK4800 | Sets the baud rate to 4800 |
AT+BAUD4 | OK9600 | Sets the baud rate to 9600 |
AT+BAUD5 | OK19200 | Sets the baud rate to 19200 |
AT+BAUD6 | OK38400 | Sets the baud rate to 38400 |
AT+BAUD7 | OK57600 | Sets the baud rate to 57600 |
AT+BAUD8 | OK115200 | Sets the baud rate to 115200 |
AT+BAUD9 | OK230400 | Sets the baud rate to 230400 |
AT+BAUDA | OK460800 | Sets the baud rate to 460800 |
AT+BAUDB | OK921600 | Sets the baud rate to 921600 |
AT+BAUDC | OK1382400 | Sets the baud rate to 1382400 |
AT+ROLE=M 或 AT+ROLE=S | OK+ROLE:M 或 OK+ROLE:S | 设置模式为Mast或 Slave (注:即使切换为Mast也无法主动连接其他Slave,因为固件不含相关指令 |
下面是2个调试使用的程序:
第一个:
void setup()
{
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
delay(2);
Serial.println("Get Infomation:");
while(Serial.available())
{
Serial.print(char( Serial.read()));
delay(2);
}
Serial.println();
}
}
连线:模块RX、TX分别接板子TX、RX
效果:手机(或其他主机)连接上蓝牙后,发送消息,可在Arduino串口监视器里得到消息。在串口监视器(PC)向Arduino模块发送消息,在手机蓝牙软件也能收到。相当于Arduino板子只是传递消息。
分析:从代码里其实也能看出来,都是Serial.read(print) ,对于Arduino来说,PC和蓝牙模块的接口都是Serial, 所以会把信息直接传过去。。不过还有个小疑问,为何PC传入的消息不会原路输出??不连接蓝牙模块时就显然能输出给PC的。。。
<span style="font-size:24px;">#include <SoftwareSerial.h> // 引用程式庫
// 定義連接藍牙模組的序列埠
SoftwareSerial BT(8, 9); // 接收腳, 傳送腳
char val; // 儲存接收資料的變數
void setup() {
Serial.begin(9600); // 與電腦序列埠連線
Serial.println("BT is ready!");
// 設定藍牙模組的連線速率
// 如果是HC-05,請改成38400
BT.begin(9600);
}
void loop() {
// 若收到「序列埠監控視窗」的資料,則送到藍牙模組
if (Serial.available()) {
val = Serial.read();
BT.print(val);
}
// 若收到藍牙模組的資料,則送到「序列埠監控視窗」
if (BT.available()) {
val = BT.read();
Serial.print(val);
}
}</span>
接线:蓝牙模块RX接9,TX接8
效果:
①通过串口连PC时:可以让电脑发送AT指令作调试用,结果能返回到PC上。
②串口连PC、蓝牙于手机匹配:手机发送的内容能显示在PC上,PC输入内容无效。
③只通过蓝牙连手机:只能发送消息,没看到返回。(是否与调试APP有关?)
关于HC-05\06 配对 请看:http://www.arduino.cn/forum.php?mod=viewthread&tid=2961&highlight=HC05