大阪近郊で活動するフリーランスエンジニアのブログです 技術情報とか趣味など

*

Raspberry Pi2でリモコンを使って水槽の照明をつける

      2015/04/24

リモコン操作で照明をつける

Web上からRaspberry Pi2で水槽の照明をリモコン操作でON/OFFするの為、通販で買ったものが届いたので早速試す

買ったのはこちら


回路組み立て

Make.さんのブログを参考に必要な部品を秋月で購入
ブログ内容を見ながら組み立ててみる
00046

eoRaw

ソフト設定

LIRCのインストールと設定

リモコンを操作するライブラリ LIRCをインストールする

sudo apt-get install lirc

ハードの設定を変更する

sudo vi /etc/lirc/hardware.conf

LIRCD_ARGS="--uinput"
DRIVER="default"
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

モジュールの追加を行い再起動

sudo vi /etc/modules


lirc_dev
#inが受信機 outが送信LED
lirc_rpi gpio_in_pin=17 gpio_out_pin=18

sudo reboot

再起動後、モジュールが追加されているか確認する

lsmod | grep lirc

・・・が、表示されないので調べているとビクトリーセブンさんのブログに行き着く

※Raspbian 2015-01-31(NOOBS 1.3.12)以降では、 /etc/modules ファイルではなく /boot/config.txt ファイルに下の行を追加します。
となっていた為設定の追加と再起動を行う

sudo vi /boot/config.txt 

dtoverlay=lirc-rpi,gpio_in_pin=17,gpio_out_pin=18

sudo reboot

再起動後、再度確認する

lsmod | grep lirc

lirc_rpi                6646  0 
lirc_dev                8181  1 lirc_rpi
rc_core                16932  1 lirc_dev

ls -l /dev/lirc*

crw-rw---T 1 root video 246, 0  1月  1  1970 /dev/lirc0

ちゃんと認識されてるっぽい

リモコンからの受信できているか確認する

sudo /etc/init.d/lirc stop
mode2 -d /dev/lirc0

#リモコンを受信機に向けて何かボタンを押す
space 2655
pulse 822
space 888
pulse 822
space 841
pulse 817
space 1903
pulse 819
space 1905

謎の数字がズラッと出れば受信できてるようなので一旦Ctrl+Cで終了する

リモコンコードの学習

コンセントのON/OFFにする信号を学習させpower.confに保存する

sudo irrecord -n -d /dev/lirc0 power.conf
  1. Enterキーを2回押して、リモコン学習を開始します。
    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.
    
    
    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. #←学習開始
    
  2. 学習したいリモコンのボタンを何度も押して学習します。
    ボタンを押すたびに「.」が1個表示されるため、”Please keep on pressing buttons like described above.”と表示されるまで、ボタンを押し続けます。

    ................................................................................ #← ON OFF 押したあとONボタン押しっぱ
    Found gap: 53955
    Please keep on pressing buttons like described above.
    
  3. “Please keep on pressing buttons like described above.”が表示されたら、10秒間待ちます。
    Please keep on pressing buttons like described above.
    irrecord: no data for 10 secs, aborting
    Creating config file in raw mode.
    
    
  4. “Please enter the name for the next button”が表示されたら、登録したいボタン名を入力してEnterを押します。
    Now enter the names for the buttons.
    
    Please enter the name for the next button (press <ENTER> to finish recording)
    power_on #←名前をいれる
    
    

    登録したいリモコンボタンを押して、登録します。

    Please enter the name for the next button (press <ENTER> to finish recording)
    power_on #←名前を登録する
    
    Now hold down button "power_on". #←power_onとして登録したいボタンはどれか押せと言われるのでリモコンのONボタンを押す
    Got it.
    Signal length is 19
    
    Please enter the name for the next button (press <ENTER> to finish recording)
    power_off #←続いてをoffを登録する
    
    Now hold down button "power_off". #←OFFボタンを押す
    Got it.
    Signal length is 19
    Please enter the name for the next button (press <ENTER> to finish recording)
    #enterで終了
    
    

作ったファイルを確認する

cat power.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 Thu Apr 23 21:59:27 2015
#
# contributed by 
#
# brand:                       power.conf
# model no. of remote control: 
# devices being controlled by this remote:
#

