我一直试图使这个代码,我只有一次电子邮件后,门别针已被绊倒。我是python的新手,一直在阅读有类似问题的帖子,并尝试在我的脚本中看到的东西。但似乎不能让它只发送一次电子邮件。
enter code here
import webiopi
import datetime
import smtplib
import subprocess
import os
GPIO = webiopi.GPIO
ARM = 7 # GPIO pin using BCM numbering
FLASH = 8
START = 25 # relay
DOOR = 11
HOUR_ON = 8 # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00
def setup():
GPIO.setFunction(ARM, GPIO.OUT)
GPIO.setFunction(FLASH, GPIO.OUT)
GPIO.setFunction(START, GPIO.OUT)
GPIO.setup(DOOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
now = datetime.datetime.now()
# test if we are between ON time and tun the light ON
if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
GPIO.digitalWrite(ARM, GPIO.HIGH)
GPIO.digitalWrite(FLASH, GPIO.HIGH)
def loop():
# retrieve current datetime
now = datetime.datetime.now()
# toggle light ON all days at the correct time
if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.LOW):
GPIO.digitalWrite(ARM, GPIO.HIGH)
# toggle light OFF
if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(ARM, GPIO.LOW)
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(FLASH, GPIO.HIGH)
webiopi.sleep(.5)
GPIO.digitalWrite(FLASH, GPIO.LOW)
if (GPIO.digitalRead(DOOR) == GPIO.HIGH):
SendText()
# gives CPU some time before looping again
webiopi.sleep(1)
def SendText():
os.system("python /home/pi/html/myemail.py")
# destroy function is called at WebIOPi shutdown
def destroy():
GPIO.digitalWrite(ARM, GPIO.LOW)
GPIO.digitalWrite(START, GPIO.LOW)
@webiopi.macro
def getLightHours():
return "%d;%d" % (HOUR_ON, HOUR_OFF)
@webiopi.macro
def setLightHours(on, off):
global HOUR_ON, HOUR_OFF
HOUR_ON = int(on)
HOUR_OFF = int(off)
return getLightHours()发布于 2015-06-30 22:38:57
您可以设置变量send_email来跟踪是否到了发送电子邮件的时间,以及该电子邮件是否已发送。
创建一个全局变量send_email = False,因为该变量将进入循环方法并需要初始化。
添加到SendText() if语句和send_email
在SendText()行之后设置send_email = False
添加另一个if语句以检查是否为GPIO.LOW,并将send_email更改为True
我已经对您的代码进行了建议的更改。我把我添加的所有文本都用*括起来。确保从代码中删除所有的*,这样它才能工作。
import webiopi
import datetime
import smtplib
import subprocess
import os
GPIO = webiopi.GPIO
ARM = 7 # GPIO pin using BCM numbering
FLASH = 8
START = 25 # relay
DOOR = 11
HOUR_ON = 8 # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00
*send_email = False*
def setup():
GPIO.setFunction(ARM, GPIO.OUT)
GPIO.setFunction(FLASH, GPIO.OUT)
GPIO.setFunction(START, GPIO.OUT)
GPIO.setup(DOOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
now = datetime.datetime.now()
# test if we are between ON time and tun the light ON
if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
GPIO.digitalWrite(ARM, GPIO.HIGH)
GPIO.digitalWrite(FLASH, GPIO.HIGH)
def loop():
# retrieve current datetime
now = datetime.datetime.now()
# toggle light ON all days at the correct time
if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.LOW):
GPIO.digitalWrite(ARM, GPIO.HIGH)
# toggle light OFF
if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(ARM, GPIO.LOW)
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(FLASH, GPIO.HIGH)
webiopi.sleep(.5)
GPIO.digitalWrite(FLASH, GPIO.LOW)
if (GPIO.digitalRead(DOOR) == GPIO.HIGH) *and send_email*:
SendText()
*send_email = False*
*if (GPIO.digitalRead(DOOR) == GPIO.LOW) and not send_email*:
*send_email = True*
# gives CPU some time before looping again
webiopi.sleep(1)
def SendText():
os.system("python /home/pi/html/myemail.py")
# destroy function is called at WebIOPi shutdown
def destroy():
GPIO.digitalWrite(ARM, GPIO.LOW)
GPIO.digitalWrite(START, GPIO.LOW)
@webiopi.macro
def getLightHours():
return "%d;%d" % (HOUR_ON, HOUR_OFF)
@webiopi.macro
def setLightHours(on, off):
global HOUR_ON, HOUR_OFF
HOUR_ON = int(on)
HOUR_OFF = int(off)
return getLightHours()https://stackoverflow.com/questions/29065302
复制相似问题