8

How can I control my TV using only a Raspberry Pi and the Infrared diode from an old remote control. Plus how can I interpret IR commands from an existing remote control.

Besi
  • 814
  • 4
  • 11
  • 22

3 Answers3

11

I can switch my TV on using the following command:

irsend SEND_ONCE LG_AKB72915207 KEY_POWER

This is how I set it up.

Hardware Setup

I am using a Raspberry Pi P1 2011.12.

The bare Infrared LED connected to GPIO#18 and GND. hardware

Installing lirc

sudo apt-get install lirc -y

Update /etc/modules

sudo nano /etc/modules

And add the following lines which results in

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
lirc_dev
lirc_rpi gpio_in_pin=4 gpio_out_pin=18

Please note that I'm not (yet) using the gpio_in_pin in my setup.

Update /etc/lirc/hardware.conf

This is my hardware.conf

# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS="--uinput"

#Don't start lircmd even if there seems to be a good config file
#START_LIRCMD=false

#Don't start irexec, even if a good config file seems to exist.
#START_IREXEC=false

#Try to load appropriate kernel modules
LOAD_MODULES=true

# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev 
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""

Update /boot/config.txt

sudo nano /boot/config.txt

Update the line regarding lirc-rpi module

# Uncomment this to enable the lirc-rpi module
dtoverlay=lirc-rpi,gpio_in_pin=4,gpio_out_pin=18
# dtparam=gpio_in_pull=up

Download the Remote Control Preset

At lirc.sourceforge.net/remotes you find a huge repository of remote controls along with the presets.

In my case the closest to my Remote Control LG AKB73275606 was the LG_AKB72915207. Which looks more or less like my remote control.

So I copied the contents of lirc.sourceforge.net/remotes/lg/AKB72915207 to /etc/lirc/lircd.conf:

sudo nano /etc/lirc/lircd.conf
#
# this config file was automatically generated
# using lirc-0.8.6(default) on Fri Sep 10 22:02:46 2010
#
# contributed by
#
# brand:                       TV LG 42"LD420
# model no. of remote control: AKB72915207
# devices being controlled by this remote:
#

begin remote

  name  LG_AKB72915207
  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       9056  4448
  one           601  1647
  zero          601   538
  ptrail        601
  repeat       9081  2199
  pre_data_bits   16
  pre_data       0x20DF
  gap          108229
  toggle_bit_mask 0x0

      begin codes
          KEY_POWER                0x10EF                    #  Was: power
          energy                   0xA956
          avmode                   0x0CF3
          input                    0xD02F
          tvrad                    0x0FF0
          KEY_1                    0x8877                    #  Was: 1
          KEY_2                    0x48B7                    #  Was: 2
          KEY_3                    0xC837                    #  Was: 3
          KEY_4                    0x28D7                    #  Was: 4
          KEY_5                    0xA857                    #  Was: 5
          KEY_6                    0x6897                    #  Was: 6
          KEY_7                    0xE817                    #  Was: 7
          KEY_8                    0x18E7                    #  Was: 8
          KEY_9                    0x9867                    #  Was: 9
          KEY_0                    0x08F7                    #  Was: 0
          KEY_LIST                 0xCA35                    #  Was: list
          q.view                   0x58A7
          KEY_VOLUMEUP             0x40BF                    #  Was: vol_up
          KEY_VOLUMEDOWN           0xC03F                    #  Was: vol_down
          KEY_CHANNELUP            0x00FF                    #  Was: ch_up
          KEY_CHANNELDOWN          0x807F                    #  Was: ch_down
          KEY_FAVORITES            0x7887                    #  Was: fav
          ratio                    0x9E61
          KEY_MUTE                 0x906F                    #  Was: mute
          KEY_MENU                 0xC23D                    #  Was: menu
          KEY_INFO                 0xD52A                    #  Was: guide
          q.menu                   0xA25D
          KEY_UP                   0x02FD                    #  Was: up
          KEY_DOWN                 0x827D                    #  Was: down
          KEY_LEFT                 0xE01F                    #  Was: left
          KEY_RIGHT                0x609F                    #  Was: right
          KEY_OK                   0x22DD                    #  Was: ok
          KEY_BACK                 0x14EB                    #  Was: back
          KEY_INFO                 0x55AA                    #  Was: info
          KEY_EXIT                 0xDA25                    #  Was: exit
          KEY_RED                  0x4EB1                    #  Was: red
          KEY_GREEN                0x8E71                    #  Was: green
          ellow                    0xC639
          KEY_BLUE                 0x8679                    #  Was: blue
          exit2                    0x04FB
          t.opt                    0x847B
          KEY_SUBTITLE             0x9C63                    #  Was: subtitle
          KEY_STOP                 0x8D72                    #  Was: stop
          KEY_PLAY                 0x0DF2                    #  Was: play
          KEY_PAUSE                0x5DA2                    #  Was: pause
          KEY_REWIND               0xF10E                    #  Was: <<
          KEY_FORWARD              0x718E                    #  Was: >>
          simplink                 0x7E81
      end codes

