程式設計可以改變您的未來(Programming can change your future)。
雲林SONG 全名為雲林軟體工程(SOftware eNGineering),目標致力於軟體人才的培養並推廣開源軟體落實於資訊教育。程式設計的觀念是軟體產品的基礎,程式碼就像沙子一樣,要紮實,所建立出來的高塔才會穩固。本站也提供資訊教育相關的教學資源。
YunlinSONG stands for Yunlin SOftware eNGineering, offering tutorial for computer programming and promoting open-source software.
Teaching resources in information technology education are provided here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
一、前言 (Preface)
在製作 Arduino 藍牙遙控器之前,我們先來了解藍牙的使用方式,以手機連接HC-06藍牙模組流程約有底下幾個步驟:
Before building a customized bluetooth controller, there are several steps we need to know about using bluetooth communication. For example, the following steps are for connecting HC-06 with a smart phone:
開啟手機藍牙。 (Turn on bluetooth on a smart phone)
尋找 HC-06 藍牙模組。(Scan for the nearby bluetooth devices)
與 HC-06 藍牙模組配對。(Paire HC-06 with the smart phone)
開啟手機藍牙App。(Open a BT application on the smart phone)
使用手機藍牙App與 HC-06 藍牙模組連線。(Use the BT application to connect with HC-06)
此外在上述流程中,手機藍牙是當主控端 (Master),HC-06藍牙模組當從端 (Slave)。而我們需要將以上的流程先建立好在主控端藍牙模組 HC-05內,讓可以當主控端的 HC-05 藍牙模組在上電後就可以自動連線到 HC-06 從端藍牙模組。本文會以 Arduino Nano 藍牙小夜燈 當 Slave 端 (HC-06)與 Arduino UNO 版的雙軸按鍵搖桿當 Master 端(HC-05)。
The role of smart phone is bluetooth master. The role of HC-06 is bluetooth slave. HC-05(master) have to store the bluetooth master configuration in itself. After saving the bluetooth master configuration in HC-05(master), it will connect to HC-06(slave) automatically.
For more details, go to "Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link".
此外主控端與從端的速度(baudrate)要一樣,密碼(password)也要一樣。此文所用的 baudrate為 9600,password為 1234。 The baudrate and password must be the same in BT master/slave. In this tutorial, the baudrate is 9600 and the password is 1234.
三、藍牙模組的設定(Bluetooth Module Configuration) HC-06的設定值 (For HC-06)
所需的指令如下(The AT Commands List):
AT
AT+PIN1234
AT+NAMEyour_name
AT+BAUD4
成功的結果會類似下圖(The below picture is the successful result) 紅色 1 OK 是 AT 檢查裝置指令回傳結果。(RED 1 OK is the return value of the AT command.) 藍色 2 OKsetPIN 是 AT+PIN1234 設定密碼指令回傳結果。(Blue 2 OKsetPIN is the return value of the AT+PIN1234 command.) 綠色 3 OKsetname 是 AT+NAMEPingLun 設定藍芽名稱指令回傳結果。(Green 3 OKsetname is the return value of the AT+NAMEPingLun command.) 紫色 4 OK9600 是 AT+BAUD4 設定傳輸速度指令回傳結果。(Purple 4 OK9600 is the return value of the AT+BAUD4 command.)
注:上圖中的位址需轉為 98D3,31,FB5396 的格式,才能給 AT 指令用,此部分可的說明請參考 藍牙模組補充說明(四):無線連結兩個Arduino控制板一文中的藍芽裝置位址一節。 PS: the MAC address must be the 98D3,31,FB5396 for AT command.
HC-05的設定值(For HC-05)
要用AT指令讓HC-05主控端連上HC-06從端時,需將HC-05切換到指令模式,這需要在上電前壓住按鈕(如下圖用夾子夾住),注意 HC-05在 指令模式下的速度為 38400 。
Hold the reset button on the HC-05 before power on. This will enable HC-05 in command mode. A plastic peg could hold the reset button for you.
所需的指令如下:(The AT Command List)
AT+RMAAD 清除已配對的藍牙模組 (Clear the paired BT devices)
AT+ROLE=1 設定為 Master (Set the role to Master)
AT+BIND=98D3,31,FB5396 (Binding with the MAC address: 98D3,31,FB5396)
有回應OK才算成功。下圖的 ERROR 就是沒成功的例子,此時再下指令直到出現 OK。
Execute every command until it succeeds, got a OK response. The ERROR:(0) response say: "the command need to execute again".
藍牙模組互聯連線成功影片(注意燈號閃爍的速度):
The successful connection video(notice the onboard LED blinking speed)
尚未連線前:HC-06 與 HC-05 的指示燈會快速閃爍。
Before connection: the HC-06 and HC-05 onboard LED blink fast.
連線成功後:HC-06 的指示燈會恆亮;HC-05 的指示燈每兩秒快速閃爍兩次。
After connection: the HC-06 onboard will turn on, not blinking; the HC-05 will blink twice every two seconds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
五、HC-05 主控端藍牙搖桿程式(The Code for HC-05 and UNO)
搖桿的控制方式如下圖,箭頭的方向代表增加該顏色的亮度,例如紅線為X軸方向,向右會增加紅色燈的亮度,向左會減少紅色燈的亮度。黑色線為控制三種顏色的亮度。
This diagram shows the mechanism of LED lightness. For example, the red line means the RED lightness will increase from left to right(X Coordinate); the blue line mean the BLUE lightness will increase from top to bottom(Y Coordinate).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
將HC-06藍牙小夜燈 與 HC-05藍牙搖桿兩程式分別燒錄好後,就可以用搖桿來控制小夜燈了。
After uploading the Nano(HC-06) and UNO(HC-05), the bluetooth lamp could be controlled remotely by a bluetooth joystick.