2
我正在使用HC_05藍牙模塊和Arduino Uno嘗試與我的Windows Phone(HTC 8X)建立簡單的藍牙連接。我正在關注在線教程here。Arduino藍牙模塊連接丟失
當我進入設置,我的手機看到「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
嘗試每寫一個字符,所以你會重置超時。如果這項工作,它真的是一個非常積極的節能政治踢。 – Lesto
它5-10秒後與我的手機和我的電腦斷開連接。這讓我覺得它在Arduino方面,更有可能它沒有傳輸任何東西。我有'Serial.println(「test」);'在我的'loop'函數中,串口監視器顯示出來很好。還有什麼我應該做的? – JcKelley
Arduino的代碼和連接? – Lesto