python - How can I continuously check the hour within a while loop? -


this first post , first project in python. i'm trying control hardware sensor through number of conditions:

  1. if sensor senses movement, send e-mail , sleep
  2. elif time range not between work hours, sleep until 9am
  3. else sleep half second , wait sensor

so far first condition , last condition work. is, if there no movement, repeat sleep(0.5) until sensor activates.

issue: i'm having trouble middle portion time not between 9-5. if run code before 9 or after 5, sleeps until 9am arrives intended, if code running between 9-5 (waiting movement), middle condition never triggers. gets stuck on sleep(0.5).

i'm not experienced have vague idea second process should keep track of time. suggestions? here code.

def pir_init():     while true:         m = datetime.datetime.now().time().minute   #loop through current time         h = datetime.datetime.now().time().hour              if h < 9:                                                        print("sleeping...")             time_sleep = (9 - h) * 3600             time.sleep(time_sleep)         elif h > 16:             print("sleeping...")             time_sleep = ((24 - h) + 9) * 3600             time.sleep(time_sleep)         elif io.input(pir_pin):             print("pir alarm")             send_gmail()                time.sleep(10)      #cooldown prevent email spew         else:             print "waiting..."             time.sleep(0.5)     #sleep , wait movement             print m     print "loop end"   # main if time_elements[0] > '0' , time_elements[0] < '6':  #weekdays     if get_hour > 8 , get_hour < 17:             #hour range         pir_init()     elif get_hour > 16:         print("initialization @ 9am...")         time_sleep = ((24 - get_hour) + 9) * 3600         time.sleep(time_sleep)         pir_init()              #initiate sensor     elif get_hour < 9:         print("initialization @ 9am...")         time_sleep = (9 - get_hour) * 3600         time.sleep(time_sleep)         pir_init() else:     #to completed     print time_elements[0]     print "bad weekday" 

you have update time_elements inside while loop, program uses current time in condition test if shall sleep until 9a.m. otherwise, if don't update time_elements inside while loop, if program didn't enter in condition in first time, never enter.

that explains behavior noticing; in fact, definition of time_elements isn't shown here...


Comments

  1. The advantages of a Magento web development are many and include the ability to save hundreds or even thousands of dollars on advertising. Click here to get more information about magento webshop.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -