Randy Reed
Published © GPL3+

Unlock Phillips Hue With IFTTT Maker, Raspberry PI

IFTTT is simple to use with Phillips Hue but its simplicity can be its downfall. Unlock its power with the Maker Channel and Raspberry Pi.

IntermediateProtip16,360
Unlock Phillips Hue With IFTTT Maker, Raspberry PI

Things used in this project

Story

Read more

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

Randy Reed

Randy Reed

1 project • 0 followers

Comments