我正在尝试将HC-05与Arduino UNO一起用于我正在进行的可穿戴设备项目。我想通过不要求用户更改硬件以进入AT模式,而不必让他们必须输入AT命令来实现“自动化”连接。为此,我使用了在github:https://github.com/jdunmire/HC05上找到的HC-05库。最终,我将要搜索设备(AT + INQ),将Bluetooth地址存储为字符串,并检查与每个地址相对应的每个设备的名称。然后,我将连接到具有特定名称的服务器。
我遇到了很多问题,所以我认为我将从最常见的问题开始。
当尝试运行许多不同的命令(AT + ORGL,AT + INIT,AT + INQM,AT + INQ,AT + UART只是为了列举一些最常见的中断命令)时,我常常会得到响应“超时” 1”而不是“ OK”。
这种情况经常发生,我不确定为什么...有时功能会超时,我会打开和关闭设备,然后突然它们会再次起作用(反之亦然)。
下面是我编写的简单代码,试图使用“ AT + ORGL”复制结果,而您知道什么,相同的结果...
#include "HC05.h"
#ifdef HC05_SOFTWARE_SERIAL
#include <SoftwareSerial.h>
HC05 btSerial = HC05(A2, A5, A3, A4); // cmd, state, rx, tx
#else
HC05 btSerial = HC05(3, 2); // cmd, state
#endif
#ifdef DEBUG_HC05
#ifdef DEBUG_SW_PORT
extern SoftwareSerial DEBUG_PORT; //This will allow me to print to serial without sending to BT
#endif
#endif
void setup() {
DEBUG_BEGIN(38400);
DEBUG_PRINTLN("Setup");
delay(3000); // this delay is for debugging convenience only
DEBUG_PRINTLN("DelayComplete");
btSerial.findBaud();
btSerial.cmd("AT"); //Let's test to make sure the KEY pin is connected well
btSerial.cmd("AT+ORGL");
}
void loop() {
}
这是串行监视器中的结果:
Setup
DelayComplete
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... x
Trying 38400... Found.
AT
OK
AT+ORGL
timeout 1
Here is a picture of my Fritzing
接线是所有库示例中使用的接线。 似乎错误“超时1”在HC05.cpp库中的此处编码:
int HC05::cmd(const char* cmd, unsigned long timeout)
{
int recvd = 0;
DEBUG_PRINTLN(cmd);
setCmdPin(HIGH);
// No spec for how long it takes to enter command mode, but 100ms
// seems to work- assuming the output has been drained.
delay(100);
_btSerial.write(cmd);
_btSerial.write("\r\n");
_btSerial.setTimeout(timeout);
do
{
// ATTENTION: At least through Arduino v1.0.3, it is not possible
// to tell the difference between a timeout and
// receiving only the termination character (NL in this
// case), because the termination character is not
// returned and timeout is not returned as a unique
// indication.
// In this case the result would be an early return
// of a multiline response before the OK is received.
// The return would incorrectly indicate an error (no
// OK response).
recvd = _btSerial.readBytesUntil('\n',_buffer,_bufsize);
if (recvd > 0)
{
DEBUG_WRITE((uint8_t *)_buffer,recvd);
DEBUG_WRITE('\n');
}
else
{
DEBUG_PRINTLN("timeout 1");
}
}
while ((recvd > 0) && (_buffer[0] != 'O' || _buffer[1] != 'K'));
setCmdPin(LOW);
// Empirically determined that it takes some time to reliably exit
// command mode. The appeared to be a baud rate dependency and with
// >100ms required at 9600 baud.
delay(150);
return((_buffer[0] == 'O' && _buffer[1] == 'K'));}
}
我不确定如何解决错误。 任何建议都非常感谢!
我相信这是我正在使用的HC-05:http://www.dsdtech-global.com/2017/07/dsd-tech-hc-06-wireless-bluetooth.html