Easy Setup IR Remote Control Using LIRC for the Raspberry PI (RPi) - Updated Oct 2021 [Part 2]

Introduction: Easy Setup IR Remote Control Using LIRC for the Raspberry PI (RPi) - Updated Oct 2021 [Part 2]

In Part 1 I demonstrated how to assemble the RPi + VS1838b and configure Raspbian's LIRC module to receive IR commands from an IR remote. All hardware and LIRC setup issues are discussed in part 1. Part 2 will demonstrate how to interface the hardware + LIRC module we setup in Part 1 with python3.

--------------Part 1----------------------|-----Part 2---------------

Remote -->[IR Receiver + RPI] <--> LIRC <--> Python3


Note: YOU MUST build with PYTHON3 (NOT python2 or python) or you will receive an error in the import in Line1. The LIRC module is only installed in Python3!

Supplies

none

Step 1: Hide Devinput.lircd.conf

Update: October 2021 This step is NOT required and can be SKIPPED!

Your remote configuration file(s) will be placed in the /etc/lirc/lircd.conf.d directory. LIRC will find any file in this directory as long as it has a .conf extension (ie: JVC.lircd.conf). We will not be using the devinput.lircd.conf file so we will hide it by changing the extension as follows by renaming devinput.lircd.conf to devinput.lircd.conf.copy

$ sudo mv /etc/lirc/lircd.conf.d/devinput.lircd.conf /etc/lirc/lircd.conf.d/devinput.lircd.conf.copy

Step 2: Download .conf File for Your Remote

By far the easiest way to obtain a remote .conf file is to download it from the huge library at lirc.sourceforge.net Even if your particular remote model is not listed it is VERY likely that another model from the same manufacturer will work fine. Just find the manufacturer of your remote and click on a model that is close. If you are unsure which model is close you can look at the contents of each model and find one that is close to yours.

The hard way to get a .conf file is to create your own using the built in tool irrecord but I was VERY unsuccessful trying to create a file using this utility despite much effort. There are lots of other folks with similar difficulties using irrecord and I highly recommend the easy route of downloading a .conf file from lirc.sourceforge.net

Your remote configuration file(s) will be placed in the /etc/lirc/lircd.conf.d directory. LIRC will find any file in this directory as long as it has a .conf extension (ie: JVC.lircd.conf).

Step 3: Python Code

Here is some code to test.

Update: October 2021

REMEMBER YOU MUST build with PYTHON3 (NOT python2 or python) or you will receive an error in the import in Line1. The LIRC module is only installed in Python3!

from lirc import RawConnection
def ProcessIRRemote():
       
    #get IR command
    #keypress format = (hexcode, repeat_num, command_key, remote_id)
    try:
        keypress = conn.readline(.0001)
    except:
        keypress=""
              
    if (keypress != "" and keypress != None):
                
        data = keypress.split()
        sequence = data[1]
        command = data[2]
        
        #ignore command repeats
        if (sequence != "00"):
           return
        
        print(command)        
            
#define Global
conn = RawConnection()
print("Starting Up...")
while True:         
      ProcessIRRemote()

