2014-03-04 128 views
2

我正在使用HC_05藍牙模塊和Arduino Uno嘗試與我的Windows Phone(HTC 8X)建立簡單的藍牙連接。我正在關注在線教程hereArduino藍牙模塊連接丟失

當我進入設置,我的手機看到「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

嘗試每寫一個字符,所以你會重置超時。如果這項工作,它真的是一個非常積極的節能政治踢。 – Lesto

+0

它5-10秒後與我的手機和我的電腦斷開連接。這讓我覺得它在Arduino方面,更有可能它沒有傳輸任何東西。我有'Serial.println(「test」);'在我的'loop'函數中,串口監視器顯示出來很好。還有什麼我應該做的? – JcKelley

+0

Arduino的代碼和連接? – Lesto

回答

1

您與Arduino的直接與TX-Rx線路連接HC-05。 HC-05應該在3.3伏而不是5伏驅動。 您的Arduino Tx引腳將在HC-05的Rx線上提供5v信號,該線在推薦電平以上。嘗試在Arduino Tx引腳(5v)和HC-05 Rx引腳(3.3v)之間放置一個電壓轉換器。

也許內部有一個保護功能,當電壓高於3.3時,HC-05內部的微控制器復位。