x和y之间的Python时间运行代码。如果xy之间的时间没有代码

时间:2018-06-04 08:14:05

标签: python python-2.7 raspberry-pi2

我会尽力解释我的努力。 脚本即时运行如下。

我希望包含一些能让pir传感器在24:00到05:00之间不做任何事情的东西。但是按钮应该在那些时候工作。

此外,我希望能够在一天中的某些时间发送不同的颜色。 因此,如果它在晚上8点到晚上11点之间,它会将这些代码提供给灯光:{" on":true," bri":255," sat": 80,"色调":357}

总共会有4种颜色。我已经尝试定义使用command()调用的命令:和颜色,但我在这里停顿。

任何人都可以帮我吗?我真的希望我能在这里说清楚,但如果有什么不清楚的话就会开火。

import sys
sys.path.append("/home/pi/.local/lib/python2.7/site-packages")

from phue import Bridge
import RPi.GPIO as GPIO
import time
import datetime
print 'Waiting for network...'
time.sleep(30)
print 'The wait is over. It\'s showtime!'
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN) #Read output from PIR motion sensor
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Read output from button.
b=Bridge('192.168.1.47')
try:
    b.connect()
except ImportError:
    print "Import error occurred!"
print "Connected to Hue bridge!"

lightson=b.get_light(2, "on")
if lightson: print "Lights are already on."
print 'Entering infinite loop...'

light_on_delay = 15  # time in min for light to stay on when button pressed
button_pressed = 0

while True:
# check for button press
input_state = GPIO.input(18)
if input_state == False:
    print('Button Pressed')
    end = (light_on_delay * 1) + time.time()
    button_pressed = 1
    command =  {"on" : True, "bri" : 255, "sat" : 0, "hue" : 0}
    b.set_group(2, command)
    lightson=True
    print('Lights are on for 15 minutes')    

# check if button has been pressed if it has check to see if time is up
if button_pressed == 1:
    if time.time() > end:
        button_pressed = 0

else:

    i=GPIO.input(4)
    timestamp=datetime.datetime.now().time()
    if (timestamp < offstarttime and timestamp > offendtime):
    if i==0:            #When output from motion sensor is LOW
        print ('No movement detected - Turning lights off')
        b.set_group(2, 'on', False) 
        lightson=False
        print ('Lights are off')
        time.sleep(0.1)
    else:               #When output from motion sensor is HIGH
        print ('Movement detected - Turning lights on')
        command =  {"on" : True, "bri" : 255, "sat" : 0, "hue" : 0}
        b.set_group(2, command)
        lightson=True
        print ('Lights are on.')
        time.sleep(5)    

# added delay to prevent program using 100% cpu time.
time.sleep(0.5)

1 个答案:

答案 0 :(得分:0)

您可以在每次迭代开始时使用datetime模块添加时间检查,以有条件地设置命令字典并在某些小时之间运行PIR代码。按钮逻辑代码应在if块之外运行,以确保其始终有效

from datetime import datetime

while True:
    now = datetime.now()
    # Check to see if it is 5am or later
    if now.hour >= 5:
        # PIR sensor code here
        print("PIR sensor should work now")

    # Check to see if between 8pm and 11pm
    if now.hour >= 20 and now.hour <= 23:
        # Configure command dictionary for specific hours
        command = {"on": True,"bri": 255,"sat": 80,"hue": 357}
    else:
        # Configure command dictionary for regular hours
        command = {"on": False}

    # Rest of your code including button logic