User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

[Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Thu Mar 07, 2019 1:42 pm

Notice: Now this works also for Raspbian Buster.

Kernel 4.19 does not include lirc_dev, so it is recommended to use gpio-ir.
viewtopic.php?f=29&t=224931&start=50

However, the command "irrecord" included in lirc does not work with gpio-ir
because the signal from /dev/lircX generated by gpio-ir is
slightly different from that of lirc_dev as follows:

lirc_dev:

Code: Select all

space XXX
pluse XXX
space XXX
...
space XXX
pulse XXX

gpio-ir:

Code: Select all

space XXX
space XXX
pulse XXX
space XXX
...
space XXX
pulse XXX
pulse XXX <- This shuold be "timeout"

Therefore, I created a patch to use irrecord with kernel 4.19.X and gpio-ir.
I also modified mode2 so that it yields the same signal as "ir-ctl -r". Instructions are shown below.

If you use "irrecord", "mode2", "irw", or "irexec" that receive IR signals, the building of patched sources shown below is required.
If you use only "irsend" that sends IR signals, the building is not required, so please install lirc with apt, and go the "After installing" section.
If your are using Raspbian Buster, the initial install will fail. It will success when you re-try the install after following "After installing" section.

Before installing:

Code: Select all

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 installed lirc with apt, please uninstall it as follows.

Code: Select all

sudo apt remove lirc liblirc0 liblirc-client0

Installing with a patch for gpio-ir in Raspbian Stretch:

Code: Select all

sudo apt build-dep lirc
mkdir build
cd build
apt source lirc
wget https://raw.githubusercontent.com/neuralassembly/raspi/master/lirc-gpio-ir.patch
patch -p0 -i lirc-gpio-ir.patch
cd lirc-0.9.4c
debuild -uc -us -b
cd ..
sudo apt install ./liblirc0_0.9.4c-9_armhf.deb ./liblirc-client0_0.9.4c-9_armhf.deb ./lirc_0.9.4c-9_armhf.deb 

Installing with a patch for gpio-ir in Raspbian Buster:

Code: Select all

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 final install command will fail. Then please configure the files shown below first, i.e., /boot/config.txt and /etc/lirc/lirc_options.conf. After that, please try the final install command again. Then the install will success.

After installing:
Please add the following lines to /boot/config.txt. You can change pin numbers.

Code: Select all

dtoverlay=gpio-ir,gpio_pin=24
dtoverlay=gpio-ir-tx,gpio_pin=25
You don't have to add anything to /etc/modules.

When using irrecord, mode2, irw, or irexec, /etc/lirc/lirc_options.conf should be editted. When you are using Raspbian Buster, first, please execute the following command.

Code: Select all

sudo cp /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf
sudo cp /etc/lirc/lircd.conf.dist /etc/lirc/lircd.conf
If you do not use devinput, please execute the following command.

Code: Select all

sudo mv /etc/lirc/lircd.conf.d/devinput.lircd.conf /etc/lirc/lircd.conf.d/devinput.lircd.conf.dist
After that, if you are using Raspbian Buster, please execute the final install command again here. Then the install will success.

Then, for all the Raspbian, please edit /etc/lirc/lirc_options.conf as follows. Restarting lirc is required. Examples of commands are: "irrecord -n", "mode2", "irw", and "irexec". Please note that there is no need to add "-d" option with the configuration below.

Code: Select all

driver = default
device = /dev/lirc1

When using irsend, please edit /etc/lirc/lirc_options.conf as follows. Restarting lirc is required. A example of commands is: "irsend SEND_ONCE TV power". Please note that there is no need to add "-d" option with the configuration below.

Code: Select all

driver = default
device = /dev/lirc0


Notice on lirc with Python3:
Lirc 0.10 installed in Raspbian Buster includes python3 module.
However, the new module has different API from that of previous python3-lirc.

The new API is shown in the following URL.
http://www.lirc.org/api-docs/html/group ... dings.html

So you have to modify your python3 script.


Notices on kernel:
When using irsend, you cannot send an IR sequcence that has more than 256 pulse/spaces because of the following limit defined in the kernel 4.19 (https://github.com/raspberrypi/linux/bl ... lirc_dev.c).

Code: Select all

#define LIRCBUF_SIZE	256

This limit does not exist in the previous kernel 4.14. LIRCBUF_SIZE would be increased to 1024 later by the kernel maintainer.

Moreover, you cannot send a sequence whose duration exceeds 500ms because of the following limit defined in the kernel (https://github.com/raspberrypi/linux/bl ... /rc-core.h).

Code: Select all

#define IR_MAX_DURATION         500000000	/* 500 ms */

This limit exists also in the previous kernel 4.14.

For example, the remote controller of my air conditioner has IR sequences with 439 pulse/spaces with a duration about 250[ms].
This signal exceeds the above LIRCBUF_SIZE, so it cannot be sent with kernel 4.19. It is also found that the limit of IR_MAX_DURATION is somewhat moderate.
Last edited by neuralassembly on Wed Sep 11, 2019 2:48 pm, edited 24 times in total.

thesheff17
Posts: 1
Joined: Mon May 27, 2019 12:15 pm

Re: Using LIRC with kernel 4.19.X and gpio-ir

Mon May 27, 2019 12:25 pm

After banging my head for hours and wondering why the IR blaster stopped working I finally found this post.

My pin out is a little different. In case anyone else stumbles onto this post. This is the IR blaster I'm using:
https://www.banggood.com/IR-Infrared-Re ... rehouse=CN

dtoverlay=gpio-ir,gpio_pin=18
dtoverlay=gpio-ir-tx,gpio_pin=17

Thank you very much for this. This helped so much! :D

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Tue May 28, 2019 3:40 am

The patch used above ( https://github.com/neuralassembly/raspi ... o-ir.patch )
works both for lirc_dev and gpio-ir.

So, I hope that this patch would be merged into the official deb package (lirc-0.9.4c).

The official source of lirc is not managed by github,
and its latest release (0.10.0) is ahead of deb package.
So, I think that merging into the deb package might be more realistic.

Does anyone know how to do it?

etjrowe
Posts: 1
Joined: Mon Jun 03, 2019 1:30 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Mon Jun 03, 2019 1:34 am

Thank you so much. I'm not completely new to Linux or the Raspberry Pi, but I'm new to GPIO and I'm no developer. So I've spend almost a week trying to follow different tutorials to get ir tx and rcv working to make an Alexa enabled remote. I had finally figured out that I needed to use gpio_ir, but couldn't get it to work. This was the missing piece! THANK YOU THANK YOU THANK YOU!

There is probably a simpler or more elegant way to accomplish this, but I wrote a shell script to toggle between "/dev/lirc0" and "/dev/lirc1" in lirc_options.conf

Code: Select all

#!/bin/bash
grep lirc0 /etc/lirc/lirc_options.conf
echo
echo "Toggling between lirc0/lirc1..."
echo
if [ $? -ne 0 ]
then
  sudo sed -i 's/lirc1/lirc0/g' /etc/lirc/lirc_options.conf
else
  sudo sed -i 's/lirc0/lirc1/g' /etc/lirc/lirc_options.conf
fi
cat /etc/lirc/lirc_options.conf
echo
echo "***Complete! See output above.***"
echo

Edit: Nevermind. This script doesn't work reliably and I'm not sure why.
Last edited by etjrowe on Tue Jun 04, 2019 7:50 pm, edited 2 times in total.

Leifberiksson
Posts: 4
Joined: Sat Jun 01, 2019 4:53 pm

Re: Using LIRC with kernel 4.19.X and gpio-ir

Mon Jun 03, 2019 7:22 pm

Thanks for this patch.
This works for me now (almost).

I can not use raw codes with irsend that are big/long.
Below is an example on a to long one. The example below is from a .conf file. If I make this slightly shorter will it work.
The error response is: Transmission failed
I can recorde a raw code that is longer than this.

Tested on a RPI 2.

Code: Select all

   name key_code_raw_to_long
       10000 10000 10000 10000 10000 10000
       10000 10000 10000 10000 10000 10000
       10000 10000 10000 10000 10000 10000
       10000 10000 10000 10000 10000 10000
       10000 10000 10000 10000 10000 10000
       10000 10000 10000 10000 10000 10000
       10000 10000 10000 10000 10000 10000
       10000 10000 10000 10000 10000 10000
       10000 10000
       10000

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Wed Jun 05, 2019 2:18 pm

I confirmed that the same problem happens also in my environment.

More precisely, the maximum number of pulse/space sequence is 256.
Actually, the number of sequence should be odd, so the max is 255.

The reason might be because the following definition
in the linux kernel (https://github.com/raspberrypi/linux/bl ... lirc_dev.c).

Code: Select all

#define LIRCBUF_SIZE 256

This definition did not exit in kernel 4.14.

So, in order to send long sequences,
it seems that kernel should be fixed.

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Wed Jun 05, 2019 4:53 pm

I created a new issue (https://github.com/raspberrypi/linux/issues/3002) in raspberrypi/linux to set LIRCBUF_SIZE 1024.

Leifberiksson
Posts: 4
Joined: Sat Jun 01, 2019 4:53 pm

Re: Using LIRC with kernel 4.19.X and gpio-ir

Wed Jun 05, 2019 7:51 pm

I think this is not the only problem.

I have much fewer pulses.
I tried and found out that my problem is that the total lenght of the sequency is larger than approx. 490000 sec.

With pulse/space of 10000s I could have approx. 51 (counting both space and pulse). a total of approx. 490000s
With pulse/space of 20000s I could have approx. 27. a total of approx. 490000s

With a real case was it not same result.
The limit was then: an average pulse/space of 830s I could have approx. 259. giving at total time of 214714s. I thought it was the total time that was the limit, but when reading previus post i would now guess that it is the 255 limit that has ocurred and that my 259 perhaps is slightly wrong.

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Thu Jun 06, 2019 12:48 am

The maximum of the total length of the sequence is 500ms.
This limitation is determined by the constant IR_MAX_DURATION defined in the kernel https://github.com/raspberrypi/linux/bl ... /rc-core.h.

Code: Select all

#define IR_MAX_DURATION         500000000	/* 500 ms */

This constant has also existed in the previous kernel 4.14. So, it is not the problem specific to the kernel 4.19.
And I think that this limitation is somewhat valid for the purpose of remote controller.

Moreover, there was a problem that lirc cannot read the long config file because the temporal buffer size was small.
It was fixed yesterday by updating the patch (https://github.com/neuralassembly/raspi ... o-ir.patch).
I increased the value of the constant LINE_LEN from 1024 to 2048.

Leifberiksson
Posts: 4
Joined: Sat Jun 01, 2019 4:53 pm

Re: Using LIRC with kernel 4.19.X and gpio-ir

Sat Jun 08, 2019 9:12 am

Sorry for my time definitions. It should of course be us and not sec. as you anyway understod

This is just a comment to perhaps not do more changes than necessarry
The total time limit problem is not for me a real problem. That limitation was found when i tried to analyze my real problem (the limit with max number of pulses).

gismatthew
Posts: 1
Joined: Tue Jun 11, 2019 12:03 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Tue Jun 11, 2019 12:09 am

neuralassembly wrote:
Thu Mar 07, 2019 1:42 pm
Kernel 4.19 does not include lirc_dev, so it is recommended to use gpio-ir.
https://lb.raspberrypi.org/forums/viewt ... 1&start=50
...
My remote stopped working and it's great to find this post. However I got this error when did `debuild -uc -us -b`. See below for details. Any ideas why? Thanks in advance!

Code: Select all

./html-source/configure.html:65: HTML parser error : Unexpected end tag : p
    the entry: </p>
                   ^
/bin/sed -i -e "s/@[email protected]/0.9.4c/" html/configure.html
Traceback (most recent call last):
  File "./data2table", line 10, in <module>
    from lirc import database
ImportError: cannot import name 'database'
Traceback (most recent call last):
  File "./data2hwdb", line 10, in <module>
    from lirc import database
ImportError: cannot import name 'database'
xsltproc --html docpage.xsl \
    $(echo ./html/driver-api.html | /bin/sed 's|html/|html-source/|') > html/driver-api.html
-:1: HTML parser error : Document is empty

^
Makefile:1245: recipe for target 'lirc.hwdb' failed
make[3]: *** [lirc.hwdb] Error 1
make[3]: *** Waiting for unfinished jobs....
/bin/sed -i -e "s/@[email protected]/0.9.4c/" html/driver-api.html
find api-docs -name \*.tmp -delete
tar czf api-docs.tar.gz api-docs
make[3]: Leaving directory '/home/pi/build/lirc-0.9.4c/doc'
Makefile:647: recipe for target 'all-recursive' failed
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory '/home/pi/build/lirc-0.9.4c'
Makefile:444: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/home/pi/build/lirc-0.9.4c'
dh_auto_build: make -j4 returned exit code 2
debian/rules:9: recipe for target 'build' failed
make: *** [build] Error 2
dpkg-buildpackage: error: debian/rules build gave error exit status 2
debuild: fatal error at line 1116:
dpkg-buildpackage -rfakeroot -us -uc -b failed

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Tue Jun 11, 2019 7:22 am

The error message "ImportError: cannot import name 'database'"
shows that your python3 cannot read the following files.

Code: Select all

build/lirc-0.9.4c/tools/lirc-setup/database.py

Code: Select all

build/lirc-0.9.4c/debian/lirc/usr/lib/arm-linux-gnueabihf/python3/dist-packages/lirc/database.py

The first one is created when executing "apt source lirc".
The second one is copied from the first one when executing "debuild -uc -us -b".

So, the reason for this error would be either of the followings:

1. The install commands are not correctly executed.

2. Some misconfigurations exist in your python3 environment.
For example, using pyenv or something like that.
I am using the python3 that is pre-installed in Raspbian,
and there is no such a trouble.

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: Using LIRC with kernel 4.19.X and gpio-ir

Tue Jun 25, 2019 4:26 pm

Now this method works also for Raspbian Buster.

hamdi_yavuz
Posts: 7
Joined: Sat Sep 08, 2018 9:08 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Wed Jun 26, 2019 1:38 pm

Hello,

It is not working on Buster's latest version (2019-06-20-raspbian-buster-full.img), do you have any information about any plan for working LIRC version on Buster?

Thank you.
Hamdi

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Wed Jun 26, 2019 3:34 pm

Please follow the steps shown at the top of this thread.

hamdi_yavuz
Posts: 7
Joined: Sat Sep 08, 2018 9:08 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Thu Jun 27, 2019 8:28 am

I will try it with fresh copy of Buster on Raspberry Pi 4, thank you.

hamdi_yavuz
Posts: 7
Joined: Sat Sep 08, 2018 9:08 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 8:23 am

Hello,

I have tried on Raspberry Pi 4 Buster, it is working, Thank you neuralassembly.

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 9:28 am

I do not have Raspberry Pi 4 yet.
Moreover, I cannot understand what happens without explanations of phenomenon, error messages, and so on.

hamdi_yavuz
Posts: 7
Joined: Sat Sep 08, 2018 9:08 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 9:35 am

Most probably I had mistake in first try, so second try is ok, but I could not use lirc.init function in Python3 code, it is giving " module 'lirc' has no attribute 'init' ", any idea?


Thanks

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 10:12 am

I do not use python module for lirc, but when I tried to import lirc from python3,
there were no errors.

Code: Select all

[email protected]:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lirc
>>>
This would be because "/usr/share/lirc/python-pkg/lirc/__init__.py" exists.

So, I think that it is not a problem of lirc.
Your environment of python3 might have troubles,
e.g., the existence of lirc.py in your working directory.

hamdi_yavuz
Posts: 7
Joined: Sat Sep 08, 2018 9:08 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 10:38 am

Thank you for your reply, it is not loader error, it is run time error as you seen below:

Code: Select all

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/home/pi/.linuxraw/bgfapp.py", line 144, in play_video
    sockid = lirc.init("myp")
AttributeError: module 'lirc' has no attribute 'init'
if lirc.init("myp") functions called in Python the error occurs.

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 11:06 am

I think you are talking about python-lirc package.
However, lirc 0.10 installed in Raspbian Buster includes python3 module.
Maybe the new module has different API from that of python-lirc.

The new API is shown in the following URL.
http://www.lirc.org/api-docs/html/group ... dings.html

Please do not ask the python problem here because I want to discuss about the installation here.


Snerler
Posts: 9
Joined: Wed Feb 10, 2016 2:20 pm

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 3:02 pm

I'm getting two lirc entries in /dev, /dev/lirc0 and /dev/lirc1.

I'm able to read remotes on /dev/lirc1 using mode2 and haven't tried transmitting yet. Most instructions online mention only /dev/lirc0.

Is that normal or something wrong with my setup? Do I transmit with lirc0 and receive with lirc1?

Using pi Stretch.

User avatar
neuralassembly
Posts: 22
Joined: Wed Sep 30, 2015 4:23 am

Re: [Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir

Fri Jun 28, 2019 3:22 pm

Both lirc0 and lirc1 exist when you use lirc with kernel 4.19 and gpio-ir.
Kernel 4.19 became available with apt command in (maybe) March, so there are few documents on that.

Return to “Troubleshooting”