LoginSignup
2
3

More than 5 years have passed since last update.

Amazon Dash Buttonでおうちハックの準備 その2

Posted at

前回の投稿と同じネタですが
別のライブラリでも環境を作ってみたくて、
今回はこちらを使って環境作ってみようと思います。
https://github.com/hortinstein/node-dash-button

というのも、
https://www.npmjs.com/package/dash-button
を使ったときに複数DashButtonに対する設定方法がわからず、
複数オブジェクトつくって待機させてみてもうまく動かなかったので、こちらを試してみることにしました。

実行環境

  • MacBookPro + macOS Sierra
  • 無線LAN
  • Node.js v7.3.0
  • npm 4.0.5

環境作成

まずディレクトリを作成。

command
$ mkdir node-dash-button-project/
$ cd node-dash-button-project/

初期化します。

command
$ npm init

そうすると、package.jsonファイルが作成されます。
次にnode-dash-buttonをインストールします。
あとで使うので、date-utilsも一緒にインストールしておきます。

command
$ sudo npm install --save node-dash-button
$ sudo npm install --save date-utils

インストールあとのディレクトリ構成は、こんな感じになりました。
node-dash-button-project/node_modules/node-dash-button/bin/に、
デバイス検出用のプログラムfindbuttonがあります。

Tree
 node-dash-button-project
  └── node_modules
      ├── nan
      │   ├── doc
      │   └── tools
      ├── node-dash-button
      │   ├── bin
      │   ├── static
      │   └── test
      │       └── lib
      ├── pcap
      │   ├── build
      │   │   └── Release
      │   │       └── obj.target
      │   │           └── pcap_binding
      │   ├── decode
      │   │   ├── ieee802.11
      │   │   └── ipv6headers
      │   ├── examples
      │   └── spec
      │       └── decode
      │           ├── ieee802.11
      │           └── ipv6headers
      ├── socketwatcher
      │   └── build
      │       └── Release
      │           └── obj.target
      │               └── socketwatcher
      └── underscore

DashButtonのスキャンを行う

検出用のプログラムを実行します。
Manufacturerが"Amazon Technologies Inc."となっているのが、Dash Buttonです。
udpなんですね。arp飛ばしているのがルーターと、MacBookPro。

command
$ sudo node bin/findbutton
Watching for arp & udp requests on your local network, please try to press your dash now
Dash buttons should appear as manufactured by 'Amazon Technologies Inc.'
Possible dash hardware address detected: c0:25:a2:2c:99:a8 Manufacturer: NEC Platforms Protocol: arp
Possible dash hardware address detected: f0:27:2d:89:38:e2 Manufacturer: Amazon Technologies Inc. Protocol: udp
Possible dash hardware address detected: f0:27:2d:05:c0:f4 Manufacturer: Amazon Technologies Inc. Protocol: udp
Possible dash hardware address detected: c0:25:a2:2c:99:a8 Manufacturer: NEC Platforms Protocol: arp
Possible dash hardware address detected: f0:27:2d:05:c0:f4 Manufacturer: Amazon Technologies Inc. Protocol: udp
Possible dash hardware address detected: c0:25:a2:2c:99:a8 Manufacturer: NEC Platforms Protocol: udp
Possible dash hardware address detected: 6c:ad:f8:e1:13:0a Manufacturer: AzureWave Technology Inc. Protocol: arp
Possible dash hardware address detected: f4:0f:24:2d:8b:41 Manufacturer: Apple Protocol: arp

これで我が家のDash buttonは

  • f0:27:2d:05:c0:f4
  • f0:27:2d:89:38:e2

ということがわかりました。

単一DashButtonの場合

Sampleを参考に、Macアドレスと出力メッセージだけ変更して以下のようにしてみました。

app.js
var dash_button = require('node-dash-button');
var dash = dash_button("f0:27:2d:05:c0:f4", null, null, 'all'); 
dash.on("detected", function (){
    var dt = new Date();
    console.log("DashButton is working."+dt.toFormat("YYYY/MM/DD HH24:MI:SS"));
});

実行してみます。

command
$ sudo node app.js
DashButton is working.2017/01/04 22:56:47
DashButton is working.2017/01/04 22:57:33

動いた!

複数DashButtonの場合

次に、複数DashButtonを待機させてみます。

app2.js
require('date-utils');

var dash_button = require('node-dash-button');
var dash = dash_button(["f0:27:2d:05:c0:f4","f0:27:2d:89:38:e2"], null, null, 'all');
dash.on("detected", function (){
    var dt = new Date();
    console.log("DashButton is working."+dt.toFormat("YYYY/MM/DD HH24:MI:SS"));
});

実行してみます。

$ sudo node app.js
DashButton is working.2017/01/04 23:01:37
DashButton is working.2017/01/04 23:01:43
DashButton is working.2017/01/04 23:01:44
DashButton is working.2017/01/04 23:01:51
DashButton is working.2017/01/04 23:01:54

動いた!

感想

こっちのほうが、単発のポチリが正確な気がしました。
DashButtonは押して、Wi-Fiに接続したときに白くLEDが光るのですが、
その時間が長いと、複数回押されたと判定するような挙動に見えました。
Defaultは5秒でタイムアウトするみたいなので、
それ以上経ったあとに、udp飛んだりすると再度認識してしまうみたい。
この辺はチューニングでいけるのかもしれませんね。
こちらのライブラリは、dash_idという形でMACアドレスを使って処理分けることや、
NetworkInterfaceも指定出来るみたいで、こっちで進めていくことになりそうです。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3