46
Bluetooth Low Energy入門講座 第二部 実習 食関連産業IT(機械化)講座 応用編 モノとアプリをつなげる基本技術 北海道ソフトウェア技術開発機構 20131112日~13

Bluetooth Low Energy入門講座 -part2

  • Upload
    edy555

  • View
    1.864

  • Download
    1

Embed Size (px)

DESCRIPTION

BLEデバイスをLinuxから使って、基礎的なインターネット連携をしてみようという実習講座の資料です。(第二部)

Citation preview

Page 1: Bluetooth Low Energy入門講座 -part2

Bluetooth Low Energy入門講座

第二部 実習

食関連産業IT化(機械化)講座 応用編

モノとアプリをつなげる基本技術

北海道ソフトウェア技術開発機構

2013年11月12日~13日

Page 2: Bluetooth Low Energy入門講座 -part2

2日目の内容

• 午前

– コードによるデータの取得

– 各方法の説明

• 午後

– インターネットへのアップロード

Page 3: Bluetooth Low Energy入門講座 -part2

コードによる制御

Page 4: Bluetooth Low Energy入門講座 -part2

コード制御の実装方法

• 自動化やデータ処理を行ないたい

• プログラムによる制御をする必要がある

• 3案

– gatttool+python

– libbluetooth+socket(C言語)

– btle.js (BLEのnode.js実装: JavaScript)

Page 5: Bluetooth Low Energy入門講座 -part2

TI社のサンプル

• TI社提供のサンプルコード

– iOS用

– Android用

– http://www.ti.com/tool/sensortag-sw

Page 6: Bluetooth Low Energy入門講座 -part2

実装方法

• python+gatttool

– gatttoolコマンドを使用、簡便

• libbluetooth+socket

– C言語

– 正統派だが面倒

• btle.js

– BLEのnode.js実装: JavaScript

– Webとの連携に良いかも

Page 7: Bluetooth Low Energy入門講座 -part2

gatttool+スクリプト

• gatttoolを利用してデバイスを制御

• スクリプト言語としてpythonを利用

– 前述のgatttoolをサブプロセスとしてstdio経由で使用する

• サンプル

– ble-python ディレクトリ

– IR Temperatureの取得

– これを実際に動作させてみる

Page 8: Bluetooth Low Energy入門講座 -part2

python超入門

• ブロック

– Cでいう { ... }

– コロン“:”で始まりインデントを下げる

– インデントにはタブを使わない(ほうが良い)

• 関数呼び出しには括弧が必須

– エラーにならないので注意

• 関数定義はdef文

Page 9: Bluetooth Low Energy入門講座 -part2

ble-python(0)

• 準備

– BDアドレスを調べておく

$ sudo hcitool lescan

LE Scan ...

90:59:AF:0A:A8:A4 (unknown)

90:59:AF:0A:A8:A4 SensorTag

Page 10: Bluetooth Low Energy入門講座 -part2

ble-python(1)

• ソース: sensortag-temp.py

• BDアドレスを修正

def main(bdaddr='90:59:AF:0A:A8:A4'):

tool = connect(bdaddr)

while True:

try:

tmpr = read_data(tool)

print tmpr

except:

print "Error:", sys.exc_info()

time.sleep(5)

Page 11: Bluetooth Low Energy入門講座 -part2

ble-python(2)

• 実行手順

– 必要に応じてボタンを押す

$ sudo python sensortag-temp.py

Preparing to connect. You might need to press the

side button...

15.3

15.6

15.3

Page 12: Bluetooth Low Energy入門講座 -part2

ble-python(3)

• データをどうやって取っているか – コマンドを送って、反応を受け取る

– どんな応答を受け取っているかprintで確認

tool.sendline('char-read-hnd 0x25')

tool.expect('descriptor: .*')

rval = tool.after.split()

#print rval

objT = floatfromhex(rval[2] + rval[1])

ambT = floatfromhex(rval[4] + rval[3])

tmpr = calcTmpTarget(objT, ambT)

Page 13: Bluetooth Low Energy入門講座 -part2

ble-python(4)

• 他のセンサを使用するよう修正 – UUIDを変更して別のセンサ値を取得してみる – 参考: http://processors.wiki.ti.com/index.php/SensorTag_User_Guide