begin remote

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

  gap          77611

      begin raw_codes

          name power_on
             2527    2707     794     889     823     862
              822    1982     742    1905     819    1899
              824    1922     807     854     824     861
              852

          name power_off
             2558    2674     823     863     824    1897
              822     864     824     864     822    1924
              799     861     823    1900     825    1900
              861

      end raw_codes

end remote

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

sudo sh -c "cat power.conf > /etc/lirc/lircd.conf"

LIRCの起動

パイ起動時にLIRCが動くように登録する

sudo update-rc.d lirc defaults

#学習用に停止していたので起動
sudo /etc/init.d/lirc start

信号送信

登録したリモコン信号の種類を確認する

irsend LIST power.conf ""

irsend: 0000000000000001 power_on
irsend: 0000000000000002 power_off

学習した信号が送信できるか確認する

irsend SEND_ONCE power.conf power_on

kitaaaaaa
キタアアアアアアアアアアアアア!

照明操作のシェルを置き換える

Raspberry Pi2側

Web上やターミナルから叩いてる照明操作のシェルを置き換える

#旧ファイルをバックアップ
sudo cp /usr/local/bin/led-on ~/backup
sudo cp /usr/local/bin/led-off ~/backup
sudo cp /usr/local/bin/led-20sec ~/backup

#それぞれ書き換え
sudo vi /usr/local/bin/led-on

#!/bin/sh
irsend SEND_ONCE power.conf power_on

sudo vi /usr/local/bin/led-off

#!/bin/sh
irsend SEND_ONCE power.conf power_off

sudo vi /usr/local/bin/led-20sec

#!/bin/sh
irsend SEND_ONCE power.conf power_on
sleep 20s
irsend SEND_ONCE power.conf power_off

さくらVPS側

点灯ボタンを制御しているPHPを修正
現在時刻が定期点灯の10:00から19:00の間だった場合、点灯(20秒後消灯)キューを送らない

if (date('Hi') > 1000 && date('Hi') < 1900 ) {
    exit;
}

定期点灯の時間帯を変更すると当phpファイルも修正しなきゃいけない下策ではあるがまぁいいか・・・
GPIO側から現在スイッチがONなのかOFFなのか判別する方法がないので信号が届かなかった場合に自動でリトライできないのが問題か
まぁ所詮照明のON/OFFなので良いことにする

 - Raspberry Pi, プログラミング, 金魚

  • このエントリーをはてなブックマークに追加
  • にほんブログ村 観賞魚ブログ 金魚へ

Comment

  1. […] してから、lircの設定をする。 Raspberry Pi2でリモコンを使って水槽の照明をつけるが大変詳しいです。 リモコンがちゃんと受信できてるかもチェック。 […]

夏の不眠解消!raspberry pi でエアコンの自動制御 へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

  関連記事

みんなのラズパイコンテストにて受賞しました

ラズパイマガジンと日経Linux、日経ソフトウエア様の みんなのラズパイコンテス …

Raspberry Pi2でプラ船のカメラを動かす

カメラの首振り プラ舟内に設置しているカメラにサーボを取り付けて遠隔からカメラの …

プラ船の金魚がネコに襲撃された

ガレージに出してるプラ船がネコにやられました・・・ これはひどい どうもカメラを …

ウチの金魚たち

ウチに置いてある水槽の紹介 仕事場水槽 金魚水槽のページで配信している60cm水 …

Raspberry Pi2で外部から水槽のLEDを点灯する その3

前回で自宅のラズパイ側の準備が整ったので今回はWeb上から電灯のON/OFFをす …

穂竜はじめました

先日新しく45cm水槽を立ち上げてまして穂竜という金魚を飼いはじめました。 高頭 …

プラ舟に外部フィルター追加

安かったのでつい・・・ ヤフオクで安い中古の外部フィルターが売っていたので衝動買 …

Raspberry Pi2で水槽のLEDを自動点灯/消灯する

先日Raspberry Pi2を購入し、自宅からのストリーミング配信する為あれこ …

Raspberry Pi ZERO + L-03D + IIJmioを使ってネットに接続する

野外のソーラーパネル&ラズパイからネットに繋ぎたい ソーラーパネルで給 …

金魚を水槽に戻しました

病気のため水槽をリセットし、隔離していた金魚を戻しました ついでに照明のLEDも …