end remote

Now restart the lirc service:

sudo /etc/init.d/lirc restart

Sending commands

Now I'm able to switch my TV on and off like so:

irsend SEND_ONCE <DEVICE> <KEY>

namely:

irsend SEND_ONCE LG_AKB72915207 KEY_POWER

And voilà my TV switches on and off.




Update: Adding a receiver

Update /etc/modules

snd-bcm2835
lirc_dev
lirc_rpi gpio_in_pin=18 gpio_out_pin=17

Update /etc/lirc/hardware.conf

# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS="--uinput"

#Don't start lircmd even if there seems to be a good config file
#START_LIRCMD=false

#Don't start irexec, even if a good config file seems to exist.
#START_IREXEC=false

#Try to load appropriate kernel modules
LOAD_MODULES=true

# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev 
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""

Append the following to /boot/config.txt:

# LIRC
dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=17
dtparam=gpio_in_pull=up

To test the remote run the following, which will show you the pulses

sudo /etc/init.d/lirc stop
mode2 -d /dev/lirc0

To record a new remote use the following commands. Make sure to share your remote with the lirc community by sending the config to lirc@bartelmus.de.

irrecord --list-namespace # Shows the allowed identifiers for the commands
irrecord -f -d /dev/lirc0 ~/lirc.conf

Now to test the config you start the lirc service and run irw

#  sudo /etc/init.d/lirc start
[ ok ] Loading LIRC modules:.
[ ok ] Starting remote control daemon(s) : LIRC :.
root@phone2:~# irw
0000000008087887 00 KEY_POWER LG_6710CMAM09D
0000000008087887 01 KEY_POWER LG_6710CMAM09D
0000000008087887 02 KEY_POWER LG_6710CMAM09D
000000000808c837 00 KEY_PAGEUP LG_6710CMAM09D
000000000808c837 01 KEY_PAGEUP LG_6710CMAM09D
00000000080848b7 00 KEY_PAGEDOWN LG_6710CMAM09D
000000000808c837 00 KEY_PAGEUP LG_6710CMAM09D



Troubleshooting

Manually start the socket daemon

sudo lircd --device /dev/lirc0

Tutorials that helped me out

Error messages I got during my setup

irsend: hardware does not support sending
irsend: command failed: SEND_ONCE LG_AKB72915207 KEY_POWER
irsend: unknown remote: "/etc/lirc/lircd.conf"
lirc_rpi: gpio chip not found!

Other posts about lirc

lsmod

lsmod
...
lirc_dev               10211  1 lirc_rpi
rc_core                23776  1 lirc_dev

Showing errors related to the lirc process:

dmesg | grep -i lirc
[    5.118133] lirc_dev: IR Remote Control driver registered, major 244
[    7.148664] lirc_rpi: module is from the staging directory, the quality is unknown, you have been warned.
[    8.269729] lirc_rpi: auto-detected active high receiver on GPIO pin 4
[    8.270174] lirc_rpi lirc_rpi: lirc_dev: driver lirc_rpi registered at minor = 0
[    8.270188] lirc_rpi: driver registered!
[   16.197900] input: lircd as /devices/virtual/input/input0

