LIRC: control your Raspberry with a remote
When I'm using my Raspberry as a Spotify Connect device , I'd like to control the playback easily without unlocking the phone. The best solution I found is to use the stereo remote.
We will take advantage of a infrared sensor connected to the GPIO pins available on the Raspberry, decoding the input signals with the tool LIRC.
Unfortunately, the Raspbian mantainers team is changing the support to LIRC, so most of the articles available online are out-to-date. The guide here is tested on Raspbian Buster and it's working!
First of all: materials needed.
- Raspberry (any version)
- Power adapter and cable
- Internet connection
- 3 jumper wires female-female
- IR receiver working at 38 kHz
The IR sensor I used has 2 power supply pins (Vs, pin 3, and GND, pin 2), that will be connected to a +3.3V pin and to a GND pin, and one communication pin (pin 1), that we will connect to pin GPIO 17. Be careful! Check the datasheet of your sensor to be sure.
Credits |
Connections recap:
Communication (pin 1)
|
GPIO17 (pin 11)
|
GND (pin 2)
|
GND (pin 6)
|
Vs (pin 3)
|
3.3V (pin 1)
|
![]() |
Credits |
Great! We completed the "hardware tasks", now switch on the Raspberry and let's configure the software.
As I said in the introduction, since the introduction of Raspbian Buster the procedure to let LIRC fully operating is a bit complex. Please follow step-by-step the instructions to avoid problems.
First, prepare the environment for the installation:
sudo su -c "grep '^deb ' /etc/apt/sources.list | sed 's/^deb/deb-src/g' > /etc/apt/sources.list.d/deb-src.list"
sudo apt update
sudo apt install devscripts
If you have a previous LIRC installation, please uninstall it:
sudo apt remove lirc liblirc0 liblirc-client0
Now we can face the effective installation of the working LIRC tool.
Warning! On my Pi2 this step took about 45 minutes.
sudo apt install dh-exec doxygen expect libasound2-dev libftdi1-dev libsystemd-dev libudev-dev libusb-1.0-0-dev libusb-dev man2html-base portaudio19-dev socat xsltproc python3-yaml dh-python libx11-dev python3-dev python3-setuptools
mkdir build
cd build
apt source lirc
wget https://raw.githubusercontent.com/neuralassembly/raspi/master/lirc-gpio-ir-0.10.patch
patch -p0 -i lirc-gpio-ir-0.10.patch
cd lirc-0.10.1
debuild -uc -us -b
cd ..
sudo apt install ./liblirc0_0.10.1-5.2_armhf.deb ./liblircclient0_0.10.1-5.2_armhf.deb ./lirc_0.10.1-5.2_armhf.deb
The last installing command should fail, this is the normal procedure.
Now add to the file
/boot/config.txt
the following lines
dtoverlay=gpio-ir,gpio_pin=17
dtoverlay=gpio-ir-tx,gpio_pin=18
(if you are using a different pin put here the corresponding number)
Now run these commands
sudo cp /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf
sudo cp /etc/lirc/lircd.conf.dist /etc/lirc/lircd.conf
sudo mv /etc/lirc/lircd.conf.d/devinput.lircd.conf /etc/lirc/lircd.conf.d/devinput.lircd.conf.dist
Now you can retry the installation command (transcribed here)
sudo apt install ./liblirc0_0.10.1-5.2_armhf.deb ./liblircclient0_0.10.1-5.2_armhf.deb ./lirc_0.10.1-5.2_armhf.deb
Now the proceduce should success.
To conclude the whole process, edit the file
/etc/lirc/lirc_options.conf
using these valuesdriver = default
device = /dev/lirc1
To check that the IR sensor is actually working run the command
mode2
, on the screen should appearUsing driver default on device /dev/lirc1
Trying device: /dev/lirc1
Using device: /dev/lirc1
Pressing a random button on the remote you should see a situation like this:
pulse 2393
space 602
pulse 1201
space 601
pulse 1209
space 596
pulse 595
space 605
pulse 595
space 604
pulse 1198
Great! Now it's all ready to set up our remote.
Using the command
irrecord
we will join a "meaning" to the specific sequence of pulse
and space
that corresponds to a choosen button. Follow the program instruction on the screen and write down the name that you use for your remote: we will need it later.Be careful! For buttons you can choose only the names that appear in this page: https://www.ocinside.de/modding_en/linux_ir_irrecord_list/
When the procedure is completed copy the file
remote-name.lirc.conf
in the LIRC folder:sudo cp remote-name.lirc.conf /etc/lircd/lircd.conf
Reboot the Raspberry.
Now, running the command
irw
and pressing random buttons you should see a thing like this:
000000000009cb9c 00 KEY_PAUSE remote-name
000000000009cb9c 01 KEY_PAUSE
remote-name
000000000009cb9c 02 KEY_PAUSE remote-name
000000000009cb9c 03 KEY_PAUSE remote-name
000000000004cb9c 00 KEY_PLAY remote-name
000000000004cb9c 01 KEY_PLAY remote-name
000000000004cb9c 02 KEY_PLAY remote-name
000000000004cb9c 03 KEY_PLAY remote-name
This means that the remote pairing procedure succeded!
And now? The last step.
Now we have to set up
irexec
such that, when we press a button, the Raspberry knows what to do!Go to the home folder and create (or edit) the file
.lircrc
cd ~/
nano .lircrc
For each button create a section like this one
begin
remote = remote-name
prog = irexec
button = KEY_NEXTSONG
config = echo “Next”
repeat = 0
end
In the
button
field put the name you gave to the corresponding button and in the field button
the command you want to run when you will press that button.Save the file and enable
irexec
to be run as a service:sudo systemctl enable irexec.service
If it doesn't work copy the file
.lircrc
in /etc/lirc
with the name irexec.lircrc
sudo cp ~/.lircrc /etc/lirc/irexec.lircrc
Reboot and... that's all folks!
Commenti
Posta un commento