Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Instruction Guide IR remote control without LIRC
#1
Big Grin 
Hi everyone!

TDLR: How to set up an infrared receiver, add/configure a remote and program the keys to control MoodeAudio without using LIRC.

I wanted to control my moodeaudio using a remote control and so far I've been using LIRC so after the last kernel updates it got so messy to set-it up that I just gave up on it. Personally, I'm much happier with this new solution however but please bare in mind that I'm a complete newbie with python and any help to improve what I'm sharing below is much appreciated. I might have forgotten to document something on the way, if so please let me know and I'll update the original thread.

Credits: This is based mainly on Lime-Parallelogram work and she goes through it in detail in this video.

My hardware:
Raspberry pi 4
A generic remote control
IR sensor TL1838
- 3 - Left - Out - GPIO 17 or pin 11 (you can use other GPIO but make sure to check the pin number)
- 2 - Middle - GND - pin 9
- 1 - Right - VCC 3.3v  - pin 1

1 - Connect the sensor to the raspberry
[Image: 2021-02-23_21_45_27-gpio_-_raspberry_pi_...tation.png]
2 - Set up the gpio and install dependencies

Code:
sudo apt install python3-gpiozero

Edit /boot/config.txt

Code:
sudo nano /boot/config.txt

add 

Code:
dtoverlay=gpio-ir,gpio_pin=17

Reboot

3.  Decode the remote(s) you wanna use

get the files and run the program

Code:
git clone https://github.com/Lime-Parallelogram/IR-Code-Decoder--
cd IR-Code-Decoder--
python CLI.py

You'll be asked to set the sensor pin. use the pin number (not GPIO number) in this case pin 11. Add a name for the remote (anything you want) and start adding keys.
Once you're done there will be a txt file with the remote name. open this file (e.g. nano remote.txt) and copy those values to some notepad.

4. Prepare the python script

download the script from Lime-Parallelogram

Code:
cd ~
wget https://raw.githubusercontent.com/Lime-Parallelogram/IR-Code-Referencer/10e7598d0e860675cd3ea7972661e9e9aa3e5a3c/Final-From-Video.py
mv Final-From-Video.py ir-scrypt.py

edit the script

Code:
nano ir-scrypt.py

firstly add the following line 'import subprocess' at the top of the script

Code:
#Imports modules
import RPi.GPIO as GPIO
from datetime import datetime
import subprocess

next, replace the list in the variables Buttons and ButtonsNames for the HEX codes and names you set up in your remote on step 3. In my case my remote has 5 buttons so I got in my txt;

Quote:Button Code - enter: 0x3482c20dfL
Button Code - up: 0x3482c40bfL
Button Code - left: 0x3482cc03fL
Button Code - right: 0x3482ca05fL
Button Code - down: 0x3482c609fL

therefore my variables in the script will become
Code:
Buttons = ["0x3482c20dfL","0x3482c40bfL","0x3482cc03fL","0x3482ca05fL","0x3482c609fL"]
ButtonsNames = ["enter","up","left","right","down"]

you can save it and confirm working by running it

Code:
python ir-scrypt.py

when pressing the buttons you should get the button name output.

4. Make the script control MPC

going back to our script (nano ir-scrypt.py) look for this code right in the end

while True:
Code:
   inData = convertHex(getBinary()) #Runs subs to get incomming hex value
       for button in range(len(Buttons)):#Runs through every value in list
               if hex(Buttons[button]) == inData: #Checks this against incomming

