我正在尝试使用运行最新 Rasbian Wheezy 发行版的 Raspberry Pi B + 在我的 Raspberry Pi B + 上使用 OWN(开放天气网络)设置滚动天气供稿,但在使用 Python LIRC添加IR支持时遇到了问题( Linux红外线 Remote )

我正在尝试做的事情:
天气变量有四个:条件,温度,湿度和风速。它们将出现在我的16x2 LCD屏幕上,标题居中,第一行居中。它们将在屏幕上停留五秒钟,然后被下一个替换。一旦到达终点,它将再次循环。循环180次(约一小时)后,它将更新天气。我想使用我的红外线 Remote 的按钮1-4跳到特定的图块,然后继续循环播放。

在做什么:
当没有按钮被按下时,与其挂起 LIRC 来跳过空队列,它卡在 lirc.nextcode()上,等待按钮按下,直到我退出 KeyboardInterrupt 为止。

一切正常,直到我添加IR。现在,它显示第一个天气变量,然后在尝试拉出下一个天气变量时,而不是在队列中没有IR代码的情况下跳过并跳至下一个图块, lirc.nextcode()暂停代码,直到接收到IR代码,而 LIRC 阻止功能已关闭则不应发生。

我拥有所有软件的最新版本( Python LIRC 1.2.1 ),我知道的先前版本Python LIRC 的阻塞参数存在错误。我花了两天时间研究并尝试所有可能的事情。这是我发现的一种可能的解决方法,但是它受到与此相同的问题的影响:“Python LIRC blocking Signal workaround not working

我知道很多代码是不正确的,例如,全局变量,东西需要包含在函数中,OWN每三个小时更新一次,而我每小时更新一次,但这是暂时的,以使其正常工作。我将整理它,稍后使其面向对象。抱歉,如果这样会使某些人更难以阅读。

import pyowm
from sys import exit
import time
import RPi.GPIO as GPIO
from RPLCD import CharLCD, cleared, cursor
import lirc

# initialize lirc and turn of blocking
sockid = lirc.init("weather", blocking=False)
lirc.set_blocking(False, sockid)

# initialize weather network
owm = pyowm.OWM('API #')

# initialize LCD
lcd = CharLCD(pin_rs=26, pin_rw=None, pin_e=24, pins_data=[22, 18, 16, 12],
                  cols=16, rows=2)

# weather data
w = None # wind m/s
wind = None # wind km/h
windkm = None
humidity = None
temper = None
COUNTER = 0 #number of cycles before update

NEXT = 1

# switches to next tile
def next_tile():
    global NEXT

这就是问题所在。 Lirc.nextcode()应该拉下一个IR
来自 LIRC 队列的代码,并将其作为列表添加到 codeIR ,但如果没有
按下按钮,并且阻止功能已关闭,应该跳过
代码。相反,它的作用就像阻止已打开,并挂起直到
按下一个按钮。然后它仍然不会继续我的主循环。它
只是打印下一个并挂起,直到我 KeyboardInterrupt 退出为止。
    codeIR = lirc.nextcode() # pulls IR code from LIRC queue.
    # checks if there's a code in codeIR and goes to that tile. If not, it
    # goes to the next tile instead.
    if not codeIR:
        if NEXT != 4: # if it's the last tile, cycle to the first
            NEXT += 1
            print NEXT
            return NEXT
        else: # if not last tile, go to next
            NEXT -= 3
            print NEXT
            return NEXT
    else:
        NEXT = codeIR[0]
        print NEXT
        return NEXT

我已经添加了其余的代码,它们都可以正常工作,但是我敢肯定,它将帮助您了解我要完成的工作。
while True:
    try:
        if COUNTER == 0:
            COUNTER = 180

            # Search for current weather in London (UK)
            observation = owm.weather_at_place('City, State')
            w = observation.get_weather()

            # Weather details
            wind = w.get_wind()                  # {'speed': 4.6, 'deg': 330}
            windkm = (wind['speed'] * 3600) / 1000 #convet to km/h
            humidity = w.get_humidity()
            # {'temp_max': 10.5, 'temp': 9.7, 'temp_min': 9.0}
            temper = w.get_temperature('celsius')
        else:
            while NEXT == 1:
                # prints condition to lcd
                lcd.cursor_pos = (0, 4) #adjust cursor position
                lcd.write_string('Weather') # write to lcd
                lcd.cursor_pos = (1, 5) # adjust cursor position
                lcd.write_string(w.get_status()) # write to lcd
                time.sleep(5) # leave on lcd for 5 seconds
                lcd.clear() # clear lcd
                next_tile() # switches to next tile

            while NEXT == 2:
                # prints temp to lcd
                lcd.cursor_pos = (0, 2)
                lcd.write_string('Temperature')
                lcd.cursor_pos = (1, 6)
                lcd.write_string(str(temper['temp']))
                lcd.write_string(' C')
                time.sleep(5)
                lcd.clear()
                next_tile()

            while NEXT == 3:
                # prints temp to lcd
                lcd.cursor_pos = (0, 4)
                lcd.write_string('Humidity')
                lcd.cursor_pos = (1, 6)
                lcd.write_string(str(humidity))
                lcd.write_string(' %')
                time.sleep(5)
                lcd.clear()
                next_tile()

            while NEXT == 4:
                # prints wind speed to lcd
                lcd.cursor_pos = (0, 3)
                lcd.write_string('Wind Speed')
                lcd.cursor_pos = (1, 6)
                lcd.write_string(str(int(windkm)))
                lcd.write_string('km')
                time.sleep(5)
                lcd.clear()
                COUNTER -= 1
                codeIR = lirc.nextcode()
                next_tile()

    # quit with ctrl+C
    except(KeyboardInterrupt, SystemExit):
        print 'quitting'
        lcd.close(clear=True)
        lirc.deinit()
        exit()

