我正在研究一个raspberrypi,我想将此代码放到crontab中每分钟执行一次,它应该将gpio从19:00降低到19:minute_max(这部分有效),然后应该给我发送电子邮件当它改变GPIO的状态时。设置为“低”时,我可以给我发送电子邮件。这段代码:
from time import localtime
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
ore = 19
minuti_max=30
minuti = range(0,minuti_max)
if (localtime()[3]==ore) and (localtime()[4] in minuti):
if GPIO.input(26)==1:
GPIO.output(26, GPIO.LOW)
#send_mail_to_me("ON","yeye")
print("on")
else:
GPIO.output(26, GPIO.HIGH)
如果我在else语句中放置send_mail_to_me函数,它将每分钟向我发送一次。但我希望仅在第一次将gpio设置为高时才发送。
我尝试了以下操作,但不起作用:
from time import localtime
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
ore = 19
minuti_max=30
minuti = range(0,minuti_max)
if (localtime()[3]==ore) and (localtime()[4] in minuti):
if GPIO.input(26)==1:
GPIO.output(26, GPIO.LOW)
#send_mail_to_me("ON","yeye")
print("on")
flag=1
elif (localtime()[4] not in minuti) and flag==1:
GPIO.output(26, GPIO.HIGH)
#send_mail_to_me("OFF","yoyo")
print("off")
flag=0
else:
GPIO.output(26, GPIO.HIGH)