Raspberry Pi で F-PLUG のログ収集

Posted: , Modified:   RaspberryPi Docker fluentd IoT Qiita

本稿は Qiita 投稿記事 のバックアップです.

概要

販売終了 が決まった富士通F-PLUGのログを, 今更ながらRaspberry Piにて集約する.

構成

Raspberry Pi の OS には Raspbian を使った. また,F-PLUG のデータは Docker コンテナを用いて取得し, Fluentd を使って収集することにした.

Raspberry Pi への Docker インストールは 別記事を参照. FluentdのインストールはRaspberry Pi に fluentd をインストールするを参考にした.

Bluetooth の設定とペアリング

まず,関連パッケージをインストールする.

$ sudo apt-get install bluetooth bluez-utils bluez-compat

次に,F-PLUGのアドレスを取得する.

$ sudo hcitool scan

得られたアドレスを用いてペアリングを行う. 下記のコマンドを実行すると F-PLUG の LED が点滅するので, 本体のボタンを長押しする.点滅が止めばペアリングは完了です.

$ sudo bluetooth-agent 1234 <address>

最後に,起動時に接続するように設定ファイル /etc/bluetooth/rfcomm.conf を編集する.

rfcomm0 {
    # Automatically bind the device at startup
    bind yes;
    # Bluetooth address of the device
    device <address>;
    # RFCOMM channel for the connection
    channel 1;
    # Description of the connection
    comment "F-PLUG No.1";
}

複数台接続する場合はアドレススキャンから設定の追加までを適宜繰り返す.

設定が終わったら Bluetoothサービスを再起動させておく.

$ sudo service bluetooth restart

データの収集

データ収集ツールを作った (と言ってもライブラリを叩くだけ)ので,これを利用する. Raspberry Pi 用 Docker イメージは ここ にある. このイメージは引数にデータ取得間隔及びデバイスパスなどを取り, F-PLUGから取得したデータを下記の JSON 形式で出力する.

{
  "temperature": "温度",
  "power": "電力",
  "humidity": "湿度",
  "illuminance": "明るさ",
  "time": "UNIX 時刻"
}

なので,Docker のログドライバを Fluentd にして実行すれば,後は Fluentd 側で好きに扱えば良い. なお,1プロセスで1デバイスからのデータ取得にしか対応していないので, 複数デバイス利用する場合は必要個数分コンテナを起動する必要がある.

コンテナの実行は,

$ docker run -dt --name fplug1 --log-driver=fluentd -v /dev/rfcomm0:/dev/rfcomm0 \
     --privileged jkawamoto/rpi-fplug-logger --interval 10

などとする.上記の設定では 10秒ごとにデータを取得する.(デフォルトは1分ごと) デバイスにアクセスするので --privileged を忘れないようにすること. また,複数台接続している場合は適宜 -v /dev/rfcomm0:/dev/rfcomm0 の部分を変更する. 例えば,rfcomm1を使う場合は -v /dev/rfcomm1:/dev/rfcomm0などとすれば良い.

参考