User avatar
Seiduna
Posts: 106
Joined: Wed Nov 20, 2013 4:12 pm

LIRC, GPIO and python

Wed Feb 24, 2016 2:15 am

hello rpi people,

I am new to GPIO thing, but I managed to do several things with quite success!

Now I have ir receiver, and small ir remote.
I managed to configure rpi, and irw gives my desired buttons.

Now I want to write a python script, if I push ir remote control button 1, LED should light up on breadboard.
How to get the remote output in python?
I searched the net, but i guess my English is not so good.

Could someone provide me with some piece of python code on this matter?

I also found that I can configure .lircrc, but that is 'kind of fixed', since, as far as I understand, is for some applications to run (i guess omxplayer, ...)

Thank you
Gnu/Linux,
because I'd rather own a free OS
than steal one that's not worth paying for.

fsr
Posts: 88
Joined: Wed Jan 13, 2016 2:29 am

Re: LIRC, GPIO and python

Wed Feb 24, 2016 6:47 am

Seiduna wrote:Now I want to write a python script, if I push ir remote control button 1, LED should light up on breadboard
I can't help you with python but lirc I know well.

Think of lirc as a 'remote control to keyboard translator'.
what your python script needs to do is listen for key presses just like it was reading a keyboard.
eg 'b' turns light on, 'n' turns light off

once you have that working you then map remote control keys to keyboard keys using lirc.
eg a pretend .lirc config that will send a 'b' when play is pressed on the remote control and send a 'n' when stop is pressed.

Code: Select all

begin
    remote = mceusb
    button = KEY_PLAY
    config = b
    repeat = 0
    delay = 0
end

begin
    remote = mceusb
    button = KEY_STOP
    config = n
    repeat = 0
    delay = 0
end
Now instead of pressing b & n on the keyboard, we can press play and stop on the remote.
you can optionally direct key presses at specific programs (say you have 3 different programs to manage from one remote at the same time)

so first - write your python script so that it responds to keypresses.
second - configure lirc to send the right keypresses

User avatar
Seiduna
Posts: 106
Joined: Wed Nov 20, 2013 4:12 pm

Re: LIRC, GPIO and python

Wed Feb 24, 2016 3:06 pm

Hey fsr,

I know nothing about lirc nor python, but i can copy and paste like a pro!

So I wrote python script that takes keyboard input, and does it nicely from keyboard.
For my .lircrc (I tryed also /etc/lirc/lircrc) I took your example, as many others, but it did not work, no input from IR remote control.
(I do have remote keys described as KEY_FUNCTION)
Further, the method you described is a work around, kind of, since one has to write python script to accept user input not as state (0 or 1) but as letter(s), which has to be than assigned to certain state, which in turn lights up LED.

What I have in mind is exactly this: http://ozzmaker.com/how-to-control-the- ... ir-remote/
and I managed to do it, but it is in C. Now I am already new to python, but i do not feel like taking up a new language, which would mean I would need to rewrite all scripts I wrote up until now.
In link above there is also a link in comments that uses python script that does the same as C, but it does not work:
https://github.com/developius/pi_scripts/tree/master/IR

I am thankful for your time and input, if you have any idea please share, I am eager to try.

Thank you
Gnu/Linux,
because I'd rather own a free OS
than steal one that's not worth paying for.

blaablaaguy
Posts: 618
Joined: Sun Sep 27, 2015 3:26 pm

Re: LIRC, GPIO and python

Thu Feb 25, 2016 11:41 am

This thing detects keypresses in python.

Code: Select all

import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(26, GPIO.OUT)
GPIO.output(26,GPIO.LOW)

pygame.init()
BLACK = (0,0,0)
WIDTH = 100
HEIGHT = 100
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

windowSurface.fill(BLACK)
x = 0
while True:
   for event in pygame.event.get():
      if event.type == QUIT:
            pygame.quit()
            sys.exit()
      if event.type == KEYDOWN:
         key = event.key
         if key == pygame.K_"CHANGE HERE":
            if x == 0:
               GPIO.output(26,GPIO.HIGH)
               x = 1
            else:
               GPIO.output(26,GPIO.LOW)
               x = 0
Remove "CHANGE HERE" (including the speech marks) with the key you want to use. GPIO 26 will be set high one your key is pressed either on the remote or keyboard. You will need to install pygame to use it.
This signature intentionally left blank.

User avatar
Seiduna
Posts: 106
Joined: Wed Nov 20, 2013 4:12 pm