• 初期化部分

– イネーブルして一度取得

tool.sendline('char-write-cmd 0x29 01')

tool.expect('¥[LE¥]>')

tool.sendline('char-read-hnd 0x25')

tool.expect('descriptor: .*')

Page 14: Bluetooth Low Energy入門講座 -part2

ble-python(5)

• 初期化部分 – イネーブル対象を変更

• データ取得部分

tool.sendline('char-write-cmd 0x29 01')

tool.expect('¥[LE¥]>')

tool.sendline('char-read-hnd 0x25')

tool.expect('descriptor: .*')

tool.sendline('char-read-hnd 0x25')

tool.expect('descriptor: .*')

rval = tool.after.split()

#print rval

Page 15: Bluetooth Low Energy入門講座 -part2

ble-python(6)

• 温度

• 加速度

• 方位(地磁気)

• ボタン

• 気圧

• ジャイロ

Page 16: Bluetooth Low Energy入門講座 -part2

socket+C言語

• 別実装の方法として参考提示

• サンプル

– ble-cディレクトリ

Page 17: Bluetooth Low Energy入門講座 -part2

socket+C言語: コンパイル

$ sudo apt-get install libbluetooth-dev

• ライブラリのインストール(済)

• コード中のアドレスを修正

– test.c line:186

• コンパイル $ make

str2ba("90:59:AF:0A:A8:A4", &opts.dst);

Page 18: Bluetooth Low Energy入門講座 -part2

socket+C言語: 実行

$ ./test

push side button

bt_io_connect: 3

write WRITE_CMDwrite: 4

write READ_REQwrite: 3

read responseread: 5

opcode: 11

ad fe c8 0a

temperature: 17.554379

• コマンド入力後、センサーのボタンを押す

• その後Enterを入力

Page 19: Bluetooth Low Energy入門講座 -part2

socket+C言語: 解説

• 講習の範囲を超えるので簡単に

• ユーティリティとしてbtio.c/hを使用

– 後述のbtle.jsの一部、bluz由来

• ソケットを作成

– PF_BLUETOOTH

– PROTO_L2CAP

• ATTのデータフォーマットで入出力

• root権限でなくても動作する

Page 20: Bluetooth Low Energy入門講座 -part2

btle.js

• Node.js向けBLEモジュール

– beetle juiceと発音するそうです

– bluezのコードをベースに、glib等ライブラリ依存を削除

– githubで公開

• https://github.com/jacklund/btle.js

– npmでインストール可能

Page 21: Bluetooth Low Energy入門講座 -part2

Node.jsとは

• 趣旨から外れるので一言だけ説明

– サーバサイドJavaScript

– イベントドリブン

– 高負荷にも耐えるWebアプリケーションサーバとして知られる

Page 22: Bluetooth Low Energy入門講座 -part2

btle.js(0)

• インストール(済)

– node.jsの最新版をインストール

– npmでbtle.jsをインストール $ sudo apt-get install libbluetooth-dev

$ sudo apt-get install python-software-properties python

$ sudo apt-get install g++ make

$ sudo add-apt-repository ppa:chris-lea/node.js

$ sudo apt-get update

$ sudo apt-get install nodejs npm

$ npm install -g node-gyp

$ npm install btle.js

Page 23: Bluetooth Low Energy入門講座 -part2

btle.js(1)

• アドレスの修正

• 実行

– Notificationを受信して生データを表示

// Connect

