Python Forum
Sending different color codes in python at certain times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending different color codes in python at certain times
#1
Hi all..

Looking for some help here.
Using below script.
Trying to add the following functions:

I want the pir sensor to basically do nothing between midnight and 0500. So disabled at certain times, but button should work still.

I want to be able to send different colors to the lights at certain times of the day.
So if motion is detected between 7am and 12 am it will be this color code sendt {"on" : True, "bri" : 255, "sat" : 0, "hue" : 0} but when its between 8pm and 23pm this will be sendt {"on" : True, "bri" : 255, "sat" : 173, "hue" : 333} for example.

Any easy way of achieving this? I have tried defining the command called when sending the color codes but im at a standstill here.

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)
Reply


Messages In This Thread
Sending different color codes in python at certain times - by Kimzer - Jun-04-2018, 09:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending a text from Python sawtooth500 2 335 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,228 Oct-25-2023, 09:09 AM
Last Post: codelab
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 7,229 Mar-02-2022, 01:23 AM
Last Post: tomtom
  Sending string commands from Python to a bluetooth device Rovelin 13 9,922 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  How to set LD_LIBRARY_PATH on python codes? aupres 1 8,671 Mar-25-2021, 08:49 AM
Last Post: buran
  Automating to run python script 100 times by changing parameters pmt 1 2,665 Dec-29-2020, 10:31 AM
Last Post: andydoc
  Sending Out Email via Python JoeDainton123 1 4,888 Aug-31-2020, 12:54 AM
Last Post: nilamo
  How to load log.txt directly into python codes? sparkt 6 3,101 Aug-21-2020, 03:51 PM
Last Post: sparkt
  Python sockets : Client sending to the server nico31780 0 2,428 May-17-2020, 07:56 PM
Last Post: nico31780
  problems sending BIN file with XMODEM python shaya2103 0 2,914 Nov-23-2019, 10:27 AM
Last Post: shaya2103

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020