just after this, we can add some conditions to control MPC in moodeaudio. Probably there are much better ways how to tackle this. I managed to make it work by using conditions. Here's my example:
Code:
while True:
       inData = convertHex(getBinary()) #Runs subs to get incomming hex value
       for button in range(len(Buttons)):#Runs through every value in list
               if hex(Buttons[button]) == inData: #Checks this against incomming
                       if ButtonsNames[button] == "enter":
                               subprocess.call(["mpc", "toggle"])
                       elif ButtonsNames[button] == "up":
                               subprocess.call(["mpc", "prev"])
                       elif ButtonsNames[button] == "down":
                               subprocess.call(["mpc", "next"])
                       elif ButtonsNames[button] == "left":
                               subprocess.call(["mpc", "seek","-5%"])
                       elif ButtonsNames[button] == "right":
                               subprocess.call(["mpc", "seek","+5%"])

If you want more buttons just copy the elif and the line after it.
List of MPC commands you can change the subprocess.call as you see fit.

You can test and run it again 'python ir-scrypt.py'.

4. Make the scrypt run on boot

You can replace my_python for name:

Code:
sudo nano /lib/systemd/system/my_python.service

If your script is at the same location as above, when it was created then paste the code below otherwise adjust the location:

Code:
[Unit]
Description=My python script for OLED, buttons and remote control

[Service]
Type=simple
ExecStart=/usr/bin/python /home/pi/ir-scrypt.py

[Install]
WantedBy=multi-user.target

then run

Code:
systemctl daemon-reload
systemctl enable my_python.service
systemctl status my_python.service

you can reboot and the service should start.

I hope it works as well for you as it did for me Smile
Reply
#2
Works great!!  Thanks so much for posting this as the other lircd methods did not work for me either using an Apple A1294 remote with 7.1.
Reply
#3
I used the PiIR library to make a version of your script that barely uses any CPU and is much simpler.  PiIR requires the use of python3, so it can still be used along side your guide except change the references from "python" to "python3".  

Thought I would share it with everyone.

Code:
from piir.io import receive
from piir.decode import decode
import subprocess,piir,sys

# GPIO of IR receiver
pinr = 24

# unique IR snippet (not entire dict) in button press of apple remote
# captured using "test" mode of this script
ok = r"xee\x87\\\xab"
menu = r"xee\x87\x03\xab"
down = r"xee\x87\x0c\xab"
up = r"xee\x87\n\xab"
left = r"xee\x87\t\xab"
right = r"xee\x87\x06\xab"
play = r"xee\x87_\xab"

# if 'test' is entered as argument then only show remote button press codes
# ex: python3 moode_ir_control.py test
# used to capture button press snippets as shown in previous section
if (len(sys.argv) - 1):
    if sys.argv[1] == 'test':
        while True:
            print(decode(receive(pinr)))

while True:
   data = str(decode(receive(pinr)))
   if ok in data:
       subprocess.call(["mpc", "toggle"])
   elif menu in data:
       subprocess.call(["mpc", "stop"])
   elif down in data:
       subprocess.call(["mpc", "seek","-5%"])
   elif up in data:
       subprocess.call(["mpc", "seek","+5%"])
   elif left in data:
       subprocess.call(["mpc", "prev"])
   elif right in data:
       subprocess.call(["mpc", "next"])
Reply
#4
I wrote a simple project lately for IR remote control. It provides a simple configuration interface, has extensive configuration and supports Spotify.


https://github.com/Larvitar/MoodeIrRemote
Reply
#5
(06-08-2021, 09:40 PM)Larvitar Wrote: I wrote a simple project lately for IR remote control. It provides a simple configuration interface, has extensive configuration and supports Spotify.


https://github.com/Larvitar/MoodeIrRemote

This is beautiful. I now use it, its so much better Smile
Thank you!
Reply
#6
(06-08-2021, 09:40 PM)Larvitar Wrote: I wrote a simple project lately for IR remote control. It provides a simple configuration interface, has extensive configuration and supports Spotify.


https://github.com/Larvitar/MoodeIrRemote

Hi,
The IR remote control works, but only for a while. When I don't use the IR remote control for a few minutes, it doesn't work until I adjust the volume in the GUI Moode player. It is as if the system has put the process to sleep or something. What could be wrong?
Reply


Forum Jump: