我有一個程序來控制許多關於我母雞房子的事情,包括在設定的時間打開和關閉門。偶爾會發生一些事情,門不會打開或關閉,現在我發現它發送了一封電子郵件。問題是它會發送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