iOS 10になって、Siriが解放された!と思い、Siri経由でLチカしてみよう。と思い、いろいろと調べているうちに、SiriKitやSpeech.framework使わないでHomeKitでいいんじゃないかという結論に。
ということで、SiriKit, Speech.frameworkも使ってみたいけどちょっと今回は保留して、今更ながらHomeKitのありがたみに触れてみたいと思う。
こちらを参考にさせていただきました。
https://datahotel.io/archives/725
最終的に
Hey Siri LEDの赤をつけて -> LED点灯
Hey Siri LEDの色を消して -> LED消灯
となる
LED + Edison セットアップ
EdisonでLチカする方法については、前のメモで書きましたが、今回は色を指定して点灯して欲しいので、さらに変更。
前回のメモ: http://qiita.com/motokazu/items/d3ee6959c10bdc271659
つまり、外からパラメータで色を指定できるようにする。
var mraa = require('mraa');
var args = process.argv;
var r = new mraa.Pwm(5);
var g = new mraa.Pwm(3);
var b = new mraa.Pwm(6);
r.enable(true);
r.period_us(2000);
r.write(0.0);
g.enable(true);
g.period_us(2000);
g.write(0.0);
b.enable(true);
b.period_us(2000);
b.write(0.0);
/// LED
var rcolor = 0;
var gcolor = 0;
var bcolor = 0;
if(args.length > 1){
var color = args[2];
switch(color){
case "red":
rcolor = 1;
break;
case "green":
gcolor = 1;
break;
case "blue":
bcolor = 1;
break;
default:
rcolor = gcolor = bcolor = 0;
}
}
r.write(rcolor);
g.write(gcolor);
b.write(bcolor);
これにより、コマンドラインから色を指定してLEDを点灯できるようになる。
node main.js red
次に HomeKitと繋ぎます。
HomeKitと繋ぐために、HomebridgeをEdisonにセットアップ。
npm i -g homebridge
homebridge
<<以下のようなアウトプット>>
[Sun Oct 23 2016 01:22:35 GMT+0000 (UTC)] No plugins found. See the README for information on installing plugins.
[Sun Oct 23 2016 01:22:35 GMT+0000 (UTC)] config.json (/home/root/.homebridge/config.json) not found.
Scan this code with your HomeKit App on your iOS device to pair with Homebridge:
┌────────────┐
│ 031-45-154 │
└────────────┘
[Sun Oct 23 2016 01:22:36 GMT+0000 (UTC)] Homebridge is running on port 60327.
動き出しました。
とはいえ、プラグインも入っていない & Config.json も未セットアップということで何もできないので、セットアップ。
homebridge pluginを入れる
まずは、今回はコマンドラインからLEDを制御するので、homebridge-cmd プラグインを入れる
npm install -g homebridge-cmd
そのほかのpluginはこちらから探すことができる。
https://www.npmjs.com/search?q=homebridge-plugin
homebridge config.jsonの設定
続いて、config.jsonを作る
vi ~/.homebridge/config.json
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:30",
"port": 51826,
"pin": "031-45-154"
},
"description": "Siri LED test",
"accessories": [
{
"accessory": "CMD",
"name": "赤いLED",
"on_cmd": "node /node_app_slot/main2.js red",
"off_cmd": "node /node_app_slot/main2.js clear"
}
]
}
サンプルのconfig.jsonを元に修正
https://github.com/nfarina/homebridge/blob/master/config-sample.json
homebridgeを起動する
homebridge
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] Loaded plugin: homebridge-cmd
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] Registering accessory 'homebridge-cmd.CMD'
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] ---
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] Loaded config.json with 1 accessories and 0 platforms.
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] ---
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] Loading 1 accessories...
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] [赤いLED] Initializing CMD accessory...
Scan this code with your HomeKit App on your iOS device to pair with Homebridge:
┌────────────┐
│ 031-45-154 │
└────────────┘
[Sun Oct 23 2016 01:42:43 GMT+0000 (UTC)] Homebridge is running on port 51826.
動いた。
HomeKitとhomebridgeを接続する
iOSにデフォルトで入っている "ホーム" アプリを起動
Siri から呼び出す
お。動いた。
もっと自然に話したい...
でも、赤いLEDをつけて , 消して。というやりとりは変だ。どちらかといえば、"LEDを赤にして" と言いたい。
なんとかできないだろうか...
そこで、部屋を使う作戦を考えた.
(もはや使い方を間違っているが、良いことにする)
"LEDの赤をつけて" と言えるようにする。ついでなので、他の色も追加する。そして無理やり消すためのアクセサリーも追加する。("LEDの色を消して" と言えるようになるはず)
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:30",
"port": 51826,
"pin": "031-45-154"
},
"description": "Siri LED test",
"accessories": [
{
"accessory": "CMD",
"name": "赤",
"on_cmd": "node /node_app_slot/main2.js red",
"off_cmd": "node /node_app_slot/main2.js clear"
},
{
"accessory": "CMD",
"name": "緑",
"on_cmd": "node /node_app_slot/main2.js green",
"off_cmd": "node /node_app_slot/main2.js clear"
},
{
"accessory": "CMD",
"name": "青",
"on_cmd": "node /node_app_slot/main2.js blue",
"off_cmd": "node /node_app_slot/main2.js clear"
},
{
"accessory": "CMD",
"name": "色",
"on_cmd": "node /node_app_slot/main2.js red",
"off_cmd": "node /node_app_slot/main2.js clear"
}
]
}
homebridgeを再起動する
homebridge
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] Loaded plugin: homebridge-cmd
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] Registering accessory 'homebridge-cmd.CMD'
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] ---
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] Loaded config.json with 4 accessories and 0 platforms.
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] ---
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] Loading 4 accessories...
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] [赤] Initializing CMD accessory...
[Sun Oct 23 2016 01:57:24 GMT+0000 (UTC)] [緑] Initializing CMD accessory...
[Sun Oct 23 2016 01:57:25 GMT+0000 (UTC)] [青] Initializing CMD accessory...
[Sun Oct 23 2016 01:57:25 GMT+0000 (UTC)] [色] Initializing CMD accessory...
Scan this code with your HomeKit App on your iOS device to pair with Homebridge:
┌────────────┐
│ 031-45-154 │
└────────────┘
[Sun Oct 23 2016 01:57:25 GMT+0000 (UTC)] Homebridge is running on port 51826.
アプリから "部屋" を設定する。既存で設定済みのデフォルトの部屋は名前を変えられない(?)ようなので、新しくLEDという部屋(?!)を作り、HomebridgeのアクセサリーをLED部屋へ移動する
ということで。。。
それっぽい!
まとめ
Edison + homebridge + HomeKitで、ターゲットのものを部屋に設定(?!)すると、それっぽい会話になることが分かったが、言い回しが増えるとどんどんアクセサリーが増えるという事態になる&リビングのLEDの〜という言い回しができなくなるので、そもそも使い方に問題があり、結果的にはこれは違う。ということになる。
けど、面白い。
つまり、言い回しバリエーション増やして対応したい。となったら、SiriKitとSpeech.frameworkをうまく使うことになるのだろう。SiriKitの現時点(2016/10)の制約の中でどこまでできるのかは気になる。