IR Remote Control

Infrared remote control

This article illustrates how to use the 38 kHz infrared receiver available on the CM3-Home

The example supplied with the SD card uses the first 6 keys of a simple remote control

As a demo it switches on the RGB LEDs with the first row keys and switches them off with the second row.

Changing the items driven by the rules many other functions can be performed:

rules/ir.rules

	
rule "IR_0-ON"
when 
	Item Ir_0 received command ON
then
    sendCommand(LedR, OFF)
end

rule "IR_0-OFF"
when 
	Item Ir_0 received command OFF
then
    sendCommand(LedR, ON)
end
	
rule "IR_1-ON"
when 
	Item Ir_1 received command ON
then
    sendCommand(LedG, OFF)
end
	
rule "IR_1-OFF"
when 
	Item Ir_1 received command OFF
then
    sendCommand(LedG, ON)
end
	
rule "IR_2-ON"
when 
	Item Ir_2 received command ON
then
    sendCommand(LedB, OFF)
end
	
rule "IR_2-OFF"
when 
	Item Ir_2 received command OFF
then
    sendCommand(LedB, ON)
end

For example activating the internal relays, KNX or RS485 switches, playing sounds or anyone of the many items defined, or even system actions as restarting the board itself.

How to set the CM3-Home internal IR receiver

This configuration is valid for Raspbian Stretch with LIRC 0.9.4

Install LIRC:

sudo apt-get install lirc

Modify:

/etc/lirc/lirc_options.conf

updating lines from

driver = devinput
device = auto

to

driver = default
device = /dev/lirc0

Reboot

To test the installation:

sudo systemctl stop lircd.service
sudo systemctl stop lircd.socket

then
mode2 -d /dev/lirc0

Pushing a remote control key, something like this should be displayed if the installation is ok and the remote control modulation frequency matches the receiver one, i.e. 38kHz:

Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Using device: /dev/lirc0
space 10743268
pulse 243
space 19204
pulse 100
space 2521370
pulse 9126
space 4441
pulse 625
space 506
pulse 623
space 513
pulse 622
space 510

At this point it’s possible to use one of the many ready to use remote controls definitions supplied by LIRC or learn your own.

To learn the remote control key codes needed:

irrecord -d /dev/lirc0 ~/lircd.conf

Follow carefully the instruction. At the end it will be available a file given_name.lirc.conf.

For example:

cat remote.lircd.conf 
	
	# Please take the time to finish this file as described in
	# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/
	# and make it available to others by sending it to
	# <lirc@bartelmus.de>
	#
	# This config file was automatically generated
	# using lirc-0.9.4c(default) on Sat Mar 24 23:05:34 2018
	# Command line used: -d /dev/lirc0 /home/openhabian/lircd.conf
	# Kernel version (uname -r): 4.9.80-v7+
	#
	# Remote name (as of config file): remote
	# Brand of remote device, the thing you hold in your hand:
	# Remote device model nr:
	# Remote device info url:
	# Does remote device has a bundled capture device e. g., a
	#     usb dongle? :
	# For bundled USB devices: usb vendor id, product id
	#     and device string (use dmesg or lsusb):
	# Type of device controlled
	#     (TV, VCR, Audio, DVD, Satellite, Cable, HTPC, ...) :
	# Device(s) controlled by this remote:
	
	begin remote
	
	  name  remote
	  bits           32
	  flags SPACE_ENC|CONST_LENGTH
	  eps            30
	  aeps          100
	
	  header       9098  4444
	  one           628  1632
	  zero          628   504
	  ptrail        629
	  repeat       9099  2187
	  gap          107733
	  toggle_bit_mask 0x0
	  frequency    38000
	
	      begin codes
	          KEY_0                    0x00FFA25D 0x7EF26F2C
	          KEY_1                    0x00FF629D 0x7EF26F2C
	          KEY_2                    0x00FFE21D 0x7EF26F2C
	          KEY_3                    0x00FF22DD 0x7EF26F2C
	          KEY_4                    0x00FF02FD 0x7EF26F2C
	          KEY_5                    0x00FFC23D 0x7EF26F2C
	      end codes
	
	end remote

Unfortunately this is wrong for a known bug of  irrecord in version 0.9.4 as documented in this blog. It looks like it doubles the code. Often deleting the second code in each row resolves the problem.

