运动感应回路错误 Python?

Motion Sensing Loop Error Python?

大家好,我已经研究这段代码很长时间了,但是一旦代码感应到运动,拍了一张照片并将其作为电子邮件附件发送出去,它就可以工作一次,但是一旦状态 returns准备好它会出现此错误,但我不明白为什么。我为包括整个代码而道歉,因为尽管错误指出第 43 行我不确定是什么导致它出错。 任何建议将不胜感激。我是 python 编程的初学者,所以我可能遗漏了一些非常明显的东西。顺便说一句,对于任何想要测试代码的人,我 运行 在 raspberry pi 上发表了这篇文章。 谢谢

import os, re
import sys
import smtplib
import RPi.GPIO as GPIO
import time
import picamera
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


GPIO.setmode(GPIO.BCM)


GPIO_PIR = 4

print "PIR Module Test (CTRL-C to exit)"


GPIO.setup(GPIO_PIR,GPIO.IN)     

Current_State  = 0
Previous_State = 0
cam = picamera.PiCamera()
try:

  print "Waiting for PIR to settle ..."


  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0

  print "  Ready"


  while True :


    Current_State = GPIO.input(GPIO_PIR)

    if Current_State==1 and Previous_State==0:
      print "  Motion detected!"
      cam.capture('/home/pi/Eaglecam/surveillance.jpg')
      print('picture taken')
      cam.close()
      SMTP_SERVER = 'smtp.gmail.com'
      SMTP_PORT = 587       
      sender = '*******************'
      password = "secret!!"
      recipient = '*********************'
      subject = 'INTRUDER DETECTER!!'
      message = 'INTRUDER ALLERT!! INTRUDER ALERT!! CHECK OUT THIS PICTURE OF THE INTRUDER! SAVE THIS PICTURE AS EVIDENCE!'

      directory = "/home/pi/Eaglecam/"

      def main():
          msg = MIMEMultipart()
          msg['Subject'] = 'INTRUDER ALERT'
          msg['To'] = recipient
          msg['From'] = sender

          files = os.listdir(directory)
          jpgsearch = re.compile(".jpg", re.IGNORECASE)
          files = filter(jpgsearch.search, files)
          for filename in files:
              path = os.path.join(directory, filename)
              if not os.path.isfile(path):
                  continue

              img = MIMEImage(open(path, 'rb').read(), _subtype="jpg")
              img.add_header('Content-Disposition', 'attachment', filename=filename)
              msg.attach(img)

          part = MIMEText('text', "plain")
          part.set_payload(message)
          msg.attach(part)

          session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

          session.ehlo()
          session.starttls()
          session.ehlo
          session.login(sender, password)

          session.sendmail(sender, recipient, msg.as_string())
          session.quit()

      if __name__ == '__main__':   
      print('  Email Sent')
      Previous_State=1
    elif Current_State==0 and Previous_State==1:

      print "  Ready"
      Previous_State=0

    time.sleep(0.01)

except KeyboardInterrupt:
  print "  Quit"

  GPIO.cleanup()

这是错误:

Traceback (most recent call last):
  File "/home/pi/Python/hi2.py", line 43, in <module>
    cam.capture('/home/pi/Eaglecam/surveillance.jpg')
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1446, in capture
    camera_port, output_port = self._get_ports(use_video_port, splitter_port)
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 708, in _get_ports
    self._camera[0].output[self.CAMERA_CAPTURE_PORT]
TypeError: 'NoneType' object has no attribute '__getitem__'

尽量不要持久化您的 cam 对象...picamera 会在它认为合适的时候刷新和关闭,因此您可能会在使用您正在使用的代码重复调用该对象时遇到问题。此外,如果拍摄第一张照片,您将在 cam 对象上调用 .close() 方法。我可能会这样完成:

surv_pic = open('/home/pi/Eaglecam/surveillance.jpg', 'wb')
if Current_State==1 and Previous_State==0:
    print "  Motion detected!"
    with picamera.PiCamera() as cam:
        ##if camera is not producing pictures because it is not warmed up yet uncomment the next two lines
        ##cam.start_preview()
        ##time.sleep(2)
        cam.capture(surv_pic)
    surv_pic.close()
    SMTP_SERVER = 'smtp.gmail.com'
    ## continue as is from here