LoginSignup
1
3

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-12-24

ADB買ってみた

Amazon Dash Buttonといういいおもちゃが発売されたので、買ってみました。
おうちハックといいながらも、まずは接続して検知するところまで。
そういえば、海外で買ったDashButtonは、その地域のアカウントでWi-Fi接続設定が必要みたいですね。
アプリは同じもので、「.com」アカウントに切り替えられるみたいです。
グローバル対応がすごい!!

幸いにも先人たちがたくさんいらっしゃるので、
コマンド打ちながら、試してみました。
主にはこちらの記事を参考に試しました。
Amazon Dash Buttonを(正しくない方向で)使ってみた

動作環境

  • iMac + macOS Sierra
  • Node.js v7.3.0
  • 無線LAN

Dash ButtonをWi-Fiに接続する。

左上のメニューから
→アカウントサービス
→Dash端末→新しい端末をセットアップ
からWifiに接続しましょう。
例によって、商品選択の画面で作業を中断。
これでDashButtonがWi-Fiにつながりました。

Wi-Fiに接続設定されているときのボタン押下は白、
Wi-Fiに接続中が青、
Wi-Fiに接続されていないときボタン押下は赤。みたいな挙動でした。

MacにNode.jsをインストールする

恥ずかしながら、我が家のiMacには、Node.jsが入っていなかったので、そこからスタート。

1.Nodebrewをインストールする

$ curl https://raw.githubusercontent.com/hokaccha/nodebrew/master/nodebrew | perl - setup
$ nodebrew help
nodebrew 0.9.6

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (compile from source)
    nodebrew install-binary <version>     Download and install <version> (binary file)
    nodebrew uninstall <version>          Uninstall <version>
(省略)

2.node.jsをインストールする

nodebrewをアップデートして、最新版をインストールします。
こちらを参考にしました。
Macにnodebrew(node.js, npm)をインストールする手順

自分は、ここinstallでめっちゃ時間がかかるので、放置していたのですが、
途中でiMacから確認画面が表示されてて、後続の処理が落ちてました。
どうやら、C+?C++?インストールでだめだったみたい。
再実行で大丈夫でした。

$ nodebrew selfupdate       //nodebrewをアップデートする
$ nodebrew install latest   //最新版のnode.jsをインストールする。
$ nodebrew use latest       //最新版のnode.jsを使用する
$ node -v
v7.3.0
$ npm -v
4.0.5
$ npm install -g npm        //npmをアップデート

DashButtonのインストール

$ npm init
$ npm install --save dash-button
$ npm install date-utils

npm initでいろいろ聞かれるけど、全部空白にしてしまった。
調べてからコマンド発行しろよというかんじですが、
ここを参考にさせてもらって、プロジェクト作成コマンドみたいです。
ああ、ホームディレクトリでinitしたから、ホームディレクトリにpackage.jsonが作成されたのですね。

ついでにdate-utilsもインストールしておきましょう。

DashButtonの設定

上述の通り、initすると、package.jsonが生成されます。
installすると、dependencyが追記されるんでしょう、きっと。←
作成されたjsonファイルを

$ vim package.json
{
  "name": "user",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dash-button": "^2.0.1"
  }
}

から

$ vim package.json
{
  "name": "user",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "scan": "dash-button scan"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dash-button": "^2.0.1"
  }
}

へと書き換えます。
そして、scanを実行して、mac アドレスを取得しましょう。

$ sudo npm run scan
Password:

> user@1.0.0 scan /Users/user
> dash-button scan

Scanning for DHCP requests and ARP probes on en1...
Detected a DHCP request or ARP probe from f0:27:2d:89:38:e2
Detected a DHCP request or ARP probe from f0:27:2d:89:38:e2
Detected a DHCP request or ARP probe from f0:27:2d:05:c0:f4

手元にある2つのボタンを押したので、無事に2種類のマックアドレスが見えています。
これをメモしておきます。

app.jsを作成して、起動させてみる。

先程までで下準備は終了です。

さっそくapp.jsを作成し、起動させてみましょう。
これも先人たちの知恵を借りました。ありがとうございます。

app.js
require('date-utils');
const DashButton = require("dash-button");

const PHY_ADDR1 = "f0:27:2d:89:38:e2";
const PHY_ADDR2 = "f0:27:2d:05:c0:f4";

let button1 = new DashButton(PHY_ADDR1);
let button2 = new DashButton(PHY_ADDR2);

console.log("----Setup Start----")

let i = 0;

// 1つ目のボタン設定
button1.addListener(() => {
  var dt = new Date();
  console.log("button1 is  working."+dt.toFormat("YYYY/MM/DD HH24:MI:SS"));
});
console.log("----setup button1 complete----")

// 2つ目のボタン設定
button2.addListener(() => {
  console.log("button2 is  working."+dt.toFormat("YYYY/MM/DD HH24:MI:SS"));
});
console.log("----setup button2 complete----")
console.log("----Listening Start----")

そして実行します。
ボタンが押された時刻を記載するようにしてみました。
ですが、sudoじゃないとExceptionが発生。

$ sudo node ./app.js
----Setup Start----
----setup button1 complete----
----setup button2 complete----
----Listening Start----
button1 is  working.2016/12/24 11:35:21

とりあえず、無事にdash button のListenが実装できておりました。
簡単にサービスにリクエストとかも実装できそう。
やはりタイムラグがあるので、どんなおうちハックをするか。。。

1
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
1
3