That’s correct:

	begin remote
	
	  name  IR
	  bits           32
	  flags SPACE_ENC|CONST_LENGTH
	  eps            30
	  aeps          100
	
	  header       9098  4447
	  one           624  1636
	  zero          624   508
	  ptrail        625
	  repeat       9102  2189
	  gap          107732
	  toggle_bit_mask 0x0
	  frequency    38000
	
	      begin codes
	          KEY_1                    0x00FFA25D
	          KEY_2                    0x00FF629D
	          KEY_3                    0x00FFE21D
	          KEY_4                    0x00FF22DD
	          KEY_5                    0x00FF02FD
	          KEY_6                    0x00FFC23D
	          KEY_7                    0x00FFE01F
	          KEY_8                    0x00FFA857
	          KEY_9                    0x00FF906F
	          KEY_0                    0x00FF6897
	      end codes
	
	end remote

Copy the file to the right path:

sudo cp remote.lircd.conf /etc/lirc/lircd.conf.d/

Restart LIRC:

sudo systemctl restart lircd

Check if the remote definition has been acknowledged by LIRC

irsend LIST "" ""

The name of the configuration (given_name) should be displayed.

With the command

irw

it’s possible to test if everything’s ok pushing all the defined keys

openhabian@cm3-home:~$ irw
0000000000ffa25d 00 KEY_1 IR
0000000000ff629d 00 KEY_2 IR
0000000000ffe21d 00 KEY_3 IR
0000000000ff22dd 00 KEY_4 IR
0000000000ff02fd 00 KEY_5 IR
0000000000ffc23d 00 KEY_6 IR
0000000000ffe01f 00 KEY_7 IR
0000000000ffa857 00 KEY_8 IR
0000000000ff906f 00 KEY_9 IR
0000000000ff6897 00 KEY_0 IR

LIRC is ready and knows the remote codes.

With lircrc it is possbile to trigger some actions everytime a key is pressed.

Edit

/etc/lirc/lircrc

In this way:

	begin
	    button = KEY_1
	    prog = irexec
	    config = /usr/bin/mosquitto_pub -h localhost -t openhab/out/ir/command -m key_1
	    repeat = 0
	end
	begin
	    button = KEY_2
	    prog = irexec
	    config = /usr/bin/mosquitto_pub -h localhost -t openhab/out/ir/command -m key_2
	    repeat = 0
	end
	begin
	    button = KEY_3
	    prog = irexec
	    config = /usr/bin/mosquitto_pub -h localhost -t openhab/out/ir/command -m key_3
	    repeat = 0
	end
	begin
	    button = KEY_4
	    prog = irexec
	    config = /usr/bin/mosquitto_pub -h localhost -t openhab/out/ir/command -m key_4
	    repeat = 0
	end
	begin
	    button = KEY_5
	    prog = irexec
	    config = /usr/bin/mosquitto_pub -h localhost -t openhab/out/ir/command -m key_5
	    repeat = 0
	end
	begin
	    button = KEY_6
	    prog = irexec
	    config = /usr/bin/mosquitto_pub -h localhost -t openhab/out/ir/command -m key_6
	    repeat = 0
	end

To publish an MQTT topic each time a defined code is received by LIRC.

Launching irexec it’s possible to check the published topics with an MQTT spy as, for example, MQTTlens

To start irexec as a demon at startup, add this row to /etc/rc.local

/usr/bin/irexec -d &

At this point the topic key_# will be published in the openhab/out/ir/command queue of the localhost broker when the KEY # is pressed.

Add the items subscribing those topics:

items/ir.items

Switch Ir_0 "IR0"  {mqtt= "<[broker1:openhab/out/ir/command:command:ON:key_1], <[broker1:openhab/out/ir/command:command:OFF:key_4]", autoupdate="false"}
Switch Ir_1 "IR1"  {mqtt= "<[broker1:openhab/out/ir/command:command:ON:key_2], <[broker1:openhab/out/ir/command:command:OFF:key_5]", autoupdate="false"}
Switch Ir_2 "IR2"  {mqtt= "<[broker1:openhab/out/ir/command:command:ON:key_3], <[broker1:openhab/out/ir/command:command:OFF:key_6]", autoupdate="false"}

Use those items to perform some action using some rules. E.g.: switch on and off the RGB LED:

rules/ir.rules
 

	rule "IR_0-ON"
		when 
			Item Ir_0 received command ON
		then
	        sendCommand(LedR, OFF)
	end
	
	rule "IR_0-OFF"
		when 
			Item Ir_0 received command OFF
		then
	        sendCommand(LedR, ON)
	end
	
	rule "IR_1-ON"
		when 
			Item Ir_1 received command ON
		then
	        sendCommand(LedG, OFF)
	end
	
	rule "IR_1-OFF"
		when 
			Item Ir_1 received command OFF
		then
	        sendCommand(LedG, ON)
	end
	
	rule "IR_2-ON"
		when 
			Item Ir_2 received command ON
		then
	        sendCommand(LedB, OFF)
	end
	
	rule "IR_2-OFF"
		when 
			Item Ir_2 received command OFF
		then
	        sendCommand(LedB, ON)
	end
Links

Share