LoginSignup
40
42

More than 5 years have passed since last update.

Raspberry Piで学習リモコン

Last updated at Posted at 2015-07-26

メモ書き程度に。
参考は2つ。
http://make.bcde.jp/category/27/
http://qiita.com/KAKY/items/55e6c54fa2073cdc0bbe

phpとApacheとlircをインストール

terminal
$ sudo apt-get install apache2
$ sudo apt-get install php5
$ sudo update-rc.d apache2 defaults
$ sudo apt-get install lirc

lircでの入出力に使うGPIOピンを指定

terminal
$ sudo modprobe lirc-rpi gpio_in_pin=24 gpio_out_pin=25

受信動作の確認

terminal
$ mode2 -d /dev/lirc0
(リモコンを赤外線受信モジュールに向けて何かボタンを押す)
pulse 399512
space 3458
pulse 1739
space 416
pulse 454
space 412
pulse 1322
space 409
pulse 459
space 409
pulse 459
space 408
pulse 476
space 391
pulse 455
 :

受信成功なら、上のように数字が出てくる

赤外線パターンファイルの作成

エアコン以外の機器はこの方法で登録できるはず。エアコンは後で。
まずはlircを止める。

terminal
$ sudo /etc/init.d/lirc stop

赤外線コードの出力先、ファイル名を指定(今回はtv.conf)

terminal
$ sudo irrecord -n -d /dev/lirc0 tv.conf

コマンドを実行すると以下の表示がされるため、以下の手順でリモコン学習を行う。

ここから下はhttp://make.bcde.jp/category/27/
をコピペしています。問題があれば報告ください。

1.Enterキーを2回押して、リモコン学習を開始します。
2.学習したいリモコンのボタンを何度も押して学習します。複数のボタンを学習したい場合は、学習したいリモコンのボタンを何度も押してください。
ボタンを押すたびに「.」が1個表示されるため、"Please keep on pressing buttons like described above."と表示されるまで、ボタンを押し続けます。
3."Please keep on pressing buttons like described above."が表示されたら、10秒間待ちます。
4."Please enter the name for the next button"が表示されたら、登録したいボタン名をを入力してEnterを押します。
5.登録したいリモコンボタンを押して、登録します。

terminal
?
irrecord -  application for recording IR-codes for usage with lirc

Copyright (C) 1998,1999 Christoph Bartelmus(lirc@bartelmus.de)

This program will record the signals from your remote control
and create a config file for lircd.

A proper config file for lircd is maybe the most vital part of this
package, so you should invest some time to create a working config
file. Although I put a good deal of effort in this program it is often
not possible to automatically recognize all features of a remote
control. Often short-comings of the receiver hardware make it nearly
impossible. If you have problems to create a config file READ THE
DOCUMENTATION of this package, especially section "Adding new remote
controls" for how to get help.

If there already is a remote control of the same brand available at
http://www.lirc.org/remotes/ you might also want to try using such a
remote as a template. The config files already contain all
parameters of the protocol used by remotes of a certain brand and
knowing these parameters makes the job of this program much
easier. There are also template files for the most common protocols
available in the remotes/generic/ directory of the source
distribution of this package. You can use a template files by
providing the path of the file as command line parameter.

Please send the finished config files to <lirc@bartelmus.de> so that I
can make them available to others. Don't forget to put all information
that you can get about the remote control in the header of the file.

Press RETURN to continue.
<<Enterキーを押す>>
Now start pressing buttons on your remote control.

It is very important that you press many different buttons and hold them
down for approximately one second. Each button should generate at least one
dot but in no case more than ten dots of output.
Don't stop pressing buttons until two lines of dots (2x80) have been
generated.

Press RETURN now to start recording.
<<Enterキーを押す>>
<<学習したいリモコンキーを全て押す>>
................................................................................
Found gap: 10239
Please keep on pressing buttons like described above.
<<ここで10秒待つ>>
.irrecord: no data for 10 secs, aborting
Creating config file in raw mode.
Now enter the names for the buttons.

Please enter the name for the next button (press <ENTER> to finish recording)
<<任意のボタン名を入力して、Enterキーを押す(ここではshutter)>>

Now hold down button "shutter".
<<学習したいリモコンのボタンを押して確認します>>
Got it.
Signal length is 85

Please enter the name for the next button (press <ENTER> to finish recording)
<<追加で学習するボタンがない場合は、Enterキーを押して終了します>>
<<追加で学習するボタンがある場合は、名前を入力して、Enterキーを押します>>

上記、コマンドを入力後、以下のようなファイルが生成されます。

