2016-09-15 104 views
1

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

這只是一種愛好和我放在一起的東西大多是從搜索,但不能破解這個所以希望得到任何幫助,它

+0

考慮添加基本的延遲功能。請參閱:http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python –

+0

我有webiopi.sleep(1)在循環中,還包括webiopi.sleep( 5)在sendemail()函數中 - 這減少了電子郵件的數量。我不明白爲什麼這些不起作用,因爲我正在檢查時間到第二個。我本來可以嘗試60秒,但我不希望程序在這段時間內暫停 – tamus

+0

您可以使用crontabs進行調度,在cpu上更容易。 :) – Caramiriel

回答

0

假設sendemail()是一個功能你定義,您可以重寫它以存儲上次發送電子郵件的時間,如果時間不夠長,請勿發送。

now.minute == HOUR_ON.minute + 120對我來說沒有意義 - 它看起來像是在說2小時,而不是2分鐘,除非now.minute在幾秒內返回一個值?

另一種可能性:你有這個python程序的多個實例在同一時間運行嗎? pgrep python3查看有多少python實例正在運行。

+0

'sendemail()'是我放在一起的一個函數,但如果我按時存儲時間,並且在晚上沒有關閉門或第二天早上打開,我不知道如何重置它。 – tamus

+0

我想加2分鐘到'HOUR_ON.minute',我想我不得不在幾秒鐘或幾小時內加入。我運行了'pgrep python3'並得到了2141 – tamus

+0

添加了一些print()並查看了我添加120的錯誤 - 謝謝 – tamus

0

這似乎是這樣做的。後

def loop(): 
    global emailsent1 
    global emailsent2 

    # 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, 10 minutes after set time and send email if not already sent 
    if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 10) and (now.second == 0) and (gpio.digitalRead(25) == 1) and not emailsent1): 
    # send email 
    a = 'opened' 
    sendemail(a) 
    emailsent1 = True 

    if ((now.hour == HOUR_ON.hour) and (now.minute == HOUR_ON.minute + 11) and emailsent1): # reset if email has been sent 
emailsent1 = False 

    # check if door is closed, 10 minutes after set time and send email if not already sent 
    if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 10) and (now.second == 0) and (gpio.digitalRead(25) == 0) and not emailsent2): 
    # send email 
    a = 'closed' 
    sendemail(a) 
    emailsent2 = True 

    if ((now.hour == HOUR_OFF.hour) and (now.minute == HOUR_OFF.minute + 11) and emailsent2): # resset if email has been sent 
    emailsent2 = False 

    # gives CPU some time before looping again 
    webiopi.sleep(1) 

10分鐘關門時間則檢查門關閉與sendmail(一)被調用,emailsent1設置爲True - 一分鐘後,它再次被設置爲false。

我需要emailsent1和emailsent2作爲全局嗎?

相關問題