Things used in this project

Code

Sample RPI Phillips Hue Connector Python
This is the full code
#RPI connector between IFTTT Maker channel and Phillips Hue
# I write a lot of stuff to the console to act as a log

from __future__ import print_function
from flask import Flask, request
import sys
import pyhue
from time import sleep
from datetime import datetime, timedelta
from apscheduler.schedulers.background import BackgroundScheduler

# with out this "No handlers could be found for logger error" occurs
import logging
logging.basicConfig()
bridge=pyhue.Bridge('192.167.0.6','username')

def sunsetLights():
    for light in bridge.lights:
        if light.name=='Office lamp':
           light.on=True
        elif light.name=='Corner lamp':
            light.on=True
        elif light.name=='Reading lamp':
            light.on=True

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello world'

@app.route('/lights', methods=['GET','POST'])
def lights():
    lcommand=request.args.get('Command')
    print (lcommand, file=sys.stderr)
    if lcommand=='sunset':
        #turn on lights 30 minutes before sunset
        sunsettime=request.args.get('sunsetat')
        print (sunsettime, file=sys.stderr)
        #convert date format
        sunset_py_time=datetime.strptime(sunsettime, '%B %d, %Y at %I:%M%p')-timedelta(minutes=30)
        cur_time=datetime.now()
        print (sunset_py_time, file=sys.stderr)
        #schedule job
        scheduler = BackgroundScheduler()
        scheduler.add_job(sunsetLights, 'date', run_date=sunset_py_time, id='sunsetsched', replace_existing=True)
        print ('sunset lights scheduled', file=sys.stderr)
        print (sunset_py_time, file=sys.stderr)
        scheduler.start()
    elif lcommand=='wificonnect':
        #on wifi connect, turn on porch light if its after 4pm and before midnight
        cur_time=datetime.now().time()
        if cur_time.hour>16 and cur_time.hour<24:
            for light in bridge.lights:
                if light.name=="Porch light":
                    light.on=True
                    print (cur_time.hour, filesys.stderr)
                    print ('porch light on in window', file=sys.stderr)
        else:
            print('porch light command not in window', file=sys.stderr)
    return stuff 


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Credits

Edb65ef924bc577cc5859e658530ac51
Randy Reed
1 project • 0 followers
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog