IR remote control in OpenWrt
Using of IR-connection can be an interesting development of router capabilities. There are many ways to use remote management capabilities and I think you can decide by yourself where to apply this option. My task is to show how easy it is to add this functionality to a router.
Some theory ))...
Infrared link (IR) consists of 2 parts at least: there are receiver and transmitter. It is very easy to build both receiver and transmitter by yourself using different ready-made solutions from Internet. But I suggest taking this one, ready-to-use.
As a transmitter is here the remote control and as a receiver is here the second part which is connected to the usb-input.
The program LIRC is responsible for working of IR link in Linux. LIRC is included in the configuration of OpenWrt. Therefore on the stage of configuration OpenWrt the following modules are to be selected:
Base system:
<*> busybox:
Linux System Utilities:
<*> lsusb
Kernel modules:
USB Support:
<M> kmod-usb-hid
Utilities:
<M> lirc:
<M> lircdaemonadd
<M> lirctools
Before building we should add the type of supported driver for LIRC by ourselves. On default LIRC is building with the support of the driver mceusb, but in our case we should use the driver devinput. It is easy to do this. Let’s open the file OpenWtr/feeds/packages/utils/lirc/Makefile. And search for the section CONFIGURE_ARGS and in the line --with-driver="mceusb" we change for --with-driver="devinput".
Let’s install the packages:
root@OpenWrt:/# opkg install kmod-usb-hid Installing kmod-usb-hid (2.6.36.2-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/kmod-usb-hid_2.6.36.2-1_brcm47xx.ipk. Installing kmod-hid (2.6.36.2-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/kmod-hid_2.6.36.2-1_brcm47xx.ipk. Installing kmod-input-core (2.6.36.2-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/kmod-input-core_2.6.36.2-1_brcm47xx.ipk. Installing kmod-input-evdev (2.6.36.2-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/kmod-input-evdev_2.6.36.2-1_brcm47xx.ipk. Configuring kmod-input-core. Configuring kmod-hid. Configuring kmod-input-evdev. Configuring kmod-usb-hid. root@OpenWrt:/# opkg install udev Installing udev (142-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/udev_142-1_brcm47xx.ipk. Configuring udev. root@OpenWrt:/# opkg install lirc Installing lirc (0.8.7-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/lirc_0.8.7-1_brcm47xx.ipk. Configuring lirc. root@OpenWrt:/# opkg install lircdaemonadd lirctools Installing lircdaemonadd (0.8.7-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/lircdaemonadd_0.8.7-1_brcm47xx.ipk. Installing lirctools (0.8.7-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/lirctools_0.8.7-1_brcm47xx.ipk. Configuring lircdaemonadd. Configuring lirctools. |
Some words about working of LIRC. By pressing the button on our remote control a command is being sent per IR-link. This command has the code of the pressed button. The connected to the router usb-port receiver is getting this command and sending it to LIRC with the code of this command. The daemon of LIRC compares the command code with the codes which it knows and runs the operation matching this code. Somehow like this...
Let’s connect the receiver to the usb-port and check what response we will get from Linux.
root@OpenWrt:/# dmesg usb 1-1.4: new low speed USB device using ohci_hcd and address 4 usb 1-1.4: config 1 interface 0 altsetting 0 has 2 endpoint descriptors, different from the interface descriptor's value: 1 input: HID 073a:2230 as /devices/ssb0:1/usb1/1-1/1-1.4/1-1.4:1.0/input/input0 generic-usb 0003:073A:2230.0001: input: USB HID v1.10 Keyboard [HID 073a:2230] on usb-ssb0:1-1.4/input0 root@OpenWrt:/# lsusb Bus 001 Device 001: ID 1d6b:0001 Bus 002 Device 001: ID 1d6b:0002 Bus 001 Device 002: ID 0a05:7211 Bus 001 Device 003: ID 0951:1603 Bus 001 Device 004: ID 073a:2230 |
As we can see the device has been detected and it has the identification code 073a:2230 Chaplet Systems, Inc.
We can also check if the device has been correctly detected in the system by using the following commands.
A device has been created in the directory /dev:
root@OpenWrt:/# ls -l /dev/input/ crw-r--r-- 1 root root 13, 64 Jan 1 00:01 event0 |
A file has been created in the directory /proc:
root@OpenWrt:/# cat /proc/bus/input/devices I: Bus=0003 Vendor=073a Product=2230 Version=0110 N: Name="HID 073a:2230" P: Phys=usb-ssb0:1-1.1/input0 S: Sysfs=/devices/ssb0:1/usb1/1-1/1-1.1/1-1.1:1.0/input/input1 U: Uniq= H: Handlers=event0 B: EV=10001f B: KEY=837fff 2c3027 bf004444 0 fffff ffffffff 10c04 b27c007 ffa67bfa d941dfff febeffdf ffefffff ffffffff fffffffe B: REL=343 B: ABS=1 30000 B: MSC=10 |
We can get the information about the device using the following command udevadm:
root@OpenWrt:/# udevadm info --query=all --name=/dev/input/event0 P: /devices/ssb0:1/usb1/1-1/1-1.1/1-1.1:1.0/input/input1/event0 E: UDEV_LOG=3 E: DEVPATH=/devices/ssb0:1/usb1/1-1/1-1.1/1-1.1:1.0/input/input1/event0 E: MAJOR=13 E: MINOR=64 E: DEVNAME=input/event0 |
Let’s check that the commands sent from the remote control are transferred to the system (let’s start the following command and press buttons on the remote control):
root@OpenWrt:/# cat /dev/input/event0 | hexdump 0000000 04ee 0000 fd21 0003 0004 0004 00ea 000c 0000010 04ee 0000 fd46 0003 0001 0072 0001 0000 0000020 04ee 0000 fd50 0003 0000 0000 0000 0000 0000030 04ee 0000 1641 0005 0004 0004 00ea 000c 0000040 04ee 0000 1666 0005 0001 0072 0000 0000 0000050 04ee 0000 1683 0005 0000 0000 0000 0000 0000060 04f1 0000 b1e3 0000 0004 0004 0028 0007 0000070 04f1 0000 b209 0000 0001 001c 0001 0000 0000080 04f1 0000 b213 0000 0000 0000 0000 0000 0000090 04f1 0000 cb11 0001 0004 0004 0028 0007 00000a0 04f1 0000 cb33 0001 0001 001c 0000 0000 00000b0 04f1 0000 cb4f 0001 0000 0000 0000 0000 |
By using the program irrecord we can give each button of the remote control its name:
root@OpenWrt:/# irrecord -d /dev/input/event0 /etc/lircd.conf --disable-namespace
For test operation there is the program mode2.
Let’s begin with the starting of the daemon lircd. The file /etc/lircd.conf is a configuration file of the daemon lircd. The button name and the code name received per IR link are compared in this file. This file can be created by yourself by reading the value of buttons and by specifying them names, but I think it is better to take the ready-to-use file
It looks like this:
begin remote name ChapletSystem bits 32 begin codes POWER 0x40004 0x80010074 RADIO 0x40004 0x8001001e TV 0x40004 0x80010014 DVD 0x40004 0x80010031 MUSIC 0x40004 0x80010032 PHOTO 0x40004 0x80010017 VIDEO 0x40004 0x80010012 DVD_MENU 0x40004 0x8001002f MUTE 0x40004 0x80010042 BACK 0x40004 0x8001000e GUIDE 0x40004 0x8001003b VOLUME_UP 0x40004 0x80010073 VOLUME_DOWN 0x40004 0x80010072 CHANNEL_UP 0x40004 0x80010068 CHANNEL_DOWN 0x40004 0x8001006d ARROW_UP 0x40004 0x80010067 ARROW_DOWN 0x40004 0x8001006c ARROW_LEFT 0x40004 0x80010069 ARROW_RIGHT 0x40004 0x8001006a ENTER 0x40004 0x8001001c RECORD 0x40004 0x8001001d 0x40004 0x80010013 REPEAT 0x40004 0x8001002a 0x40004 0x80010013 PLAY 0x40004 0x8001002a 0x40004 0x80010019 PAUSE 0x40004 0x8001001d 0x40004 0x80010019 STOP 0x40004 0x8001001f REWIND 0x40004 0x8001002a 0x40004 0x80010020 FORWARD 0x40004 0x8001002a 0x40004 0x80010021 PREV_TRACK 0x40004 0x8001001d 0x40004 0x80010030 NEXT_TRACK 0x40004 0x8001001d 0x40004 0x80010021 NUMPAD_1 0x40004 0x80010002 NUMPAD_2 0x40004 0x80010003 NUMPAD_3 0x40004 0x80010004 NUMPAD_4 0x40004 0x80010005 NUMPAD_5 0x40004 0x80010006 NUMPAD_6 0x40004 0x80010007 NUMPAD_7 0x40004 0x80010008 NUMPAD_8 0x40004 0x80010009 NUMPAD_9 0x40004 0x8001000a NUMPAD_0 0x40004 0x8001000b end codes end remote |
Let’s start the daemon lircd:
root@OpenWrt:/# mkdir /var/run/lirc root@OpenWrt:/# lircd -n --driver=dev/input --device=/dev/input/event0 --pidfile /var/run/lirc/lircd.pid |
Let’s check how it works. In another window we start the client irw. The client irw reads the data of lircd and outputs the result on the display. After starting we press the buttons on the remote control. We can see on the display the name of the buttons and the code:
root@OpenWrt:/# irw 0000000000040004 00 ENTER ChapletSystem 0000000000040004 00 VOLUME_UP ChapletSystem 0000000000040004 00 VOLUME_DOWN ChapletSystem 0000000000040004 00 ARROW_RIGHT ChapletSystem 0000000000040004 00 ARROW_UP ChapletSystem 0000000000040004 00 ARROW_DOWN ChapletSystem 0000000000040004 00 ARROW_LEFT ChapletSystem |
For running of the button a specific operation there is the file lircrc.
Its format is very simple. Let’s consider an example:
begin prog = irexec button = ENTER config = echo "It is button 'ENTER'" end begin prog = irexec button = VOLUME_UP config = /home/my_script.sh end |
By pressing the button ENTER on the remote control we set the command echo "It is button 'ENTER'" or by pressing the button VOLUME_UP on the remote control we set the script /home/my_script.sh. Irexec it is a daemon which starts the programs by pressing a button on the remote control.
Let’s check how it works. Let’s start the daemon lircd in the background mode and by starting of the daemon irexec we specify him the file lircrc.
root@OpenWrt:/# lircd --driver=dev/input --device=/dev/input/event0 —pidfile root@OpenWrt:/# irexec /home/lircrc sh: /home/my_script.sh: not found It is button 'ENTER' It is button 'ENTER' sh: /home/my_script.sh: not found /var/run/lirc/lircd.pid |
Buy a set of IR remote control