关于蓝牙模块(HC-06)

关于如何与Arduino接线、进入AT模式,请看:http://swf.com.tw/?p=712


这是HC-06所有AT指令(网上参考资料或多或少会漏,这是我找到的比较齐全的)

CommandResponseComment
ATOKUsed to verify communication  验证
AT+VERSIONOKlinvorV1.8The firmware version (version might depend on firmware)
AT+NAMExyzOKsetname设置设备名称
AT+PIN1234OKsetPIN设置PIN码
AT+BAUD1OK1200Sets the baud rate to 1200 波特率 下同
AT+BAUD2OK2400Sets the baud rate to 2400
AT+BAUD3OK4800Sets the baud rate to 4800
AT+BAUD4OK9600Sets the baud rate to 9600
AT+BAUD5OK19200Sets the baud rate to 19200
AT+BAUD6OK38400Sets the baud rate to 38400
AT+BAUD7OK57600Sets the baud rate to 57600
AT+BAUD8OK115200Sets the baud rate to 115200
AT+BAUD9OK230400Sets the baud rate to 230400
AT+BAUDAOK460800Sets the baud rate to 460800
AT+BAUDBOK921600Sets the baud rate to 921600
AT+BAUDCOK1382400Sets the baud rate to 1382400
AT+ROLE=M 或 AT+ROLE=SOK+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


  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值