Be the First to Share

    Recommendations

    • For the Home Contest

      For the Home Contest
    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Make It Bridge

      Make It Bridge

    31 Comments

    0
    ChadleyM
    ChadleyM

    3 years ago

    Hi, I've gotten it to work up until the python testing set... I'm getting a "cannot import name RawConnection". Has anyone gotten and/or solved this issue?

    0
    Rich101101
    Rich101101

    Reply 1 year ago

    Wih Buster the LIRC module is only installed under python3. The build must be with python3. Python2 or python builds result in an import error as described.

    1
    colin19
    colin19

    Reply 2 years ago

    I ran into the same issue installing on a Pi3 and the latest copy of the Buster. It turns out that you should not run your test code as root. The reason why seems to be explained here: https://sourceforge.net/p/lirc/tickets/341/
    When you do it delete an important file (_client.so). No idea why they did that. If you're in this situation then, as user Pi, run
    sudo apt-get remove lirc
    sudo apt-get install lirc
    sudo reboot
    and when done make sure that
    /usr/lib/arm-linux-gnueabihf/python3.7/site-packages/lirc/_client.so
    exists and all the other steps in the article have been completed. Then run
    python3 <yourtestfile>.py
    It will then say
    Starting Up...
    and it will respond to a configured remote control input.
    No obvious way to end the process, so
    ctrl/z
    ps
    kill -9 <pid>

    Hope this helps.

    0
    Rich101101
    Rich101101

    Reply 2 years ago

    Great info! As crazy as it sounds, Colin19 has found that LIRC setup fails if run as root and may explain the dreaded "cannot import name RawConnection" error. I always run as user PI and have not noticed this bug in LIRC setup. In any case its probably safer to run as user PI and avoid running as root. I will edit the tutorial to reflect this new information. Thanks!

    My only comment is that you state "It turns out that you should not run your test code as root." but I think you meant to say "you should not run LIRC setup as root".

    --Rich

    0
    colin19
    colin19

    Reply 2 years ago

    Happy I could help.
    Based on the description in the link, the file may well get deleted if you install as root. But I can confirm that it will also get deleted if you run the test code, as I indicated. Also note that the code seems to prefer being run by python3. You don't get the "cannot import name RawConnection" error,but if the file is missing it complains about _client.so missing, which makes sense. Even with the file there, running under python doesn't work. But bear in mind that I running Buster V5.4.x,
    Additionally, I was never able to successfully get lirc to work under Buster V5.4.x. There are a number of sites explaining how to do it, but all apply to v4.x. Apparently V5.x made some significant changes and although it seems to work, using irrecord for example, just returns zero codes. See https://raspberrypi.stackexchange.com/questions/104008/lirc-irrecord-wont-record-buster-mode2-works. I tried doing the build but apt refused to install the results.
    It would seem that LIRC has been replaced by ir-keytable, but it's not nearly as well supported with config files.
    BTW, I really am trying to control my Mitsubishi Ductless A/C and its IR codes are completely non-standard. But there is a Python package which looks promising. https://github.com/Ericmas001/HVAC-IR-Control and
    pip install git+https://github.com/Ericmas001/HVAC-IR-Control
    are what I am experimenting with. A bit off topic but it took me ages to find it, so another reference on the web might help someone else.

    0
    Rich101101
    Rich101101

    Reply 3 years ago

    Have you gone through all of the steps in Part1 AND checked there were no errors installing LIRC? and the import statement is typed correctly?

    0
    Sanerz
    Sanerz

    Reply 3 years ago

    Hi, I'm having the exact same issue. There does not seem to be much documentation online about this error. I have correctly done all steps in Part1 and all the above here. The python code was copy pasted, hence the import statement is also correct.

    0
    Rich101101
    Rich101101

    Reply 3 years ago

    This error is saying Python can't find the LIRC module named RawConnection. Did LIRC install correctly from Part 1? There must be NO errors.

    Stop, start and check status of lircd to ensure there are no errors.
    $ sudo systemctl stop lircd.service
    $ sudo systemctl start lircd.service
    $ sudo systemctl status lircd.service
    $ sudo reboot

    What are your results from the Testing step in Part 1?

    0
    Sanerz
    Sanerz

    Reply 3 years ago

    Finally i got it working after a lot of headaches! It seems that the LIRC distribution for Rasbian BUSTER needs to be built from scratch with a couple of patches for it to work. To do so, I followed these instructions: https://gist.github.com/billpatrianakos/cb72e984d4...

    Then I got so far that mode2 and irrecord were working but irw wasn't. This was due to an error in irrecord: it generated an output file with correct codes, but generated 2 codes for each button. Removing the second code (which was the same for all buttons) from the conf file got everything working.

    0
    ruediheimlicher
    ruediheimlicher

    Reply 2 years ago

    hi
    I got so far as sudo mode2 -d /dev/lirc0 is running and reporting the space and pulse data

    Running the python example with name checkapple.py I get the error
    A: python3:
    File "./checkapple.py", line 6, in <module>

    from lirc import RawConnection

    File "/usr/lib/python3/dist-packages/lirc/__init__.py", line 7, in <module>

    from .client import get_default_lircrc_path

    File "/usr/lib/python3/dist-packages/lirc/client.py", line 38, in <module>

    import _client

    ModuleNotFoundError: No module named '_client'

    B: python:
    Traceback (most recent call last):

    File "./checkapple.py", line 6, in <module>

    from lirc import RawConnection

    ImportError: No module named lirc

    shebang in both files set appropriate.

    Do I really have to rebuild lirc?

    0
    Sanerz
    Sanerz

    Reply 2 years ago

    have you tried running it with python3? if you have multiple versions installed, then this could be it... I have had the same issue.
    Also: be careful with your config.txt: there you should put the gpio-tx line FIRST, otherwise it won't work.
    Good luck

    0
    ruediheimlicher
    ruediheimlicher

    Reply 2 years ago

    thanks
    I tried with python3 with shebang
    #!/usr/bin/python3
    with
    sudo python3 checkapple3.py

    I got the error
    Traceback (most recent call last):
    File "checkapple3.py", line 6, in <module>
    from lirc import RawConnection
    File "/usr/lib/python3/dist-packages/lirc/__init__.py", line 7, in <module>
    from .client import get_default_lircrc_path
    File "/usr/lib/python3/dist-packages/lirc/client.py", line 38, in <module>
    import _client
    ModuleNotFoundError: No module named '_client'

    0
    Sanerz
    Sanerz

    Reply 2 years ago

    have you followed all the instructions in the above tutorial? Like I said: had to rebuild the lirc package with the patch provided for Buster.

    0
    ruediheimlicher
    ruediheimlicher

    Reply 2 years ago

    I installed LIRC following the mentioned instructions - my Raspi Z worked nearly an hour - and ggot it to work with my remote.
    Running the example sript gave me the same error as before:

    Traceback (most recent call last):

    File "remotetest.py", line 5, in <module>

    from lirc import RawConnection

    File "/usr/lib/python3/dist-packages/lirc/__init__.py", line 7, in <module>

    from .client import get_default_lircrc_path

    File "/usr/lib/python3/dist-packages/lirc/client.py", line 38, in <module>

    import _client

    ModuleNotFoundError: No module named '_client'
    adjusting -client to async_client in the script dist-packages/lirc/client.py and runnig remotetest again, I got
    Traceback (most recent call last):

    File "remotetest.py", line 5, in <module>

    from lirc import RawConnection

    File "/usr/lib/python3/dist-packages/lirc/__init__.py", line 7, in <module>

    from .client import get_default_lircrc_path

    File "/usr/lib/python3/dist-packages/lirc/client.py", line 38, in <module>

    import async_client

    File "/usr/lib/python3/dist-packages/lirc/async_client.py", line 43, in <module>

    from lirc.client import AbstractConnection as AbstractConnection

    ImportError: cannot import name 'AbstractConnection' from 'lirc.client' (/usr/lib/python3/dist-packages/lirc/client.py)

    It is not very important since my script works now.
    Thanks for your help.


    0
    fernandoa45
    fernandoa45

    2 years ago

    The easy way to create a .conf file for your remote, is to install OSMC a version from 05/2019, and trough a SSH connection create a file, because LIRC already is installing on OSMC

    0
    Atchoo78
    Atchoo78

    2 years ago

    Hello,

    (Aug. 31. 2020)

    I ran into an evil circle of unforeseen problems when trying to install python bindings for lirc in a virtualenv (see comment from Colin19). To put a long story short, here's how I solved it (works):

    Getting rid of the initially installed version:

    sudo apt remove lirc
    sudo apt remove python-pylirc
    pip3 uninstall lirc

    for Python 2.7:
    pip uninstall lirc

    Re-install
    sudo -H apt-get install lirc
    sudo -H apt-get install python-pylirc
    pip3 install lirc

    for Python 2.7:
    pip install lirc

    Make a backup of the "fresh" files, just in case:
    sudo cp -R /usr/lib/arm-linux-gnueabihf/python3.7/site-packages/lirc/ ~/backup

    Minor adjustments (or "Protecting your system from the system"):

    sudo nano /usr/lib/arm-linux-gnueabihf/python3.7/site-packages/lirc/paths.py

    remove the following section (line 12 through 16)
    if os.path.exists(os.path.join(HERE, '_client.so')):

    try:

    os.unlink(os.path.join(HERE, '_client.so'))

    except PermissionError:

    pass

    Press Ctrl-X and save.

    If everything went well, the script referenced above should now work as expected.

    virtualenv "hack":

    If you're trying to install this in a python virtual environment (virtualenv), do the following after reinstalling lirc, but before running any python code. Replace "VIRTENV" with your actual virtualenv name. I'll assume your virtualenv is located in the root of your home folder, just to make it as clear as I can.

    sudo cp -R /usr/lib/arm-linux-gnueabihf/python3.7/site-packages/lirc ~/VIRTENV/lib/python3.7/site-packages/lirc

    sudo -H /etc/init.d/lircd restart
    Lirc should now work within the virtualenv, given that you removed the part I mentioned from the paths.py file first. I don't know if this has any significance at all regarding this issue, but I also added my user to the "plugdev" system group, somewhere along this way:

    sudo usermod -G plugdev -a YOURUSERNAME

    You can of course skip the "Minor Adjustments" and apply that to the virtualenv copy only. Either way you're probably way better off than to begin with 😀

    Word of warning though: Software is changing constantly. This particular issue might be stale, old and non-existent by the time you're reading this.

    0
    DrReddog
    DrReddog

    2 years ago

    Just wanted to say thank you for saving me a great deal of time, when it came to converting my old lirc project to the latest Buster Raspbian. Much appreciated!

    0
    Rich101101
    Rich101101

    Reply 2 years ago

    Happy to help!

    0
    Rich101101
    Rich101101

    2 years ago

    Great info! As crazy as it sounds, Colin19 has found that LIRC setup fails if run as root and may explain the dreaded "cannot import name RawConnection" error. I always run as user PI and have not noticed this bug in LIRC setup. I will edit the tutorial to reflect this new information. Thanks!

    --Rich