我正在使用HC_05蓝牙模块和Arduino Uno尝试与我的Windows Phone(HTC 8X)建立简单的蓝牙连接。我正在关注在线教程here。
当我进入设置时,我的手机会看到“HC_05”蓝牙信号。我点击它,它立即连接。 保持连接5-10秒,然后突然断开连接。
我想我的手机没有从蓝牙模块接收任何数据,因此决定信号毫无价值,然后丢弃它。但即便如此,为什么呢?当我从链接代码中调用btSerial.read()
时,是不是在与设备通话?
Arduino代码:
#include <SoftwareSerial.h>
const int TX_BT = 10;
const int RX_BT = 11;
SoftwareSerial btSerial(TX_BT, RX_BT);
//Frequency to send periodic messages to Windows Phone, in milliseconds.
//Core code.
const unsigned long periodicMessageFrequency = 5000;
unsigned long time = 0;
//Process the incoming command from Windows Phone.
//It should be changed according to what you want to do.
void processCommand(char* command) {
}
//Send a message back to the Windows Phone.
//Is can't be changed.
void sendMessage(char* message) {
int messageLen = strlen(message);
if(messageLen < 256) {
btSerial.write(messageLen);
btSerial.print(message);
}
}
//Send a set of periodic messages to the Windows Phone.
//It should be changed according to what you want to do.
//This message could be a sensor data, like a thermometer data.
void sendPeriodicMessages() {
}
//Setup Arduino function
void setup() {
Serial.begin(9600);
Serial.println("USB Connected");
btSerial.begin(9600);
}
//Loop Arduino function
//It can't be changed
void loop() {
if(btSerial.available()) {
int commandSize = (int)btSerial.read();
char command[commandSize];
int commandPos = 0;
while(commandPos < commandSize) {
if(btSerial.available()) {
command[commandPos] = (char)btSerial.read();
commandPos++;
}
}
command[commandPos] = 0;
processCommand(command);
}
unsigned long currentTime = millis();
if((currentTime - time) > periodicMessageFrequency) {
sendPeriodicMessages();
time = currentTime;
}
}
HC_05连接如下:
GND -> GND
3.3V -> 3.3V
RX -> D11
TX-> D10
答案 0 :(得分:1)
您可以将HC-05与Arduino直接连接到Tx-Rx线。 HC-05应该在3.3v而不是5伏驱动。 您的Arduino Tx引脚将在HC-05的Rx线上提供5v信号,该信号高于推荐的电平。尝试在Arduino Tx引脚(5v)和HC-05 Rx引脚(3.3v)之间放置一个电压转换器。
当电压超过3.3时,可能内部有一个保护功能可以重置HC-05内部的微控制器。