Listing a remote:

Listing all remotes:

irsend LIST "" ""
irsend: LG_AKB72915207

Listing the commands for a given Remote

irsend LIST LG_AKB72915207 ""
irsend: 00000000000010ef KEY_POWER
irsend: 000000000000a956 energy
irsend: 0000000000000cf3 avmode
irsend: 000000000000d02f input
irsend: 0000000000000ff0 tvrad
irsend: 0000000000008877 KEY_1
irsend: 00000000000048b7 KEY_2
irsend: 000000000000c837 KEY_3
irsend: 00000000000028d7 KEY_4
irsend: 000000000000a857 KEY_5
irsend: 0000000000006897 KEY_6
irsend: 000000000000e817 KEY_7
irsend: 00000000000018e7 KEY_8
irsend: 0000000000009867 KEY_9
irsend: 00000000000008f7 KEY_0
irsend: 000000000000ca35 KEY_LIST
irsend: 00000000000058a7 q.view
irsend: 00000000000040bf KEY_VOLUMEUP
irsend: 000000000000c03f KEY_VOLUMEDOWN
irsend: 00000000000000ff KEY_CHANNELUP
irsend: 000000000000807f KEY_CHANNELDOWN
irsend: 0000000000007887 KEY_FAVORITES
irsend: 0000000000009e61 ratio
irsend: 000000000000906f KEY_MUTE
irsend: 000000000000c23d KEY_MENU
irsend: 000000000000d52a KEY_INFO
irsend: 000000000000a25d q.menu
irsend: 00000000000002fd KEY_UP
irsend: 000000000000827d KEY_DOWN
irsend: 000000000000e01f KEY_LEFT
irsend: 000000000000609f KEY_RIGHT
irsend: 00000000000022dd KEY_OK
irsend: 00000000000014eb KEY_BACK
irsend: 00000000000055aa KEY_INFO
irsend: 000000000000da25 KEY_EXIT
irsend: 0000000000004eb1 KEY_RED
irsend: 0000000000008e71 KEY_GREEN
irsend: 000000000000c639 ellow
irsend: 0000000000008679 KEY_BLUE
irsend: 00000000000004fb exit2
irsend: 000000000000847b t.opt
irsend: 0000000000009c63 KEY_SUBTITLE
irsend: 0000000000008d72 KEY_STOP
irsend: 0000000000000df2 KEY_PLAY
irsend: 0000000000005da2 KEY_PAUSE
irsend: 000000000000f10e KEY_REWIND
irsend: 000000000000718e KEY_FORWARD
irsend: 0000000000007e81 simplink

Displaying the lirc status:

The following command should not show any errors

sudo /etc/init.d/lirc status

This is what I see on my working setup:

$ sudo /etc/init.d/lirc status
● lirc.service - LSB: Starts LIRC daemon.
   Loaded: loaded (/etc/init.d/lirc)
   Active: active (running) since Wed 2017-08-09 08:21:34 UTC; 21min ago
  Process: 385 ExecStart=/etc/init.d/lirc start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/lirc.service
           ├─444 /usr/sbin/lircd --driver=default --device=/dev/lirc0 --uinput
           └─458 /usr/sbin/lircmd

Aug 09 08:36:22 JHD lircd-0.9.0-pre1[444]: accepted new client on /var/run/lirc/lircd
Aug 09 08:36:22 JHD lircd-0.9.0-pre1[444]: removed client
Aug 09 08:36:24 JHD lircd-0.9.0-pre1[444]: accepted new client on /var/run/lirc/lircd
Aug 09 08:36:25 JHD lircd-0.9.0-pre1[444]: removed client
Aug 09 08:36:26 JHD lircd-0.9.0-pre1[444]: accepted new client on /var/run/lirc/lircd
Aug 09 08:36:26 JHD lircd-0.9.0-pre1[444]: removed client
Aug 09 08:36:27 JHD lircd-0.9.0-pre1[444]: accepted new client on /var/run/lirc/lircd
Aug 09 08:36:27 JHD lircd-0.9.0-pre1[444]: removed client
Aug 09 08:36:29 JHD lircd-0.9.0-pre1[444]: accepted new client on /var/run/lirc/lircd
Aug 09 08:36:29 JHD lircd-0.9.0-pre1[444]: removed client