当我 KeyboardInterrupt 外出时, Traceback 总是导致 lirc.nextcode(),我将发布该错误,但是我更改了代码,现在它仅追溯到包含 lirc.nextcode的函数()

我花了两天时间来解决这个问题,而且我几乎要拔头发了,所以我会采取任何解决方案或变通办法,你们可以给我。在此先感谢您,我非常感谢能找到的任何帮助。我找到了使用信号模块AlarmException 的解决方法,但是从我从raw_input()切换到lirc.nextcode()的那一刻,它也以相同的方式挂起(即使将计时器放在raw_input()上也没有问题)并阻止了警报从正确的工作。再次是链接:“Python LIRC blocking Signal workaround not working

最佳答案

事实证明,该错误仍在 1.2.1 中。我切换到 Pylirc2 ,它关闭了 pylirc.blocking(0)的无问题阻止功能。我还必须从 next_tile()函数中删除return

如果有人感兴趣,这是我最终使用的完成代码,它肯定会节省很多时间:

import pyowm
from sys import exit
import time
import RPi.GPIO as GPIO, feedparser, time
from RPLCD import CharLCD, cleared, cursor
import pylirc

sockid = pylirc.init('weather')
allow = pylirc.blocking(0)
owm = pyowm.OWM('API Key')
lcd = CharLCD(pin_rs=26, pin_rw=None, pin_e=24, pins_data=[22, 18, 16, 12],
                  cols=16, rows=2)


class mail(object):
    def __init__(self):
        self.username = "email address"
        self.password = "password"
        self.newmail_offset = 0
        self.current = 0
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(15, GPIO.OUT)
        GPIO.setup(13, GPIO.OUT)
        GPIO.setup(11, GPIO.OUT)

    def buzz(self):
        self.period = 1.0 / 250
        self.delay = self.period / 2
        self.cycles = 250

        for i in range(self.cycles):
            GPIO.output(11, True)
            time.sleep(self.delay)

            GPIO.output(11, False)
            time.sleep(self.delay)

    def check(self):
        self.newmails = int(feedparser.parse("https://" + self.username + ":" +
                            self.password +"@mail.google.com/gmail/feed/atom")
                            ["feed"]["fullcount"])

        if self.newmails > self.newmail_offset:
            GPIO.output(15, True)
            GPIO.output(13, False)

            if self.newmails > self.current:
                self.buzz()
                self.current += 1
        else:
            GPIO.output(15, False)
            GPIO.output(13, True)
            self.current = 0

### will be a class
class weather(object):
    def __init__(self):
        self.w = None
        self.wind = None
        self.windkm = None
        self.humidity = None
        self.temper = None
        self.counter = 0
        self.next = 1

    def update(self):
        if self.counter == 0:
            self.counter = 180
            self.observation = owm.weather_at_place('City, Country')
            self.w = self.observation.get_weather()
            self.wind = self.w.get_wind()
            self.windkm = (self.wind['speed'] * 3600) / 1000
            self.humidity = self.w.get_humidity()
            self.temper = self.w.get_temperature('celsius')
        else:
            pass

    def display_weather(self):
        lcd.cursor_pos = (0, 4)
        lcd.write_string('Weather')
        lcd.cursor_pos = (1, 5)
        lcd.write_string(self.w.get_status())
        time.sleep(3)
        lcd.clear()

    def display_temp(self):
        lcd.cursor_pos = (0, 2)
        lcd.write_string('Temperature')
        lcd.cursor_pos = (1, 6)
        lcd.write_string(str(self.temper['temp']))
        lcd.write_string(' C')
        time.sleep(3)
        lcd.clear()

    def display_hum(self):
        lcd.cursor_pos = (0, 4)
        lcd.write_string('Humidity')
        lcd.cursor_pos = (1, 6)
        lcd.write_string(str(self.humidity))
        lcd.write_string(' %')
        time.sleep(3)
        lcd.clear()

    def display_wind(self):
        lcd.cursor_pos = (0, 3)
        lcd.write_string('Wind Speed')
        lcd.cursor_pos = (1, 4)
        lcd.write_string(str(int(self.windkm)))
        lcd.write_string('km/h')
        time.sleep(3)
        lcd.clear()

    def next_tile(self):
        self.counter -= 1
        self.codeIR = pylirc.nextcode()
        if  not self.codeIR or self.codeIR[0] == self.next:
            if self.next != 4:
                self.next += 1
            else:
                self.next -= 3
        else:
            self.next = int(self.codeIR[0])

email = mail()
weather = weather()
weather.update()
def up_next():
    weather.update()
    weather.next_tile()

while True:
    try:
        while weather.next == 1:
            weather.display_weather()
            up_next()

        while weather.next == 2:
            weather.display_temp()
            up_next()

        while weather.next == 3:
            weather.display_hum()
            up_next()

        while weather.next == 4:
            weather.display_wind()
            email.check()
            up_next()

    except(KeyboardInterrupt, SystemExit):
        print 'quitting'
        lcd.close(clear=True)
        exit()

10-01 11:28