【问题标题】:How to use IR Remote with Raspberry Pi using Python?如何使用 Python 将 IR Remote 与 Raspberry Pi 一起使用?
【发布时间】:2017-09-27 04:53:08
【问题描述】:

我购买了这个 [IR 传感器和遥控器][1] 用于我的 Raspberry Pi 3。

我已经设置了 LIRC,并且可以使用以下命令检测来自 IR 遥控器的输入:

sudo /etc/init.d/lirc 停止

mode2 -d /dev/lirc0

当我运行上述命令时,我能够检测到来自 IR 遥控器的输入。当我按下 IR REmote 上的任何按钮时,我会得到如下输出:

我的问题是 - 在上面的输出中,我在遥控器上按下了“2”。我应该如何(在 python 中)解密真正按下了哪个按钮?

更新 1:

我尝试使用 python-lirc 包,但在这一行出现错误:

【问题讨论】:

    标签: python raspberry-pi3 infrared lirc


    【解决方案1】:

    这些天没有必要使用 lirc 作为 IR 事件are handled through the kernel

    在我的例子中,我在/boot/config.txt 的 GPIO 引脚上设置了一个 IR 接收器

    dtoverlay=gpio-ir,gpio_pin=16

    然后安装evdev

    pip install evdev

    重新启动,以下工作正常(您的输入设备路径不同)

    from evdev import InputDevice, categorize, ecodes
    dev = InputDevice('/dev/input/event0')
    
    for event in dev.read_loop():
        print(event)
    

    当按下遥控器上的一些按钮时,我得到:

    $> sudo python3 irtest.py
    device /dev/input/event0, name "gpio_ir_recv", phys "gpio_ir_recv/input0"
    event at 1639133806.295636, code 04, type 04, val 64
    event at 1639133806.295636, code 00, type 00, val 00
    event at 1639133806.405607, code 04, type 04, val 64
    event at 1639133806.405607, code 00, type 00, val 00
    event at 1639133808.745610, code 04, type 04, val 16
    ...
    

    【讨论】:

      【解决方案2】:

      前面的答案简化了 lirc 解码。由于您有一个工作模式2,内核驱动程序可以工作并将正确的数据发送到 lircd。但是,mode2 不会告诉您解码是否有效。

      要检查解码,请使用 irw(1)。在您获得该程序的工作输出之前,您不知道 lirc 是否可以解码您的遥控器。

      上述的 lircrc 文件用于将通用按钮按下(如 irw 所示)转换为特定于应用程序的命令。要调试此文件,请使用 ircat(1)。

      当您从 irw(1) 和 ircat(1) 获得工作输出时,您的 lirc 设置就完成了。在使用任何 python 包之前,确实需要一个有效的 lirc 设置。顺便说一句,从即将发布的 0.10.0 开始,lirc 将具有本机 python 绑定。

      可以在http://lirc.org/html/configuration-guide.html找到设置 lirc 的综合指南

      【讨论】:

        【解决方案3】:

        您可能不想为此使用mode2 输出。有一个可用的 Python 库 (Here),这可能是该项目的更好方法。

        代码:

        import lirc
        sockid = lirc.init("myprogram")
        print(lirc.nextcode())
        lirc.deinit()
        

        lircrc 配置文件

        begin
          button = 1          # what button is pressed on the remote
          prog = myprogram    # program to handle this command
          config = one, horse # configs are given to program as list
        end
        
        begin
          button = 2
          prog = myprogram
          config = two
        end
        

        按下按钮 1 后的结果

        ['one', 'horse']
        

        【讨论】:

        • 我试过了,但我在 'sockid = lirc.init("myprogram")' 行中发现了一个问题。我已经用我看到的输出更新了这个问题。
        • @NeilDey,看起来您没有正确设置 lirc 或其 conf 文件。建议您尝试调试提示 (here)。请注意用于调试的echo 命令...您是否在sudo /etc/init.d/lirc stop 之后重新启动了lirc?
        猜你喜欢
        • 2021-12-01
        • 1970-01-01
        • 2016-07-18
        • 2019-09-09
        • 2018-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-11
        相关资源
        最近更新 更多