Bottom Page

Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Need some help with merging two scripts
#1
Would you be able to assist a noobie here with a small python script?

Editing my question. As the below code worked for what it was intended.

Now what i am trying to achieve is that i also have a second script. (posted below) That controls a PIR Sensor wich ALSO turns the light on. But i would like to try and merge the two scripts into 1 single script.

It should as default use the PIR sensor to turn the light on when movement is detected. But! If the button is pushed it should override the sensor and leave the light on for 15 minutes.

As i have it set up now the pir does its job. But if there is no movement in front of it and i push the button it turns the light on then quickly off again as the pir is detecting no movement..

Both scripts below. All help i can get is very very much appreciated. :)

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(4, GPIO.IN)         #Read output from PIR motion sensor
while True:
   i=GPIO.input(4)
   if i==0:                 #When output from motion sensor is LOW
         print "No movement detected - Turning lights off",i
         exec(open("./LightsOff.py").read(), globals())
         time.sleep(0.1)
   elif i==1:               #When output from motion sensor is HIGH
         print "Movement detected - Turning lights on",i
         exec(open("./LightsOn.py").read(), globals())
         time.sleep(60 * 15)


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
input_state = GPIO.input(18)
if input_state == False:
    print('Button Pressed')
    execfile("LightsOn.py")
    time.sleep(0.2)
Quote
#2
Hello!

One is Python 2 script the other Python 3... But not really. The exec(open(fd).read()) is Python 3 function but in the same script is used print statement instead of the puthon3 print function. Confusing. The same in the other script. The print function is used which indicates python3 but execfile is python2 function. Smile

Anyway!
The movement sensor is read if the button is not pressed. However, I don't know how long the state of the button is keeped so I change its state ( input_state ) after turning the lights off. Pay attention to this. I am not familiar with Pi programming at all. Or the kind of button/switch.

import RPi.GPIO as GPIO
import time


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4, GPIO.IN)         #Read output from PIR motion sensor

while True:
    
    input_state = GPIO.input(18)
    if input_state == False:
        print('Button Pressed')
        exec(open("LightsOn.py").read())
        time.sleep(60 * 15)
        exec(open("./LightsOff.py").read())
        input_state = True
    else:
        
        i=GPIO.input(4)
        if i==0:                 #When output from motion sensor is LOW
             print("No movement detected - Turning lights off",i)
             exec(open("./LightsOff.py").read(), globals())
             time.sleep(0.1)
        elif i==1:               #When output from motion sensor is HIGH
             print("Movement detected - Turning lights on",i)
             exec(open("./LightsOn.py").read(), globals())
             time.sleep(60 * 15)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#3
Got some more help on another forum, but this also really helped me along.

Currently stuck here, line 50 is causing issues and im not able to see why.

import RPi.GPIO as GPIO
import time
import requests

def LightsOn():
	colors = {
	            'dailywhite':'{"on":true,"bri":255,"sat":0,"hue":0}',
	            'latewhite':'{"on":true,"bri":255,"sat":80,"hue":357}',
                'midnight':'{"on":true,"bri":255,"sat":68,"hue":357}',
                'afterparty':'{"on":true,"bri":255,"sat":255,"hue":0,"effect":"colorloop"}'
			}
 
hour = time.localtime().tm_hour

time_colors = {(0,2):'midnight', (2,7):'afterparty', (7, 20):'dailywhite', (20,24):'latewhite'}
color_schema  = {hour:color for hours, color in time_colors.items() for hour in range(hours[0], hours[1])}
color = colors[color_schema[hour]]

headers = {
    'Accept': 'application/json',
}
 
requests.put('http://192.168.1.47/api/xxxxxxxxxxxxxxxxxxxxxx/lights/2/state', headers=headers, data=color)

def LightsOff():
    time.sleep(0.1)

headers = {
    'Accept': 'application/json',
}

data = '{"on":false}'

requests.put('http://192.168.1.47/api/xxxxxxxxxxxxxxxxxxxxxxxxx/lights/2/state', headers=headers, data=data)


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(4, GPIO.IN)         #Read output from PIR motion sensor
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

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 time = time.time + (light_on_delay * 60)
        button_pressed = 1
        LightsOn()
    
   
    # 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)
        if i==0:                 #When output from motion sensor is LOW
             print "No movement detected - Turning lights off",i
             LightsOn()
             time.sleep(0.1)
        else:               #When output from motion sensor is HIGH
             print "Movement detected - Turning lights on",i
             LightsOff()
             time.sleep(60 * 15)

    # added delay to prevent program using 100% cpu time.
    time.sleep(0.5)
Quote
#4
'Issues' is not very descriptive.
Have you tried my example?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#5
Your example worked somewhat. But it wouldnt continiue after pir had seen movement

This is how it looks after ive been tinkering.
The error is here on line 17:
        end time = time.time + (light_on_delay * 60)
And this is the error message:

  File "button.py", line 17
    end time = time.time + (light_on_delay * 60)
           ^
SyntaxError: invalid syntax
import RPi.GPIO as GPIO
import time

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)

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 time = time.time + (light_on_delay * 60)
        button_pressed = 1
        #exec(open("./LightsOn.py").read(), globals())
        print('button push')
    
   
    # 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)
        if i==0:            #When output from motion sensor is LOW
            print ('No movement detected - Turning lights off')
            #exec(open("./LightsOn.py").read(), globals()) 
            print ('off')
            time.sleep(0.1)
        else:               #When output from motion sensor is HIGH
            print ('Movement detected - Turning lights on')
            #exec(open("./LightsOff.py").read(), globals())
            print ('on')
            time.sleep(60 * 15)

    # added delay to prevent program using 100% cpu time.
    time.sleep(0.5)
Quote
#6
time.time is a method. You have to call it.
>>> import time
>>> time.time()
1527294696.1632197
So:
end time = time.time() + (light_on_delay * 60) # see the brackets?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)