2016-09-15 73 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作为全局吗?

相关问题