1
我正在为监控摄像头编写脚本。我已将其编程为在检测到运动(PIR传感器)时拍摄照片,然后将照片附加到电子邮件中,发送给选定的收件人。但是因为我没有安装屏幕,鼠标或键盘,所以无法在没有拔插头的情况下关闭设备!所以我创建了一个脚本来关闭按钮上的计算机(这本身工作正常),但是,因为其余的代码是在一个循环中,我不知道该把它放在哪里。请记住,我需要能够在代码中的任何位置关闭它。 如果您有任何意见,他们将不胜感激 感谢 詹姆斯按钮关闭树莓派蟒
import os, re
import sys
import smtplib
import RPi.GPIO as GPIO
import time
import picamera
inport os
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
import RPi.GPIO as gpio
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
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)
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:
cam.capture(surv_pic)
surv_pic.close()
print(' Picture Taken')
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = '**************'
password = "**********"
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)
import time
import RPi.GPIO as gpio
except KeyboardInterrupt:
print('Quit')
GPIO.cleanup()
这里是脚本的关闭部分。我将把它放在循环中?
gpio.setmode(gpio.BCM)
gpio.setup(7, gpio.IN, pull_up_down=gpio.PUD_UP)
buttonReleased = True
while buttonReleased:
gpio.wait_for_edge(7, gpio.FALLING)
buttonReleased = False
for i in range(1):
time.sleep(0.1)
if gpio.input(7):
buttonReleased = True
break
恩,好吧,我不清楚你想做什么。所以你有这个脚本在运行(还有一个像Raspian这样的操作系统,我假设?!),并且当你按下按钮时你想关闭Raspi?当出现这种情况时,只需启动两个脚本,您的操作系统就会有一个调度程序,即使您在其中一个脚本中有无限循环,它也会为这两个脚本提供处理时间。否则,这个问题需要更多的澄清。 – Salo
我只想知道在哪里放置关闭代码在循环中。对不清楚的问题抱歉。脚本的关机部分在底部 – James
好的,但是你想关闭什么时候?按下按钮或发送电子邮件后? – Salo