Raspberry PI を購入したので何かやってみようと思って、最近流行っているスマートスピーカーを作ることを目標に頑張ってみようかと思います。
第一弾は RPZ-IR-Sensor を利用して家電を操作してみようと思います。
準備するもの
- Raspberry PI 3 Model B 本体セット
- 本体
- 電源
- micro SD カード(32G)
- RPZ-IR-Sensor
- はんだごてセット
モニターやケーブル類は PC で利用していたものを流用しています。
OS のセットアップやはんだ付けについては何かのタイミングで備忘録的に記事にしていきたいと思います。
個々のセンサーを利用せずに RPZ-IR-Sensor を利用したのは All-in-One で様々なセンサーが付いているので GPIO に接続するだけで回路図とか考えなくていいかなと思って決めました。
OS などのバージョン確認
$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.1 (stretch)
Release: 9.1
Codename: stretch
2017/11 時点で最新の OS のバージョンで確認しています。
公式のページによると、OS のバージョンなどで動作保証はしていないようです。
概要
製品の開発元のサンプルコード(LIRC)を利用して家の照明のON/OFFを行います。
LIRC とは
LIRC はサポートしているリモコンのキーが押されたのをプログラムにあわせてコマンドに変換することができるデーモンです。ここで、「プログラムにあわせて」とは実行しているプログラムによってキーの押下で異なる動作をさせることができることを意味します。
※ archWiki 参考
照明の ON/OFF を行う
照明のリモコン
- ON : 照明をつける
- OFF : 照明を消す
- CHANGE : 一部の照明の ON/OFF を行う

公式サンプル
LIRC のインストール
$ sudo apt-get install lirc
LIRC の設定
マニュアルの通り、config.txt ファイルに設定値を追加します。
# Uncomment this to enable the lirc-rpi module
dtoverlay=lirc-rpi
dtparam=gpio_out_pin=13
dtparam=gpio_in_pin=4
マニュアルに記載された、hardware.conf ファイルは OS がバージョンアップ(stretch)されたため、lirc_options.conf に変更になりました。
driver と device の部分だけ変更してください。
[lircd]
nodaemon = False
driver = default
device = /dev/lirc0
output = /var/run/lirc/lircd
pidfile = /var/run/lirc/lircd.pid
plugindir = /usr/lib/arm-linux-gnueabihf/lirc/plugins
permission = 666
allow-simulate = No
repeat-max = 600
上記の設定が完了後、以下のコマンドで lircd を再起動してください。
$ sudo killall lircd
$ sudo lircd
赤外線受信
照明のリモコンから出る赤外線が受信できるか確認します。
$ mode2 -d /dev/lirc0
Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Using device: /dev/lirc0
space 2460566
pulse 1204
space 483
pulse 1206
space 472
pulse 370
space 1315
pulse 384
...
マニュアルとは違いますが、irrecord を利用してリモコン情報を学習します。
$ irrecord -n -f -d /dev/lirc0
Using raw access on device /dev/lirc0
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 at https://sf.net/p/lirc-remotes/wiki
If there already is a remote control of the same brand available at
http://sf.net/p/lirc-remotes you might want to try using such a
remote as a template. The config files already contains 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. Templates can be downloaded using irdb-get(1). You use a
template file by providing the path of the file as a command line
parameter.
Please take the time to finish the file as described in
https://sourceforge.net/p/lirc-remotes/wiki/Checklist/ an send it
to <lirc@bartelmus.de> so it can be made available to others.
Press RETURN to continue.
Checking for ambient light creating too much disturbances.
Please don't press any buttons, just wait a few seconds...
No significant noise (received 0 bytes)
Enter name of remote (only ascii, no spaces) :lightd
Using lightd.lircd.conf as output filename
Now start pressing buttons on your remote control.
It is very important that you press many different buttons randomly
and hold them down for approximately one second. Each button should
generate at least one dot but never 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.
................................................................................
Please enter the name for the next button (press <ENTER> to finish recording)
off
Now hold down button "off".
Please enter the name for the next button (press <ENTER> to finish recording)
on
Now hold down button "on".
Please enter the name for the next button (press <ENTER> to finish recording)
change
Now hold down button "change".
Please enter the name for the next button (press <ENTER> to finish recording)
Successfully written config file lightd.lircd.conf
学習しリモコン情報を LIRC の設定ディレクトリにコピーします。
$ sudo cp lightd.lircd.conf /etc/lirc/lircd.conf.d/
lircd を再起動します。
$ sudo killall lircd
$ sudo lircd
赤外線送信
照明を消す
$ irsend SEND_ONCE lightd off
照明をつける
$ irsend SEND_ONCE lightd on
照明を切り替える
$ irsend SEND_ONCE lightd change
$ irsend SEND_ONCE lightd change
$ irsend SEND_ONCE lightd change