btle.connect('90:59:AF:0A:A8:A4', function(err, device) {

$ node btle-test.js

Temperature, 65230, 2944

Temperature, 65220, 2944

Page 24: Bluetooth Low Energy入門講座 -part2

btle.js(2)

• アトリビュートの読み出し版

– 温度への変換を実装

– 少し遅延を入れている

$ node btle-read2.js

connecting SensorTag...

connected

Temperature: 17.475821408366528

Got close

Page 25: Bluetooth Low Energy入門講座 -part2

btle.js(3)

• HTTPサーバ版

– ブラウザから参照する http://localhost:1337/

$ node btle-http2.js

connecting SensorTag...

connected

Page 26: Bluetooth Low Energy入門講座 -part2

noble

• noble: もうひとつのnode.js ble実装

– というか決定版

– https://github.com/sandeepmistry/noble

– 各種デバイス対応やペリフェラルも存在

Page 27: Bluetooth Low Energy入門講座 -part2

noble+sensortag

• インストールはこれだけ

• 実行

$ sudo npm install sensortag

$ sudo npm install async

$ cd node_modules/sensortag

$ sudo node test.js

Page 28: Bluetooth Low Energy入門講座 -part2

インターネット連携

Page 29: Bluetooth Low Energy入門講座 -part2

インターネット連携

• やること:センサーから取得したデータをサーバにアップロードする

• クラウドサービスを利用する

Page 30: Bluetooth Low Energy入門講座 -part2

xively.com

• 「Public Cloud for the Internet of Things」

• モノのインターネットのためのクラウド

• データの蓄積

• グラフ化

• WebAPI (REST)

• 各種プラットフォーム用ライブラリあり

• Developer Accountは無料で取得可能

Page 31: Bluetooth Low Energy入門講座 -part2

xively.com

Page 32: Bluetooth Low Energy入門講座 -part2
Page 33: Bluetooth Low Energy入門講座 -part2

実例:室温記録

Page 34: Bluetooth Low Energy入門講座 -part2

xivelyのはじめ方

1. アカウントを作成

2. フィードを作成

3. APIキーを取得

4. データをアップロード

Page 35: Bluetooth Low Energy入門講座 -part2

xively: アカウント作成

• (作成済みのアカウントを共有します)

Page 36: Bluetooth Low Energy入門講座 -part2

xively:手順

• ログイン

• デバイス(フィード)作成

• Feed IDとAPI Keyを取得

• スクリプトに設定

• 実行

Page 37: Bluetooth Low Energy入門講座 -part2

xively:デバイス追加

Page 38: Bluetooth Low Energy入門講座 -part2

xively:Feed IDとAPI Key

Page 39: Bluetooth Low Energy入門講座 -part2

xively:スクリプト

• Feed IDとAPIキーを転記

• Bluetoothアドレスを転記

• インターバルを調整

XIVELY_API_KEY =

"eIpXlvKHd9CdtdD5SK9TgzaiZ9osxOX2a05MK18epQdAb0iy"

XIVELY_FEED_ID = 813879821

def main(bdaddr='90:59:AF:0A:A8:A4'):

Page 40: Bluetooth Low Energy入門講座 -part2

xively:実行

• 実行すると、データ取得&アップロードを継続

$ python ./sensortag2xively.py

Preparing to connect. You might need to press the side

button...

(datetime.datetime(2013, 11, 9, 14, 11, 29, 172604,

tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>), 20.7)

(datetime.datetime(2013, 11, 9, 14, 11, 30, 828813,

tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>), 21.1)

....

Page 41: Bluetooth Low Energy入門講座 -part2

xively:コンソール

Page 42: Bluetooth Low Energy入門講座 -part2

xively:フィード表示

• Xivelyのサイトでグラフ表示

– https://xively.com/feeds/813879821

Page 43: Bluetooth Low Energy入門講座 -part2

xively:データ取得

• REST APIにより蓄積データを参照可能 – https://xively.com/dev/docs/api/quick_reference/historical_data/

– JSON, XML, CSV

• CSVで一日分のデータを取得する例 $ wget --header "X-ApiKey:

eIpXlvKHd9CdtdD5SK9TgzaiZ9osxOX2a05MK18epQdAb0iy"

'https://api.xively.com/v2/feeds/813879821/datastreams/tmpr.

csv?start=2013-11-09T00:00:00Z&duration=24hours' -O tmpr.csv

Page 44: Bluetooth Low Energy入門講座 -part2

xively:グラフイメージ取得

• グラフイメージの取得 – see https://xively.com/dev/docs/api/data/read/single_datastream/

https://api.xively.com/v2/feeds/{feed id}/datastreams/{stream id}.png

Page 45: Bluetooth Low Energy入門講座 -part2

xively:JSによるグラフ作成

• インタラクティブなグラフを含むページ作成

– http://xively.github.io/channel-viz/

Page 46: Bluetooth Low Energy入門講座 -part2

まとめ

• BLEの仕組みの理解

• Linuxからの利用

• Xivelyへのアップロード