我有一个程序来控制许多关于我母鸡房子的事情,包括在设定的时间打开和关闭门。偶尔会发生一些事情,门不会打开或关闭,现在我发现它发送了一封电子邮件。问题是它会发送6个或更多的电子邮件,我一直在努力解决如何限制它只发送一个可能使用的时间或如果 - 但然后我需要重新设置它,以便如果再次发生在另一个它会发送另一封电子邮件。这是循环,我有限制程序只发送一封电子邮件
def loop():
# retrieve current datetime
now = datetime.time(datetime.datetime.now().hour, datetime.datetime.now().minute)
# Open door on all days at the correct time
if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute)):
if (gpio.digitalRead(17) == 0):
openplay()
# Close door on all days at the correct time
if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute)):
if (gpio.digitalRead(22) == 1):
closeplay()
# check if door is open, 2 minutes after set time
if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 120) and (now.second == 0) and (gpio.digitalRead(25) == 0)):
# send email
sendemail()
# check if door is closed, 2 minutes after set time
if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 120) and (now.second == 0) and (gpio.digitalRead(25) == 1)):
# send email
sendemail()
# gives CPU some time before looping again
webiopi.sleep(1)
这只是一种爱好和我放在一起的东西大多是从搜索,但不能破解这个所以希望得到任何帮助,它
考虑添加基本的延迟功能。请参阅:http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python –
我有webiopi.sleep(1)在循环中,还包括webiopi.sleep( 5)在sendemail()函数中 - 这减少了电子邮件的数量。我不明白为什么这些不起作用,因为我正在检查时间到第二个。我本来可以尝试60秒,但我不希望程序在这段时间内暂停 – tamus
您可以使用crontabs进行调度,在cpu上更容易。 :) – Caramiriel