【问题标题】:Unexpected results in Raspberry Pi email notifierRaspberry Pi 电子邮件通知程序中的意外结果
【发布时间】:2014-09-22 03:40:40
【问题描述】:

刚买了一个树莓派 b+ 并决定处理一些“简单”的项目。目前我正在尝试将我的 gmail 帐户同步到 LED 通知器。

如果我有电子邮件,绿色 LED 会亮起。如果我没有电子邮件,红色的会亮起。问题是......因为它目前的功能,当我没有电子邮件时,绿色 LED 会亮起,但是,当我确实有至少 1 封电子邮件时,两个 LED 都不会亮起。我知道硬件方面的一切都在工作,因为我测试了不同的组合。

我已经粘贴了来自下面tutorial 的代码,据我所知,这是一个非常简单的条件,带有一些额外的东西,因此它可以与我的树莓派对话。

if-statement 总是返回 true 似乎很奇怪。

import RPi.GPIO as GPIO, feedparser, time

DEBUG = 1

USERNAME = "my-username"
PASSWORD = "my-password"     

NEWMAIL_OFFSET = 0        # empty inbox
MAIL_CHECK_FREQ = 60      # check mail every 60 seconds

GPIO.setmode(GPIO.BCM)
GREEN_LED = 18
RED_LED = 23
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)

while True:

    newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom")["feed"]["fullcount"])

    if DEBUG:
            print "You have", newmails, "new emails!"

    if newmails > NEWMAIL_OFFSET:
            GPIO.output(GREEN_LED, True)
            GPIO.output(RED_LED, False)
    else:
            GPIO.output(GREEN_LED, False)
            GPIO.output(RED_LED, True)

    time.sleep(MAIL_CHECK_FREQ)
!

这是我的设置...

【问题讨论】:

  • 我们不知道您的硬件是如何连接的。
  • 底部的感叹号是什么?你检查过你没有混合制表符和空格吗?
  • 制表符中没有任何空格。感叹号来自我发布的教程链接,据我所知,它只是说这是文件的结尾 - 当我输入 nano ./filename.py 时甚至没有出现。
  • 为了清楚起见,你能在调试输出中看到正确的电子邮件数量吗?
  • @Himal 是的,调试器总是输出正确和预期的结果。

标签: python linux raspberry-pi


【解决方案1】:

从您的硬件图片来看,可能是您的电阻器接触到了那里。此外,通向绿色 LED 的引线并没有在空行上。目前,它会与引脚 16 交互,并且当高电压耗尽时,您的电压会导致...这篇文章可能会有所帮助

https://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds/wire-leds

【讨论】:

    【解决方案2】:

    首先澄清一下:只要您的收件箱中至少有一封未读邮件,代码不会通知您有新邮件。

    灯的问题确实与硬件有关,因为我只是在家中进行了相同的设置并且效果很好。不过,我对代码做了一些小改动:

    import RPi.GPIO as GPIO, feedparser, time, sys
    
    USERNAME = "my-username"
    PASSWORD = "my-password"     
    
    NEWMAIL_OFFSET = 0        # empty inbox
    MAIL_CHECK_FREQ = 60      # check mail every 60 seconds
    
    GPIO.setmode(GPIO.BCM)
    GREEN_LED = 18
    RED_LED = 23
    GPIO.setup(GREEN_LED, GPIO.OUT)
    GPIO.setup(RED_LED, GPIO.OUT)
    
    while True:
    
        try:
            newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom")["feed"]["fullcount"])
    
            print "You have ", newmails, " unread emails!"
    
            if newmails > NEWMAIL_OFFSET:
                GPIO.output(GREEN_LED, True)
                GPIO.output(RED_LED, False)
            else:
                GPIO.output(GREEN_LED, False)
                GPIO.output(RED_LED, True)
    
            time.sleep(MAIL_CHECK_FREQ)
    
        except KeyboardInterrupt:
            #Ctrl + C
            #Ensure you are cleaning up the GPIO setup or it might not work correctly after the first run
            GPIO.cleanup()
            break
        except:
            #Here you can capture possible unknown issues with the code
            print("There following error happened: " + str(sys.exc_info()[1]))
        finally:
            pass
    

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 2012-05-02
      • 1970-01-01
      相关资源
      最近更新 更多