Sunday 8 November 2020

Lirc and Mopidy for Raspberry Pi with Buster

Lirc  seems a lot easier to set up with Buster - if you are using it to control Mopidy it's a doddle to set up.

Set up is assuming you have set up Mopidy on a Raspberry Pi with Buster, you will also need to have installed mpc (sudo apt install mpc)

First of all you need to get your IR receiver, the kit here is what I've used.

To connect up to the GPIO pins there are lots of images here, I used pin 2 for 5v power, pin 6 for Ground and pin 12 (GPIO 18) for data/input. You can choose whichever pins suit, but remember which one you use for the data/input as you'll need to enter it in the conf later.

If using a similar receiver to the one in the link, the wiring is as follows, with the X on the receiver facing away from you;

Pin 1 is Ground, Pin 2 is Power and Pin 3 is Data/input.

If you have a lot of info etc on the sd card I would recommend backing up the card. 

Once you have the hardware in place and if relevant you've backed up the sd card.

Start here, ssh or open a terminal window, then;

sudo nano /boot/config.txt

Scroll down to this line, remove the # and enter the gpio pin number.

# Uncomment this to enable infrared communication.
dtoverlay=gpio-ir,gpio_pin=18
#dtoverlay=gpio-ir-tx,gpio_pin=17

ctrl x to save and close

sudo reboot

Once it's restarted

sudo apt update
sudo apt install lirc

Installation might fail on Buster here because of the .dist suffix, if so the following will help, then run the install lirc again.

sudo cp /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf
sudo apt install lirc

Once installed, open,

sudo nano /etc/lirc/lirc_options.conf

and change the following lines

driver = default
device = /dev/lirc0

ctrl + x to save and exit.

Now to set up the remote config files with the scan codes;

sudo nano ~/lircd.conf

If you are using the Keyes or similar remote you can try the codes below - if not you can either use irrecord to record your own, search sourceforge for similar brand or search google.

-------------------------

# brand:                      KEYES

# model no. of remote control:
# devices being controlled by this remote:

begin remote
  name  KEYES
  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       9281  4484
  one           622  1657
  zero          622   513
  ptrail        622
  repeat       9241  2236
  pre_data_bits   16
  pre_data       0xFF
  gap          109259
  toggle_bit_mask 0x0

      begin codes
          KEY_VOLUMEUP                   0x629D
          KEY_VOLUMEDOWN                 0xA857
          KEY_PREVIOUSSONG                 0x22DD
          KEY_NEXTSONG             0xC23D
          KEY_PAUSE                 0x02FD
          KEY_1                    0x6897
          KEY_2                    0x9867
          KEY_3                    0xB04F
          KEY_4                    0x30CF
          KEY_5                    0x18E7
          KEY_6                    0x7A85
          KEY_7                    0x10EF
          KEY_8                    0x38C7
          KEY_9                    0x5AA5
          KEY_0                    0x4AB5
          KEY_SHUFFLE         0x42BD
          KEY_NUMERIC_POUND        0x52AD
      end codes

end remote
--------------------------------------------------------------------
ctrl and x to save and exit

Now replace the original lircd.conf with the new one;

sudo cp ~/lircd.conf /etc/lirc/lircd.conf

and reboot
sudo reboot

Now test the remote by using irw, type irw at the command prompt, then point and press remote buttons, with a bit of luck you'll get something similar to this.


ctrl + c to exit irw 

Hopefully you've produced a similar result to the screenshot above.

Now to instruct what each button will do, if you use my existing file as a temp measure then adapt to your own needs using mpc, you will also need to run irexec.
Open a new file;

sudo nano /home/pi/.lircrc
and paste in the following
-----------------------------------

begin        
        prog = irexec
        button = KEY_PAUSE
        config = mpc toggle
end
begin
        prog = irexec
        button = KEY_NEXTSONG
        config = mpc next
end
begin
        prog = irexec
        button = KEY_PREVIOUSSONG
        config = mpc prev
end
begin
        prog = irexec
        button = KEY_NUMERIC_POUND
        config = shutdown –h now
end
begin
        prog = irexec
        button = KEY_SHUFFLE
        config = mpc shuffle
end
begin
        prog = irexec
        button = KEY_VOLUMEUP
        config = mpc volume +10
end
begin
        prog = irexec
        button = KEY_VOLUMEDOWN
        config = mpc volume -10
end
# insert your playlist name between the " " 
begin
        prog = irexec
        button = KEY_1
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end
# insert your playlist name between the " " 
begin
        prog = irexec
        button = KEY_2
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_3
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_4
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_5
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_6
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_7
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_8
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_9
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end

begin
        prog = irexec
        button = KEY_0
        config = mpc stop; mpc clear; mpc load "Your playlist here";mpc play;
end
---------------------------
ctrl + x and y to save and exit

Need to make sure irexec starts on reboot, easiest way I found was to add it to cron

sudo crontab -e
scroll to the bottom and add
irexec
ctrl + x then y to save and exit

Reboot

When it comes back up, open your chosen web client for mopidy, start a track playing then try and control it using the remote. Good luck