[基本編] BeagleBoneBlack と Arduino を Bluetooth で接続
- 参考:How to make BeagleBone and Arduino Communication
- 参考:Connecting your Linux Laptop (Particularly Your Asus Eee) to Your Cellphone via Bluetooth
- 参考:BluetoothSetup
- 参考:How do I connect and send data to a bluetooth serial port on Linux?
「コンピューターとしゃべる(Bluetooth)」で示したように、Arduino に HC-06(JY-MCU) を接続して、BeagleBone のアプリケーションと通信するまでを説明するよ。
SDPに必要なサービスを登録する
BeagleBoneBlack に SPP が登録されているか確認する。
$ sdptool browse local
登録されていないので、以下のコマンドで登録する。
$ sdptool add --channel=22 SP Serial Port service registered
SPP が登録された。
# sdptool browse local Service Name: Serial Port Service Description: COM Port Service Provider: BlueZ Service RecHandle: 0x10007 Service Class ID List: "Serial Port" (0x1101) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 22 Language Base Attr List: code_ISO639: 0x656e encoding: 0x6a base_offset: 0x100 Profile Descriptor List: "Serial Port" (0x1101) Version: 0x0100
Bluetooth デバイスの登録
Bluez-simple-agent で libffi.so ライブラリが必要なのでソースをダウンロードしてインストールしておく。
シンボリックリンクを張るのを忘れずに。
$ wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz $ tar xzvf libffi-3.2.1.tar.gz $ cd libffi-3.2.1 $ ./configure && make # make install # ln -s /usr/local/lib/libffi.so.6 /usr/lib/
Arduino の Bluetooth デバイス HC-06 を検索する。
$ sudo hcitool scan Scanning ... 30:14:10:15:02:64 HC-06
RFCOMM ポートとアダプタをバインドする。
# rfcomm bind 1 30:14:10:15:02:64 # rfcomm rfcomm1: 30:14:10:15:02:64 channel 1 closed
rfcomm bind コマンド
指定したリモートのBluetoothデバイスと仮想シリアルポートを関連付ける。 bindコマンド実行後、/dev/rfcommDEV(DEVはポート番号)というデバイスファイルが作成されるので、 以降はこれを使って通信ができる。 bindコマンドでは、デバイスファイルを作るだけで実際にリモートデバイスと接続は行わない。 アプリケーションがデバイスファイルをオープンする段階で接続処理が行われる。
Bluetooth デバイスへの接続
HC-06 が見つかったので、MAC アドレスを指定してペアリングする。
ピンコードは 1234 を入力する。
*bluez-simple-agentは python 2.7系で動かない。
$ sudo bluez-simple-agent hci0 30:14:10:15:02:64 RequestPinCode (/org/bluez/399/hci0/dev_30_14_10_15_02_64) Enter PIN Code: 1234 Release New device (/org/bluez/399/hci0/dev_30_14_10_15_02_64)
現在のセッションのみで接続する。
$ sudo hidd --connect 30:14:10:15:02:64
minicom で通信確認
minicom のインストール&セットアップ
$ sudo apt-get install minicom
minicom を起動して、Serial port setup で以下のように設定する。
$ sudo minicom -s A - Serial device: /dev/rfcomm1 E - Bps/Par/Bits: 115200 8N1
Configuration 画面から exit すると受信データが表示される。
Welcome to minicom 2.6.2 OPTIONS: I18n Compiled on Feb 8 2013, 05:09:59. Port /dev/rfcomm1 Press CTRL-A Z for help on special keys hello world! :
pySerial でシルアル通信確認 (python 3.3のみ有効)
$ pip install pyserial $ sudo easy_install -U pyserial
注意:python 2.7 から python 3.3 へ移行したとき pip でエラーが発生したが 以下の手順でうまくいった。
# curl https://bootstrap.pypa.io/ez_setup.py | python # easy_install --upgrade pip # pip install --upgrade setuptools
pyserial を使って受信データを表示する。
> import serial > ser = serial.Serial('/dev/rfcomm0', timeout=0.1) # timeoutを秒で設定(default:None)ボーレートはデフォルトで9600 > c = ser.read(10) # 10文字読み込み > print(c) > ser.close()
node-serial で通信
必要モジュールのインストール
BeagleBoneBlack に Node.js と Snowshoe 環境はインストールされているものとする。
シリアル通信モジュール インストール
$ npm install serialport
シルアル通信用のコード serialtest.js
var serialport = require('serialport'); /* ポートの設定 */ var portName = '/dev/rfcomm1'; var sp = new serialport.SerialPort(portName, { baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: false, parser: serialport.parsers.readline("n") }); /* イベントハンドラ */ sp.on('data', function(input) { console.log('Received : ' + input); });
実行結果
$ node serialtest.js Received : hello world! Received : hello world! Received : hello world! :