Python Forum

Full Version: Dusk till dawn
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

I bought a four channel relay card for rpi's gpio, my idea is to use it for light in a quail-box.
I'd like to fake dusk and dawn by switching one weak led on at 6:00am and stronger number two at 6:30am
with the below script, it works, but does someone out there like to help me clean it up make it better?


#!/usr/bin/python
import RPi.GPIO as GPIO
import os
from time import sleep     # this lets us have a time delay (see line 15)  
import string
from datetime import datetime

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering  
GPIO.setup(26, GPIO.OUT)   # set GPIO18 as an output (LED) 
GPIO.setup(6, GPIO.OUT)   # set GPIO18 as an output (LED) 
try:
    while True:
        now = datetime.now()
        h = string.atoi((now.strftime("%H")))
        m = string.atoi((now.strftime("%M")))
        s = string.atoi((now.strftime("%S")))
        tid = (h*3600)+(m*60)+s
#        print tid

        if tid > ((6*3600)+(30*60)+0) and tid < ((22*3600)+(0*60)+0):
#            print "day"
            GPIO.output(26,1)
            GPIO.output(6,1)
        else:
            if tid > ((6*3600)+(0*60)+0) and tid < ((6*3600)+(30*60)+0):
#                print "dawn"
                GPIO.output(6,1)
            elif tid > ((22*3600)+(0*60)+0) and tid < ((22*3600)+(30*60)+0):
#                print "dusk"
                GPIO.output(6,1)
            else:
#                print "night"
                GPIO.output(26,0)
                GPIO.output(6,0)
        sleep(0.3)

finally:
    GPIO.cleanup()
#except Exception:
#    print("error")
#    os.system('chicken.py')
I made a couple changes to simplify your code. Instead of breaking your time up into hours, minutes, and seconds and running a calculation each time, I changed those to datetime.time objects. It makes the code much easier to read. Instantiating those variables has also been moved above the while loop so they only created once instead of every time, which should have a marginal performance boost.

The if statements have been consolidated into a if statement for simplicity.

The try-except-finally remains although it's superfluous so long as the except clause is commented out (which I changed to a block quote).

#!/usr/bin/python
# import os
from time import sleep
from datetime import datetime, time

import RPi.GPIO as GPIO
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering  
GPIO.setup(26, GPIO.OUT)   # set GPIO18 as an output (LED) 
GPIO.setup(6, GPIO.OUT)   # set GPIO18 as an output (LED) 

try:
    dawnStart = time(hour = 6)
    dawnEnd = time(hour = 6, minute = 30)
    duskStart = time(hour = 22)
    duskEnd = time(hour = 22, minute = 30)
    while True:
        now = datetime.now().time() 

        if dawnEnd <= now <= duskStart:
            GPIO.output(26,1)
            GPIO.output(6,1)
        elif dawnStart <= now < dawnEnd:
            GPIO.output(6,1)
        elif duskStart <= now < duskEnd:
            GPIO.output(6,1)
        else:
            GPIO.output(26,0)
            GPIO.output(6,0)

        sleep(0.3)
        """
        except Exception:
            print("error")
            os.system('chicken.py')
        """"
finally:
    GPIO.cleanup()
(Oct-13-2018, 03:00 PM)stullis Wrote: [ -> ]I made a couple changes to simplify your code. Instead of breaking your time up into hours, minutes, and seconds and running a calculation each time, I changed those to datetime.time objects. It makes the code much easier to read. Instantiating those variables has also been moved above the while loop so they only created once instead of every time, which should have a marginal performance boost. The if statements have been consolidated into a if statement for simplicity. The try-except-finally remains although it's superfluous so long as the except clause is commented out (which I changed to a block quote).
 #!/usr/bin/python # import os from time import sleep from datetime import datetime, time import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering GPIO.setup(26, GPIO.OUT) # set GPIO18 as an output (LED) GPIO.setup(6, GPIO.OUT) # set GPIO18 as an output (LED) try: dawnStart = time(hour = 6) dawnEnd = time(hour = 6, minute = 30) duskStart = time(hour = 22) duskEnd = time(hour = 22, minute = 30) while True: now = datetime.now().time() if dawnEnd <= now <= duskStart: GPIO.output(26,1) GPIO.output(6,1) elif dawnStart <= now < dawnEnd: GPIO.output(6,1) elif duskStart <= now < duskEnd: GPIO.output(6,1) else: GPIO.output(26,0) GPIO.output(6,0) sleep(0.3) """ except Exception: print("error") os.system('chicken.py') """" finally: GPIO.cleanup() 

Great, during dusk both lights are lit, but it gives me new ideas, thank you
(Oct-13-2018, 03:00 PM)stullis Wrote: [ -> ]I made a couple changes to simplify your code. Instead of breaking your time up into hours, minutes, and seconds and running a calculation each time, I changed those to datetime.time objects. It makes the code much easier to read. Instantiating those variables has also been moved above the while loop so they only created once instead of every time, which should have a marginal performance boost.

The if statements have been consolidated into a if statement for simplicity.

The try-except-finally remains although it's superfluous so long as the except clause is commented out (which I changed to a block quote).

#!/usr/bin/python
# import os
from time import sleep
from datetime import datetime, time

import RPi.GPIO as GPIO
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering  
GPIO.setup(26, GPIO.OUT)   # set GPIO18 as an output (LED) 
GPIO.setup(6, GPIO.OUT)   # set GPIO18 as an output (LED) 

try:
    dawnStart = time(hour = 6)
    dawnEnd = time(hour = 6, minute = 30)
    duskStart = time(hour = 22)
    duskEnd = time(hour = 22, minute = 30)
    while True:
        now = datetime.now().time() 

        if dawnEnd <= now <= duskStart:
            GPIO.output(26,1)
            GPIO.output(6,1)
        elif dawnStart <= now < dawnEnd:
            GPIO.output(6,1)
            GPIO.output(26,0) # <------------------
        elif duskStart <= now < duskEnd:
            GPIO.output(6,1)
            GPIO.output(26,0) # <------------------
        else:
            GPIO.output(26,0)
            GPIO.output(6,0)

        sleep(0.3)
        """
        except Exception:
            print("error")
            os.system('chicken.py')
        """"
finally:
    GPIO.cleanup()

That worked, thanks.