Skip to content

Instantly share code, notes, and snippets.

@RZRZR
Last active August 29, 2015 14:14
Show Gist options
  • Save RZRZR/7782eb37a249e62fe8c2 to your computer and use it in GitHub Desktop.
Save RZRZR/7782eb37a249e62fe8c2 to your computer and use it in GitHub Desktop.
from energenie import switch_on, switch_off
from time import sleep
from datetime import datetime, time
import RPi.GPIO as GPIO
import thread
now = datetime.now()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
tv_switch = 2
GPIO.setup(tv_switch, GPIO.IN, GPIO.PUD_UP)
def blanket():
if time(21,30) <= now.time() <= time(00,30):
switch_on(1)
print("blanket on")
else:
switch_off(1)
print("blanket off")
sleep(1)
def heater():
if time(20,30) <= now.time() <= time(00,30):
switch_on(2)
print("heater on")
if time(07,30) <= now.time() <= time(09,00):
switch_on(2)
print("heater on")
else:
switch_off(2)
print("heater off")
sleep(1)
def tv():
if tv_switch True:
switch_on(3) # Turn on USB hub powering Pi and speakers
print("TV on")
else:
switch_off(3)
print("TV off")
sleep(1)
def LightLevel(pin):
reading = 0
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(pin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(pin) == GPIO.LOW):
reading += 1
return reading
def UnicornPattern():
def Unicorn(LightValue):
if LightValue > 25: # Light is on
print("Unicorn - light's on, awaiting")
time.sleep(1)
if LightValue < 25: # then light is turned off
print("Unicorn - doing my thang")
UnicornPattern() # Dim light for 30 seconds
else:
print("Unicorn - it's dark")
def main():
print("Blanket & Heater thread")
blanket()
heater()
time.sleep(10)
GPIO.add_event_detect(2, GPIO.FALLING, tv, 1000)
if __name__ == '__main__':
try:
thread.start_new_thread(main())
thread.start_new_thread(Unicorn(LightLevel(18)))
except KeyboardInterrupt:
GPIO.cleanup()
finally:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment