Raspberry Piを使ったおうちハック第一弾。
玄関に設置したAmazon Dash Buttonを押すとLineに帰宅をメッセージを投稿できるようにしてみました。
Raspberry Pi(サーバー)とIFTTT(アプリ)を使用しています。
iOTボタン(Amazon Dash Button)の設定
iOTボタン(Amazon Dash Button)の設定をまとめました。
iOTボタン(Amazon Dash Button)の購入
プライム会員しか購入できません。今回のために1ヶ月無料のプライム会員に。
Amazon Dash Buttonの設定
amazonアプリを携帯にインストール
あとはメニュー>アカウントサービス>Dash端末>新しい端末をセットアップからガイドに従う。
商品を選択するところで、終了します。
Raspberry Piの設定
まずはこちらのドキュメントを確認することを推奨します!
GitHub – maddox/dasher: A simple way to bridge your Amazon Dash buttons to HTTP services
npmのインストール
sudo apt-get install libpcap-dev
sudo apt-get install npm
※libpcap-devでインストールエラーが起きた場合
メモ
特定のファイルがなんらかの原因で壊れている可能性が高い場合は
cd で指定のフルダの位置へ
sudo mv 元ファイル名 元ファイル名. brokenとかリネームして
再度インストールをする
Node.jsのインストール
sudo apt-get install node
wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
Dasherのインストール
git clone https://github.com/maddox/dasher.git
cd dasher
npm install
Amazon Dash ButtonのMacアドレスを調べる
script/find_button
※sudo ./script/find_button
と書いてある記事が多いのですが、上記のコマンドで上手く行きました。
※FingというアプリでもMacアドレスを調べることができます。
Slackに投稿
先にSlackにてテストしてみます。
config.jsonの作成
config.json
というファイルを作成。
config.jsonに記述
addressはAmazon Dash ButtonのMacアドレス
urlはSlackのメッセージ送信用URL、取得は下記のURLから
{"buttons":[
{
"name" : "slack",
"address": "xx:xx:xx:xx:xx:xx",
"url": "https://hooks.slack.com/services/{KEY}",
"method": "POST",
"json": true,
"body": {"text":"テスト!"}
}
]}
npmを起動
npmを起動します。
エラーが出る場合は、config.json
のコードを疑ってみてください。
cd dasher sudo npm run start
Amazon Dash Buttonボタンを押してslackに投稿
slackに投稿されているのを確認します。
IFTTTの設定
IFTTTを使ってLineに投稿できるようにします。
Maker WebhooksとLINEをコネクト
New Applet
New Appletを選択、thisを Maker Webhooks、thatをLINEにして作成
投稿URLの確認
Maker Webhooksに載っているURLへ飛び、投稿URLを確認。
※アクセス先で、触っている内にURLが変わることがあるので要注意。
Lineに投稿
Line用にconfig.jsonを記述
config.json
を下記のように記述
※urlには投稿URLを記入(途中イベント名も記述します)
{"buttons":[
{
"name" : "send_line_message",
"address": "XX:XX:XX:XX:XX:XXB",
"url": "https://maker.ifttt.com/trigger/send_line_message/with/key/{KEY}",
"method": "POST",
"json": true,
"body": {"text":"テスト!"}
}
]}
Amazon Dash ButtonとIFTTTでLINEにメッセージを送信する – 気になるガジェット
※参考の記事通りにするとエラーが出たので、必要最低限にしました。(これに気がつくのに時間がかかりました><)
npmを起動
npmを起動します。
エラーが出る場合は、config.json
のコードを疑ってみてください。
cd dasher
sudo npm run start
下記のようなエラーが出る場合
インストール関連が基本、問題なく。さらに Slackで投稿できるのに、Lineの場合にエラーが出る場合はconfig.jsonが正しく記述されていない場合があります。
module.js:453
throw err;
^
SyntaxError: /home/pi/dasher/config/config.json: Unexpected token }
at Object.parse (native)
at Object.Module._extensions..json (module.js:450:27)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/home/pi/dasher/app.js:2:14)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
npm ERR! Linux 4.9.24+
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start"
npm ERR! node v4.2.1
npm ERR! npm v2.14.7
npm ERR! code ELIFECYCLE
npm ERR! dasher@1.4.1 start: node app.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the dasher@1.4.1 start script 'node app.js'.
npm ERR! This is most likely a problem with the dasher package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system: npm ERR! node app.js
npm ERR! You can get their info via: npm ERR! npm owner ls dasher
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request: npm ERR! /home/pi/dasher/npm-debug.log
OS起動時に起動
こちらを参照します。
Running Dasher on a Raspberry Pi at startup · maddox/dasher Wiki · GitHub
sudo nano /etc/init.d/dasher
#!/bin/sh
### BEGIN INIT INFO
# Provides:
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
dir="/home/pi/dasher"
cmd="DEBUG=* node app.js"
user="root"
name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
cd "$dir"
if [ -z "$user" ]; then
sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
else
sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
fi
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
kill `get_pid`
for i in {1..10}
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
Ctrl+X→Y→Enterで保存
sudo chmod 755 /etc/init.d/dasher
sudo update-rc.d dasher defaults
上記のコマンドを入力
これでOS起動時にDasherのプログラムが起動します。
テストしたいときは sudo /etc/init.d/dasher start
を入力
Amazon Dash Buttonを押してlineに投稿
