Sadek
Posts: 26
Joined: Wed Jul 06, 2016 9:26 pm

triger script for relay in python (without cleanup)

Wed Jul 06, 2016 9:47 pm

Code: Select all

import RPi.GPIO as GPIO            # import RPi.GPIO module  
from time import sleep             # lets us have a delay  
GPIO.setmode(GPIO.BCM)             # choose BCM or BOARD  
GPIO.setup(14, GPIO.OUT)           # set GPIO24 as an output     
try:  
    while True:
        GPIO.input(14)          # set GPIO24 to 1/GPIO.HIGH/True  
        sleep(0.5)                  # wait half a second
        GPIO.output(14, 1)
    if GPIO.input(14):
        GPIO.output(14, 0)
except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt  
    GPIO.cleanup()
Hello I try to make smart room and turn on/off lights etc. via www using shh to execute python script on raspberry

but after i run program above, command cleanup turn switch off. How too end it without cleanup command ?
i'm begginer in raspberry and in pythone and try figure it out alone exit() quit() don't work maybe my whole idea is wrong ?

I have hope someone of You can help me and tell me if the idea of buttons on www are good ? maybe some article anything that can help me are welcome.

4LULZ
Posts: 26
Joined: Sat May 07, 2016 1:30 am

Re: triger script for relay in python (without cleanup)

Fri Jul 08, 2016 5:42 am

Apart from switching off when the user presses Ctrl+C, does that code run as you expect it to?

To me it looks as though that code would make the light flash for 1/2 second repeatedly until the user triggers the KeyboardInterrupt. I'm not sure if your first implementation of the GPIO.input() command is actually doing anything, see my comment below -

Code: Select all

import RPi.GPIO as GPIO            # import RPi.GPIO module  
from time import sleep             # lets us have a delay  
GPIO.setmode(GPIO.BCM)             # choose BCM or BOARD  
GPIO.setup(14, GPIO.OUT)           # set GPIO14 as an output     
try:  
    while True:
        GPIO.input(14)          # This line looks unnecessary, what does it do? 
        sleep(0.5)                  # wait half a second
        GPIO.output(14, 1)  #  set GPIO14 to 1/GPIO.HIGH/True  
    if GPIO.input(14):         # If GPIO14 is TRUE/HIGH
        GPIO.output(14, 0)  # Set GPIO to FALSE/LOW
except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt  
    GPIO.cleanup()
This instructable might help get you on the right track....

Return to “Beginners”