続・Webエンジニアのためのスマートホームハック
Node.js で IoT プロトコルハック
2017年9月24日(日)
HTML5 Conference 2017
@futomi futomi.hatano
日経SYSTEMSコラム
2
• マイコンボードを使って大気圧と気温を取得
• Webブラウザーから制御 MIDIでサーボを動かす
• Node.jsで家電を操作 赤外線を利用しよう
• ECHONET Liteで家電をコントロール
• スマホから機器を制御「デバイスコネクトWebAPI」
• 電池交換が不要に 無線技術「EnOcean」に注目
• JavaScriptで車載情報を取得「OBD-II」を使う
• Node.jsで電力情報を取得スマートメータを活用しよう
• ネットワークカメラに注目 Webブラウザーで操作可能に
• WebブラウザーからBLEでセンサーにアクセス
IoTで火が付くITエンジニア魂:2016年4月号~2017年3月号
GitHubとnpm
3
https://www.npmjs.com/~futomihttps://github.com/futomi
4
前回のあらまし
HTML5 Conference 2016
前回紹介したデバイスとプロトコル
5
MIDI
つづきはWebで
6
もくじ
• 前回のあらまし
• BLE編
• iBeacon/Eddystone
• Linking/ALPS/TI SensorTag
• サブギガ無線方式編
• ALPS
• Z-Wave
• カメラ編
• ネットワークカメラ ONVIF
• 顔認識カメラ OMRON
• 自動車編
• OBD-II
7
8
Bluetooth Low Energy
iBeacon/Eddystone/センサータグ
iBeacon/Eddystone
• BLEのアドバタイジングパケット
• ビーコンが短いデータをまき散らす
• iBeacon
• Appleのビーコン仕様
• UUID/Major/Minor
• Eddystone
• Google提唱のオープンなビーコン仕様
• UID/URL/TLMの3種類
9
iBeacon/Eddystone製品例
• 専用アプリで接続・設定
• ビーコンデバイスをクラウドで管理・設定できる
• iBeacon/Eddystoneの両方を同時に発信できる製品も
10
http://reco2.me/ https://kontakt.io/ https://estimote.com/
node-beacon-scanner
11
https://github.com/futomi/node-beacon-scanner
const BeaconScanner = require('node-beacon-scanner');
const scanner = new BeaconScanner();
// Set an Event handler for becons
scanner.onadvertisement = (ad) => {
console.log(JSON.stringify(ad, null, ' '));
};
// Start scanning
scanner.startScan().then(() => {
console.log('Started to scan.');
}).catch((error) => {
console.error(error);
});
{
"id": "c6debed8f549",
"address": "c6:de:be:d8:f5:49",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "eddystoneUrl”,
"eddystoneUrl": {
"txPower": -35,
"url": http://go.esti.be/
}
}
EddyStone URLの例
デモ
12
ビーコンスパム、絶賛大放出中
Linking
• NTTドコモが開発したBLEプロトコル
• センサー/ボタン/LED/バイブレーション
• 仕様はProject Linkingとして一般に公開
• アドバタイジングパケット仕様とベアリングモード仕様
• 製品
• (株)Braveridge
• (株)芳和システムデザイン
• 比較的安いのが特徴
13
https://linkingiot.com/
Linking製品例
14
温度
湿度
気圧
加速度
回転速度
磁気
LED
バイブレーション
ボタン
バッテリー
node-linking
15
https://github.com/futomi/node-linking
const Linking = require('node-linking’);
const linking = new Linking();
// Set a callback function called when a packet is received
linking.onadvertisement = (ad) => {
console.log(JSON.stringify(ad, null, ' '));
};
// Start to scan advertising packets from Linking devices
linking.startScan({ nameFilter: 'Tukeru' });
{
...
"localName": "Tukeru_th0164069",
"txPowerLevel": -96,
"rssi": -67,
"distance": 0.03548133892335755,
"version": 0,
"vendorId": 0,
"individualNumber": 164069,
"beaconServiceId": 1,
"beaconServiceData": {
"name": "Temperature (°C)",
"temperature": 25.875
}
}
デモ
16
ALPS センサネットワークモジュール
• アルプス電気が販売するBLEセンサータグ
• 温度/湿度/照度/気圧/UV/加速度/地磁気
• かなり細かい設定が可能
• リクエストとレスポンスが別のCharacteristic
17
http://www.alps.com/j/iotsmart-network/index.html
Service
Characteristic 3
Characteristic 2
Characteristic 1
Write
コマンド制御
Notification
センサ情報
Notification
Read応答など
node-alps
18
https://github.com/futomi/node-alps
const Alps = require('node-alps’);
const alps = new Alps();
let device = null;
alps.init().then(() => {
return alps.discover();
}).then((device_list) => {
device = device_list[0];
return device.connect();
}).then(() => {
return device.startMonitor({mode : 0});
}).then(() => {
device.onnotify = (data) => {
console.log(JSON.stringify(data, null, ' '));
};
});
{
"pressure": 996.4881361104754,
"humidity": 51.65625,
"temperature": 24.04,
"uv": 0.02577319587628866,
"ambient": 129.3103448275862,
"ambientLed": 222.94887039239,
"ambientFluorescent": 293.8871473354232,
"timeStamp": {...},
"dataIndex": 29
}
TI SimpleLink SensorTag CC2650STK
• Texas Instrumentsが販売するBLEセンサータグ
• 温度/湿度/照度/気圧/加速度/回転速度/磁気/LED/ブザー/ボタン/バッテリー
• センサータグの先駆けで、開発者の中では世界で一番有名?
• BLE接続後、Readでワンショット取得、Subscribeで通知登録、Notificationで通知
受信で、非常に分かりやすいプロファイル設計
19
html5-CC2650SensorTag.js
20
https://github.com/futomi/html5-CC2650SensorTag.js
const tag = new CC2650SensorTag();
tag.discover().then(() => {
return tag.connect();
}).then(() => {
let c = {temperature: {enable_ir_temperature_sensor: true}};
return tag.writeConfigurations(c);
}).then(() => {
tag.ontemperaturenotify = (data) => {
console.log(data['ambience'] + '°C’);
};
return tag.startTemperatureNotifications();
}).catch((error) => {
console.log(error.message);
});
nodeモジュールではなく Web Bluetoothを使ったJavaScriptライブラリ
デモ
21
https://rawgit.com/futomi/html5-CC2650SensorTag.js/master/sample/monitor/index.html
22
サブギガ帯無線方式
アルプス電気 環境センサモジュール
ALPS 環境センサモジュール
• アルプス電気のセンサモジュール
• サブギガ帯 (920MHz)/プロトコルは独自
• ソーラーパネル式と電池式/送信出力1mWと10mWのモデル
• 温度/湿度/照度/気圧に対応
• USBゲートウェイは単なるシリアル通信 (COMポート)
23
http://www.alps.com/j/iotsmart-environment/index.html
node-alps-env
24
https://github.com/futomi/node-alps-env
const AlpsEnv = require('node-alps-env’);
AlpsEnv.connect().then(() => {
// センサー情報受信のイベントハンドラをセット
AlpsEnv.onmessage = (data) => {
// 受信データを出力
console.log(JSON.stringify(data, null, ' ‘));
};
}).catch((error) => {
console.error(error);
});
{
"no": 2,
"txid": 14746066,
"temp": 28.2,
"humi": 60.9,
"pres": 1000.29,
"light": 484,
"door": 1,
"vbat": 2.9,
"rssi": -79
}
25
Z-Wave
スマートホーム/サブギガ帯無線規格
Z-Wave
• 欧米/韓国ではスマートホームでメジャーな無線規格
• サブギガ帯 (日本では920MHz)/メッシュ対応
• 機器種別ごとのプロトコル (Command Class) まで規定 (一般公開)
• 低消費電力 - センサーデバイスなら単四電池2個で3年程度
• 技術評価と開発
• Dev.Kit購入およびSigma Designs社とNDA/Dev.Kitライセンス契約締結
• SDKや非公開情報の公開
• 日本国内ではユニ電子(株)が取り扱い (http://www.uni-elec.co.jp/)
• サービス提供に必要な手続き
• Z-Wave Alliance加盟
• 機器の認証
26
http://www.z-wave.com/
Controller Development Kits
27
http://z-wave.sigmadesigns.com/design-z-wave/controller-development-kits/
技適取得済みデバイス例
28
VISION/ZM2201JP-5
4 in 1 Door Sensor
VISION/ZD2112JP-5
Door Sensor (Thin Magnet Type)
Goodway/WD6052
Z-Wave USB Dongle
Aeotec/ZW100
Multi sensor 6
VISION/ZP3111JP-5
4in1 PIR Sensor
Goodway/TD13320
Smart Meter Switch
Command Class
29
Command Classの例
• Anti-theft
• Binary Switch
• Color Switch
• Door Lock
• Energy Production
• Geographic Location
• HRV Control
• Humidity Control Mode
• Meter
• Multilevel Sensor
• Thermostat Fan Mode
• Window Covering
Command Class Structure
Fundamental Commands
• SET
• GET
• REPORT
Z-Ware
30
• Dev.Kit契約やAlliance加盟なしにRPiで評価
• RaspbianのOSイメージで配布
• http://zwavepublic.com/downloads
• ローカルにZ-Ware APIが起動
• デフォルトでダッシュボード利用可
• RESTベースでWebアプリ試作可
31
ネットワークカメラ
ONVIF
ONVIF
32
• Open Network Video Interface Forum
• ネットワークカメラのプロトコルの標準
• デバイス発見、ビデオストリーミング、PTZ制
御など
• NVT発見にWS-Discovery
• UDPポート3702によるマルチキャスト
• メッセージはSOAPベース
• HTTPプロトコルでSOAPをPOST
• 今どきSOAP?
• でもONVIF策定当時はSOAPがナウい時代
http://www.onvif.org/
33
node-onvif
34
https://github.com/futomi/node-onvif
const onvif = require('node-onvif’);
const fs = require('fs’);
let device = new onvif.OnvifDevice({
xaddr: 'http://192.168.10.14:10080/onvif/device_service’,
user : 'admin’, pass : '123456’
});
device.init().then(() => {
return device.fetchSnapshot();
}).then((res) => {
fs.writeFileSync('snapshot.jpg', res.body, {encoding: 'binary’});
}).catch((error) => {
console.error(error);
});
デモ
35
36
顔認識カメラ
オムロンヒューマンビジョンコンポ
OMRON HVC-P2
37
人体検出
手検出
顔検出
顔向き推定
年齢推定
性別推定
視線推定
目つむり推定
表情推定
顔認証
node-omron-hvc-p2
38
https://github.com/futomi/node-omron-hvc-p2
const HvcP2 = require('node-omron-hvc-p2’);
const hvcp2 = new HvcP2();
hvcp2.connect().then(() => {
return hvcp2.detect({
face : 1, // Enable face detection
age : 1, // Enable age estimation
gender : 1 // Enable gender Estimation
});
}).then((res) => {
console.log(JSON.stringify(res, null, ' ‘));
}).catch((error) => {
console.error(error);
});
{"face": [{
"face": {
"x": 930,
"y": 497,
"size": 390,
"confidence": 579
},
"age": {
"age": 41,
"confidence": 500
},
"gender": {
"gender": 1,
"confidence": 1000
}
}]}
デモ
39
40
自動車
OBD-II
OBD-IIコネクタ
OBD-IIアダプタ (ELM327互換)
USB Serial Bluetooth RFCOMM/L2CAP
昔Chrome Appsでアプリを作ってみた
• Chrome Platform APIs
• bluetooth API
• bluetoothSocket API
• serial API
• ODB-II ECU Simulator
• ISO 15765-4 (CAN)
• ODB-II Adapter
• IC: STN1100 (ELM327互換)
• Interface: USB Serial
• baud rate: 115,200 bps
47
[PR] Open Hardware Pavilion
APPS JAPAN 2018 @幕張メッセ
Open Hardware Pavilion (仮)
• APPS JAPAN 2018@幕張メッセ 2018年6月13日~15日
• 新たに Open Hardware Pavilion (仮) を企画中
• オープンでアプリ開発者フレンドリーなハードウェアを一堂に集結
• ご興味があるメーカーさん、ぜひお声がけください
• ご意見、ご要望、お聞かせください
ご清聴ありがとうございました
@futomi futomi.hatano

続・Webエンジニアのためのスマートホームハック ~ Node.js で IoT プロトコルハック ~