【问题标题】:Button Press To Shutdown Raspberry Pi Python按下按钮关闭 Raspberry Pi Python
【发布时间】:2015-05-04 11:09:14
【问题描述】:

我正在为监控摄像头编写脚本。我已将其编程为在检测到运动时拍照(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?在这种情况下,只需启动两个脚本,您的操作系统确实有一个调度程序,即使您在其中一个脚本中有无限循环,也会为这两个脚本提供处理时间。否则问题需要进一步澄清。
  • 我只想知道将关闭代码放在循环中的什么位置。对不起,不清楚的问题。脚本的关闭部分在底部
  • 好的,但是你想什么时候关机?按下按钮时或发送电子邮件后?
  • 为什么要使用一个脚本而不是两个?这样你就有一个检查按钮是否按下并在按下时关闭,还有一个运行入侵者检测?
  • 是的,但是你可以用另一个无限循环来启动另一个循环来检查按钮是否被按下,不是吗?所以步骤是:启动;以无限循环启动脚本 1;以无限循环启动脚本 2;当您确实在后台运行操作系统时,这是可能的。

标签: python button raspberry-pi shutdown headless


【解决方案1】:

此项目不需要两个单独的脚本。

您可以通过多种方式做到这一点。

  1. 创建一个名为shutdownButton 或其他的全局布尔变量。为 GPIO 引脚使用回调函数,并在回调中设置shutdownButton = True。然后将主循环更改为 while not shutdownButton: 而不是 while True:
  2. 您可以将主循环更改为 while gpio.input(7): 而不是 while True:(假设您将引脚向上拉,然后用开关将其接地)。

最后,只需添加一个像 os.system('sudo shutdown -h now') 这样的关闭或调用您想要运行的其他脚本来清理它。关键是您只需要一个函数来在按下按钮时跳出主循环,然后在程序结束时关闭您的 pi。
还有其他方法可以做到这一点(我个人喜欢 Adafruit 的内核补丁,它允许将电源开关配置添加到 /etc/modprobe.d...),但我只列出了直接适用于您的问题的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多