Re: LIRC, GPIO and python

Thu Feb 25, 2016 2:05 pm

hello blaablaaguy,

Do I need X for this? I forgot to mention, I do all in CLI style, via ssh, so no display available.
And GPIO 26? that is the very last one? Not one of the regular outputs?
Also, do I need lircrc file for this, or not?
For "change here" do you mean like K_n (for n key) or K_N (also for n key)? Than I suppose i need lircrc?

Thank you
Gnu/Linux,
because I'd rather own a free OS
than steal one that's not worth paying for.

blaablaaguy
Posts: 618
Joined: Sun Sep 27, 2015 3:26 pm

Re: LIRC, GPIO and python

Thu Feb 25, 2016 9:08 pm

I think you need x to use it. Dunno much about LIRC but if you can set a button on your remote to a certain keyboard key and the code to the same key it should work. You can change the gpio pin by just replacing every number 26 with the pin you want.
This signature intentionally left blank.

input
Posts: 130
Joined: Sun Nov 02, 2014 9:18 am

Re: LIRC, GPIO and python

Tue Jan 03, 2017 2:52 pm

blaablaaguy wrote:This thing detects keypresses in python.

Code: Select all

import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(26, GPIO.OUT)
GPIO.output(26,GPIO.LOW)

pygame.init()
BLACK = (0,0,0)
WIDTH = 100
HEIGHT = 100
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

windowSurface.fill(BLACK)
x = 0
while True:
   for event in pygame.event.get():
      if event.type == QUIT:
            pygame.quit()
            sys.exit()
      if event.type == KEYDOWN:
         key = event.key
         if key == pygame.K_"CHANGE HERE":
            if x == 0:
               GPIO.output(26,GPIO.HIGH)
               x = 1
            else:
               GPIO.output(26,GPIO.LOW)
               x = 0
Remove "CHANGE HERE" (including the speech marks) with the key you want to use. GPIO 26 will be set high one your key is pressed either on the remote or keyboard. You will need to install pygame to use it.
I have also a question for working with a led.
I use your script and I change your script: if key == pygame.K_"CHANGE HERE" in : if key == pygame.KEY_1:

But when I pressed on the button "1" from my remote control or my keyboard I get this message:

Traceback (most recent call last):
File "/home/pi/ir.py", line 24, in <module>
if key == pygame.KEY_1:
AttributeError: 'module' object has no attribute 'KEY_1

Do you ahve any idee what is wrong ?

Thanks

nahuavrilero
Posts: 8
Joined: Fri Jun 22, 2018 11:22 am

Re: LIRC, GPIO and python

Fri Jun 22, 2018 11:50 am

input wrote:
blaablaaguy wrote:This thing detects keypresses in python.

Code: Select all

import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(26, GPIO.OUT)
GPIO.output(26,GPIO.LOW)

pygame.init()
BLACK = (0,0,0)
WIDTH = 100
HEIGHT = 100
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

windowSurface.fill(BLACK)
x = 0
while True:
   for event in pygame.event.get():
      if event.type == QUIT:
            pygame.quit()
            sys.exit()
      if event.type == KEYDOWN:
         key = event.key
         if key == pygame.K_"CHANGE HERE":
            if x == 0:
               GPIO.output(26,GPIO.HIGH)
               x = 1
            else:
               GPIO.output(26,GPIO.LOW)
               x = 0
Remove "CHANGE HERE" (including the speech marks) with the key you want to use. GPIO 26 will be set high one your key is pressed either on the remote or keyboard. You will need to install pygame to use it.
I have also a question for working with a led.
I use your script and I change your script: if key == pygame.K_"CHANGE HERE" in : if key == pygame.KEY_1:

But when I pressed on the button "1" from my remote control or my keyboard I get this message:

Traceback (most recent call last):
File "/home/pi/ir.py", line 24, in <module>
if key == pygame.KEY_1:
AttributeError: 'module' object has no attribute 'KEY_1

Do you ahve any idee what is wrong ?
I don't know ir pygame can talk with the remote.
Maybe Is because of that.
Have you tried another button?

Also, pylirc can recognize remote button presses.
You have to write a lircrc file with the line: prog=anynameyouwant and call it from python with pylirc.init("anynameyouwant").

I managed to light a led via remote button, but the problem i encountered is i have to press a long time the button or press it a few times in order to work.
But probably is my por coding.
I can post the code if you want.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 14 guests