terminal
?
begin remote

  name tv.conf
  flags RAW_CODES|CONST_LENGTH
  eps            30
  aeps          100

  gap          97792

      begin raw_codes

          name dengen
             1975   27526     403    1543     405    3513
              405

      end raw_codes

end remote

このファイルを /etc/lirc/lircd.conf にコピーして、LIRCから使えるようにします。

terminal
?
$ sudo sh -c "cat nikon_shutter.conf > /etc/lirc/lircd.conf"

/etc/lirc/lircd.confに#UNCONFIGUREDというコメントが残っているとLIRCが動かないので注意してください。

複数のリモコンを学習したい場合は、/etc/lirc/lircd.confに自分でコピペするとうまく行きました。わからない方はご連絡を。

起動時に自動的に動くように設定

terminal
$ sudo update-rc.d lirc defaults
$ sudo /etc/init.d/lirc start

以下のコマンドで、登録されたリモコン信号を送信できる。
lang:terminal
irsend SEND_ONCE tv.conf dengen

上記の方法で失敗したリモコンや、エアコンを登録する方法
私の場合、エアコンのリモコンが登録できなかったので、受信した赤外線をそのまま送信する方法をとった。

ここからはhttp://qiita.com/KAKY/items/55e6c54fa2073cdc0bbe
ページのコピペが入ります。

terminal
mode2 -d /dev/lirc0 | tee AIRON
(エアコンをonにするボタンを押す → Ctrl+C)

エディタでparse.rbを作成。
中身を以下にする

parse.rb
lines =  File.open(ARGV[0]).readlines #行ごとに配列として読み込み
lines_without_first = lines.slice(1..-1) #先頭の行は不必要なので省く
puts lines_without_first.map{|line|line.split(" ")[1]}.join(" ") #整形して表示
terminal
$ ruby parse.rb AIROFF
3458 1739 416 454 412 1322 409 459 409 459 408 476 391 455 431 436 408 465 413 448 407 477 396 453 412 454 413 450 415 1322 409 457 414 455 410 456 411 457 411 455 410 460 411 451 412 1326 410.....

ここで表示された数字の羅列は、先ほどの赤外線パターンファイルの中身の数字部分のみを並べたものです。
この数字の羅列を用いて、/etc/lirc/lircd.confに追加しましょう。

lircd.conf
# Please make this file available to others
# by sending it to <lirc@bartelmus.de>
#
# this config file was automatically generated
# using lirc-0.9.0-pre1(default) on 2015
#
# contributed by <% YOUR NAME %>
#
# brand: <% BRAND %>
# model no. of remote control: <% MODEL No. %>
# devices being controlled by this remote: <% DEVICE %>
#

begin remote

  name  aircon
  flags RAW_CODES
  eps            30
  aeps          100

  gap          200000
  toggle_bit_mask 0x0

      begin raw_codes

    name on
3465 1733 417 452 414 1322 410 454 411 458 418 445 413 454 421 447 411 496
370 454 429 442 418 445 413 456 416 461 396 1327 408 457 413 454 414 452
412 457 416 449 412 454 412 456 411 1326 409 1325 408 1324 435 431 412 452
...... 

      end raw_codes

end remote

数字の羅列の部分は先ほどのスクリプトで表示された数字の羅列を入れてください。(※1行があまりに長いとうまく動かないらしいので、80文字ごとを目安に改行を入れてください。)

上記までで、terminalを使ってリモコンを操作する方法が完成しました。
ここからは簡単にwebで使えるようにまとめます。

webからコントロールする

例によって、詳細が聞きたい方はご連絡ください。
javascriptとphpとajaxを使います。

phpは

api.php
<?php
//$data={$_POST["tvValue"])};
$tvText ="irsend SEND_ONCE orionTV.conf ".$_POST['tvValue'];
exec($tvText);
?>

と書きました。
javascriptは

script.js
$(function () {
    // 「#dengen」をクリックしたとき
    $('#dengen').click(function () {
        MainActivity.vibrate();
        // Ajax通信を開始
        $.ajax({
            url: 'api.php',
            type: 'post', 
            dataType: 'json',
        data: {
            'tvValue':'dengen'
            }
        })
    });
});

みたいな感じで書きました。

MainActivity.vibrate();

はAndroidのwebviewから使うときに、振動機能を実装するために使ったものなので、web上で使うかたは不要です。

php等の技術を理解せずに作成したものなので、書き方に問題はあるかもしれませんがとりあえず動きます。

Androidアプリ化は後日追記するかもしれません。

40
42
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
40
42