Badly formatted quotes taken from online tutorials

Badly formatted quotes in the config files caused some troubles that took me quite some time to resolve.

”/dev/lirc0″

instead of proper quotes

"/dev/lirc0"
Besi
  • 814
  • 4
  • 11
  • 22
3

@Besi thank you so much. It was really helpful for me.

For me I still have a problem when I reboot my rasp.

I think it's better to use to enable it on startup

systemctl enable lirc.service

than

sudo lircd --device /dev/lirc0

And now you needn't to edit the /etc/modules.

Here my install script :

#!/usr/bin/env bash

# Echo in Blue
function echoBlue () {
  echo -e "\x1B[0;34m\033[1m${1}\033[0m"
}

# Echo in Red
function echoRed () {
  echo -e "\x1B[0;31m\033[1m${1}\033[0m"
}

echoRed "For me PinIN=4 and PinOUT=26"

# Ask Pin Number
echoBlue "Pin In Number ?"
read pinIN
echoBlue "Pin OUT Number ?"
read pinOUT

# Install Lirc
echoBlue "Install Lirc"
sudo apt-get install lirc

# Load Module
# For raspberry instead use /boot/config.txt
#sudo cat >> /etc/modules <<EOF
#lirc_dev
#lirc_rpi gpio_out_pin=4 gpio_in_pin=26
#EOF

# Edit your /boot/config.txt by entering the command below
if ! grep -Fxq "dtoverlay=lirc-rpi,gpio_out_pin=$pinIN,gpio_in_pin=$pinOUT" /boot/config.txt; then
  echoBlue "Edit /boot/config.txt"
  cat >> /boot/config.txt <<EOF

dtoverlay=lirc-rpi,gpio_out_pin=$pinIN,gpio_in_pin=$pinOUT
EOF
else
  echoRed "Line already exist in /boot/config.txt"
fi

# Backup hardware.conf
echoBlue "Backup hardware.conf"
sudo cp /etc/lirc/hardware.conf /etc/lirc/hardware.bak.conf

# Add hardware
echoBlue "Create hardware.conf"
sudo cat > /etc/lirc/hardware.conf <<EOF
########################################################
# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS="--uinput"
# Don't start lircmd even if there seems to be a good config file
# START_LIRCMD=false
# Don't start irexec, even if a good config file seems to exist.
# START_IREXEC=false
# Try to load appropriate kernel modules
LOAD_MODULES=true
# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"
# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""
########################################################
EOF

# Now use systemcl instead of init.d
echoBlue "Stop lirc.service"
sudo systemctl stop lirc.service
echoBlue "Start lirc.service"
sudo systemctl start lirc.service
echoBlue "Status lirc.service"
sudo systemctl status lirc.service

# Launch it on startup
echoBlue "Launch it on startup"
echoBlue "o|O|y|Y ou n|N|*"
read repdrop
echo -e "\t"
case "$repdrop" in
  o|O|y|Y)
    sudo systemctl enable lirc.service
  ;;
  n|N|*)
    echoBlue "If you want to add it latter just run sudo systemctl enable lirc.service"
  ;;
esac

echoRed "You need to reboot pi"
echoRed "You have to check that /etc/lirc/lircd.conf exist or dl one on http://lirc.sourceforge.net/remotes"
0

Great tutorial, very detailed, but it didn't solve my problem. After hours trying I was still getting the "irsend: hardware does not support sending" message.

Finally I found this https://www.raspberrypi.org/forums/viewtopic.php?t=191425#p1213186

and it SOLVED my problem. To sum up...

sudo /usr/share/lirc/lirc-old2new

and restart lirc:

sudo /etc/init.d/lircd restart

Cheers, Daniel

Using RPi 3 lircd-0.9.4c