LIRC with Python

The following is to use Python on the Raspberry Pi to receive/transmit infrared signals. ie: To have the Raspberry Pi act as an IR remote control unit.

Requirements

Components Required:

Wiring

Raspberry Pi configuration

Ensure your Raspberry Pi is up to date

sudo apt-get update
sudo apt-get upgrade

Install LIRC

sudo apt-get install lirc

Edit /etc/modules file

sudo nano /etc/modules

Add to the bottom of file:

lirc_dev
lirc_rpi gpio_in_pin=18 gpio_out_pin=22

Edit /etc/lirc/hardware.conf:

sudo nano /etc/lirc/hardware.conf

Change it to:

LIRCD_ARGS="--uinput"
LOAD_MODULES=true
DRIVER="default"
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"
LIRCD_CONF=""
LIRCMD_CONF=""

Edit the /boot/config.txt file:

sudo nano /boot/config.txt

Add this line:

dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=22

Create /etc/modprobe.d/ir-remote.conf file:

sudo nano /etc/modprobe.d/ir-remote.conf

Add this line:

options lirc_rpi gpio_in_pin=18 gpio_out_pin=22

Go ahead and reboot the Pi:

sudo reboot

Test IR receiver

Time to test IR Reciever:

sudo modprobe lirc_rpi

then

sudo kill $(pidof lircd)

then

mode2 -d /dev/lirc0

At this point you should be able to press buttons on your remote and see output similar to this:

(hit “ctrl + c” to exit)

sudo kill $(pidof lircd)

save output to text file for future reference:

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

Recording IR signals

run

sudo kill $(pidof lircd)

then

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

Carefully follow the prompts given by the program

verify /home/pi/lircd.conf created successfully:

cat /home/pi/lircd.conf

(if so continue, if not start over at MAPPING REMOTE CODES section)

copy/overwrite /home/pi/lircd.conf to /etc/lirc/

sudo cp /home/pi/lircd.conf /etc/lirc/lircd.conf

Restart LIRC:

sudo /etc/init.d/lircd restart

Sending IR signals

Before issuing commands run this:

sudo lircd --device /dev/lirc0

Test sample command:

irsend SEND_ONCE /home/pi/lircd.conf KEY_POWER

Python coding

#!/usr/bin/env python
import os
import time

#turn on TV and change input source
os.system('irsend SEND_ONCE /home/pi/lircd.conf KEY_POWER')
time.sleep(12)
os.system('irsend SEND_ONCE /home/pi/lircd.conf KEY_AUX')
time.sleep(4)
os.system('irsend SEND_ONCE /home/pi/lircd.conf KEY_AUX')
time.sleep(4)
os.system('irsend SEND_ONCE /home/pi/lircd.conf KEY_AUX')
time.sleep(4)
os.system('irsend SEND_ONCE /home/pi/lircd.conf KEY_OK')

Sourced from