在GPIO.RaspberryPi3上使用触发器发送电子邮件

时间:2019-02-01 13:23:27

标签: python python-3.x raspberry-pi raspberry-pi3 gpio

我是python的新手,使用RPi3的GPIO开始执行python脚本的某些部分时遇到了一些麻烦。

为简单起见,我有一台可以在保持状态或程序中止时发出数字触发器(类似于机械开关)的机器。 我想要的是让Raspberry在触发给定的GPIO时发送电子邮件。

例如:

-处于保留状态时,机器会触发Pin 21

-中止时,机器触发20号针脚

我编写了以下代码:

import smtplib
import RPi.GPIO as GPIO
import os
import os.path
from time import gmtime, strftime, sleep
import datetime as dt


GPIO.setmode(GPIO.BCM)

#Input GPIO for trigger
GPIO_Hold=21
GPIO_Abort=20


#Setup Pins
GPIO.setup(GPIO_Hold, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_Abort, GPIO.IN, pull_up_down=GPIO.PUD_UP)


#Get get time for when the programm was started
t = dt.datetime.now()

#Function for Email
def send_mail(alert):
    HOST = "smtp.gmail.com"
    PORT = 587
    SUBJECT = "[UPDATE] Machine"
    TO = "EMAIL@EMAIL.COM"
    FROM = "RPI@EMAIL.COM"
    PWD = "RPi"
    text = alert
    BODY = string.join(("from: %s" %FROM, "to: %s" %TO,"Subject: %s" %SUBJECT, "     ", text), "\r\n")
    s = smtplib.SMTP(HOST,PORT)
    s.set_debuglevel(1)
    s.ehlo()
    s.starttls()
    s.login(FROM, PWD)
    s.sendmail(FROM,[TO],BODY)
    s.quit

#Function for Logbook file for sent infos
def log_book(alert):
    time_stamp = strftime("%Y-%m-%d_%H:%M:%S", gmtime())
    f= open("logbook12K.txt",'a+')
    f.write(time_stamp + " // " + alert + "\n")
    f.close()

# Begin Loop
try:
    while True:
        print("Waiting for Trigger...")


#If Test on Hold
        if GPIO.wait_for_edge(GPIO_Hold, GPIO.FALLING):
            alert = "Test on Hold"
            log_book(alert)
            send_mail(alert)
            sleep (1)

#If test aborted
        elif GPIO.wait_for_edge(GPIO_Abort, GPIO.FALLING):
            alert = "Test Aborted"
            log_book(alert)
            send_mail(alert)
            sleep (1)




except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
  GPIO.cleanup() # resets all GPIO ports used by this program

我知道GPIO.wait_for_edge(GPIO_Abort, GPIO.FALLING)并不是最好的解决方案,但是使用GPIO.event_detected(channel)也不可行。 处于保留状态时,我的机器将不断触发21针。我不希望RPi3持续发送数十亿封电子邮件,因此我不能只使用if GPIO.input(GPIO_Hold)

GPIO.event_detected(channel)仅检测到更改,因此当机器再次启动并且触发器恢复正常时,它还将发送“保留机器”更新。 另外,我真的不知道GPIO.add_event_detect(channel, GPIO.FALLING, callback=my_callback)是否有用,因为我无法在回调函数中抛出变量。

也许通过将def send_mail(alert):def log_book(alert):放在更大的整体功能上?

我希望我足够清楚。 谢谢!

1 个答案:

答案 0 :(得分:0)

我为那些可能感兴趣的人找到了解决方案:

import smtplib
import RPi.GPIO as GPIO
from time import gmtime, strftime, sleep
import datetime as dt


GPIO_Hold=21
GPIO_Abort=20

GPIO.setmode(GPIO.BCM)  

GPIO.setup(GPIO_Hold, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIO_Abort, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def email_logbook(channel):

    if channel == GPIO_Hold:
        alert = " Hold"
        print("Send mail "+alert)
        #Function for Logbook file for sent infos
        print("Logbook "+alert)

    elif channel== GPIO_Abort:
        alert = " Aborted"
        print("Send mail "+alert)
        #Function for Logbook file for sent infos
        print("Logbook "+alert)

GPIO.add_event_detect(GPIO_Hold, GPIO.FALLING, callback=email_logbook, bouncetime=1000)     
GPIO.add_event_detect(GPIO_Abort, GPIO.FALLING, callback=email_logbook, bouncetime=1000)    

count=0
try:
    while True: 
        count=count+1

except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit

GPIO.cleanup()           # clean up GPIO

您必须使用ifelif在回调函数中处理事件,在其中检查事件的GPIO引脚