首页 > 解决方案 > x 和 y 运行代码之间的 Python 时间。如果 xy 之间的时间没有代码

问题描述

我会尽力解释。我正在运行的脚本如下。

我想添加一些东西,使 pir 传感器在 24:00 和 05:00 之间不做任何事情。但是无论如何,该按钮都应该在这些时候起作用。

我也希望能够在一天中的某些时间发送不同的颜色。因此,如果它在晚上 8 点到晚上 11 点之间,它会将此代码提供给灯光:{"on":true,"bri":255,"sat":80,"hue":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)

标签: pythonpython-2.7raspberry-pi2

解决方案


您可以在每次迭代开始时使用 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

推荐阅读