LoginSignup
6
7

More than 5 years have passed since last update.

Do ButtonでRaspberry Piを操作する

Posted at

この記事はおうちハック Advent Calendar 2016の19日目の記事です。

目的

エアコンをプログラマブルに操作したい、という動機のもとに「任意のタイミングでRaspberry Piを手軽に操作する」ことを実現します。このtipsは下記のような目的にも使えます。

・任意のタイミングでRaspberry Piに命令を出したい
・ユーザが指示したときに特定サービス、コマンドを実行したい

必要なもの

・Raspberry Pi か 常時起動可能なPC
 (Apache HTTP Serverが動けばよいので、WindowsでもLinuxでも可能)
・グローバルIPアドレス
 (ルーターのポートフォワード等の機能で外部からRaspberry Piにアクセスできるようにしておく)
・iOS端末もしくはAndroid端末

実装方式概要

・Do ButtonからHTTPリクエストをRaspberry Piに送信
・ApacheにてDo Buttonからのリクエストを受け付け、ApacheのSSI機能を用いてRaspberry Pi上でコマンド実行

概要図
実装方式概要

設定方法

設定は下記の2つに対して必要です。
・Do Button
・Apache HTTP Server

Do Buttonの設定

IFTTT(iOS)(Android) をインストールの上、下記の通りに設定。
下記はiOS ver3.1.1にて確認
参考:IFTTT Help center

■設定一覧
This:Button widget
That:Maker
 URL:http://[RaspberryPiのグローバルIPアドレス]/filename.shtml
 Method:GET
 Content Type:text/plain

Apache HTTP Serverの設定

下記の設定手順、ファイルの変更内容を参考に設定を入れ、SSI機能を有効にする。
下記はRaspbian Jessie(Debian8)、Apache 2.4.10にて確認
参考:Apache チュートリアル: Server Side Includes 入門

設定手順
①Apache HTTP Serverをインストールする
$ apt-get install apache2

②SSI機能を有効にするために設定ファイルを編集する(下記参考)
$ vi /etc/apache2/apache2.conf

③SSI機能の有効化
$ a2enmod include; a2enmod cgid

④Raspberry Piに実行させたい内容をコマンドとしてshtmlファイルに記載(下記参考)
$ vi /var/www/html/filename.shtml

⑤Apache HTTP Serverを再起動する
$ systemctl restart apache2
/etc/apache2/apache2.conf
<Directory /var/www/>
        (~~省略~~)
        Options +Includes # この行を追加
</Directory>
/var/www/html/filename.shtml
<!--#exec cmd=“/path/to/cmd (any option)" -->

デモ動画

Raspberry PiからIRKitに連携し、エアコンを操作するデモ動画です。
右下の青色のLEDがIRKitです。
https://www.youtube.com/watch?v=YiEAQL1TJ2Y

補足

・ボタンを押してからの遅延は5秒程度です。(ちなみにDo ButtonのパケットはアメリカのAWSから来ます)
・暗号化も何もしていないので、このままでは途中経路でパケットをのぞき見されたら誰でもあなたの家のRaspberry Piにあらかじめ設定した操作を実行できます。気になる場合はオレオレ証明書を作成してhttpsリクエストを受けることで回避可能です。
・Do Buttonから送信したHTTPリクエストのステータスコードがエラーだとIFTTTから"エラーが起きました"メールが来ます。

6
7
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
6
7