Create a colorful sunrise in your room each morning

Requirements:

  • Philips Hue Color lights with hub. I have 3 of these lights.
  • PC with python installed.  It must be on the same network as your Hue lights. I’m using Ubuntu 16.04, but this should work in Windows or a Mac as well.
  • pip
  • Daily access to your local dawn and/or sunrise time
  • Phue which is a Python library for the Philips hue system.

Helpful:

  • A python development environment. I’m using Ninja IDE
  • A small program to fetch colors from a Hue light set to some desired color: see below

Note: pip is already installed if you’re using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloaded from python.org, but you’ll need to upgrade pip.If you don’t have python pip, open a terminal and:

sudo apt-get install python-pip

Instructions and code:

The first thing you will need is the IP address of your hue hub. You can find that by opening the administration console of your home router in your browser. Navigate to the page showing the IP addresses of your router’s hardwired clients. Look for the client named Philips Hue to find the address you need.

Installing phue: Download the phue library from: https://github.com/studioimaginaire/phue

Then open a terminal and navigate to the directory where you downloaded the phue ibrary (probably ~/Downloads). Then:

pip install phue

Your program/system needs to be registered with the Hue hub. Here’s a small Python program to do that.

#!/usr/bin/python
import phue
b.connect

Enter this program with a text editor then save it to a file called ~/connect.py. Then open a terminal and:

chmod  +x ~/connect.py

followed by:

~/connect.py

And press the button on your Hue hub. This will register your computer on the Hue hub. It only needs to be done one time. At least that was how it worked on my system. Your mileage may vary of course.

The daily sunrise sunset times are available at: http://sunrise-sunset.org/

The sunrise program is below. There are two places you will need to modify. The first is to enter your own longitude and latitude where it is indicated by bold italics within in the program. You can get them here:

http://www.gps-coordinates.net/

You will also need to enter the IP address of your Hue hub where it is indicated by bold italics within the program.

Here is the program:

 #!/usr/bin/python
 #---------------------------------------------------------------------
 # This program creates a simulated sunrise using philips hue lights.
 # It is scheduled to run each morning before dawn and then waits until
 # dawn to begin the simulation
 # --------------------------------------------------------------------
 from phue import Bridge
 import urllib
 import datetime
 import time
 import logging
 # convert HH:MM to minute of the day
 def get_min(time_str):
 h, m = time_str.split(':')
 return int(h) * 60 + int(m)
 # Gets the time of dawn at my longitude and latitude in UTC time

def dawn():
 req = 'http://api.sunrise-sunset.org/json?\
#---------------------------------------
# Insert your longitude and latitude in the line below 
 lat=yourlongitude&lng=yourlatitude&formatted=0'
 response = urllib.urlopen(req)
 timestring = response.read()
 dawn_delay = 10 * 60
 # utcsunrise = timestring[34:39]
 # utcsunset = timestring[71:76]
 utcmorning = timestring[182:187]
 # utcnight = timestring[231:236]
 # get the current time HH:MM
 now = datetime.datetime.utcnow().strftime("%H:%M")
 print now, utcmorning
 now1 = get_min(now)
 morn = get_min(utcmorning)
 # wait until morning to begin "sunrise"
 while now1 < morn:
 now = datetime.datetime.utcnow().strftime("%H:%M")
 now1 = get_min(now)
 time.sleep(15)
 time.sleep(dawn_delay)
 now = datetime.datetime.utcnow().strftime("%H:%M")
 #    print now, utcmorning

# Main Program
 logging.basicConfig()
 dawn()
# Hue hub IP address
 b = Bridge('yourhubIPaddress')
 # which hue lights am I setting
 lght = [2, 3, 1]
 wait_time = 20 * 60
 # Setup light brightness, saturation and hues. There are four stages in this
 # 'sunrise'.
 # set starting and ending brightness at each stage--need to multiply by 254
 brt = [[0, 0.25], [0.25, 0.75], [0.75, 1.0], [1.0, 1.0]]
 # set time in minutes for the 4 stages in minutes
 tim = [14, 6, 4, 12]
 # 4 stages 1.) blue 2.) blue to red 3.) red to yellow 4.) yellow to white
 # followed by a wait period in white
 hues = [[46647, 46647], [46647, 62914], [0, 10924], [10924, 15958]]
 # starting and ending saturations at each stage--need to multiply by 254
 sts = [[0.8, 0.85], [0.85, 1], [1, .75], [.75, .34]]
 # execute 4 stages
 # print len(hues[0:])
 # lights are moved linearly from the state at the start of the stage
 # to the state at the end of the stage changing every 5 seconds.
 for h in range(len(hues[0:])):
 # convert time in minutes to number of 5 second intervals
 pas = int(12 * tim[h] + 0.25)
 #    print h, pas
 for c in range(0, pas):
 # current saturation
 st = int((((sts[h][1] - sts[h][0]) * 254 * (c + 1) / pas) + 254 *
 sts[h][0]))
 # current brightness
 bt = int((((brt[h][1] - brt[h][0]) * 254 * (c + 1) / pas) + 254 *
 brt[h][0]))
 # current hue
 hu = int(((hues[h][1] - hues[h][0]) * (c + 1) / pas) + hues[h][0])
 # setup command to send to phue
 comd = {'transitiontime': 0, 'on': True,
 'sat': st, 'bri': bt, 'hue': hu}
 #        print c, st, bt, hu
 try:
 # send the hue, saturation and brightness to each light
 b.set_light(lght[:], comd)
 except:
 # Allow for more or less graceful failure
 pass
 if c == 0:
 time.sleep(1)
 time.sleep(5)
 # leave lights on at last state for wait_time minutes
 time.sleep(wait_time)
 # turn the lights off
 b.set_light(lght[:], 'on', False)

In Ubuntu I’m using cron to start my program each morning before dawn as a normal user. My program resides in my home directory and is called sunrise1.py. To do this open a terminal and enter:

crontab -e

The following entry runs the program at 5:00AM each morning:

00 05 * * * ~/sunrise1.py

Just make sure you schedule it before the earliest dawn (which is about a half hour before sunrise) in the year. It will wait to begin the simulation.

 

 

Finally the small python program to help figure out the brightness, saturation, and hue settings needed to produce the colors you want. I used this program to get the color codes for the beginning and end of each stage in the sunrise program. To use it, I set the lights to the color I wanted from my Hue phone app. Then I ran the program to get the correct settings.

#!/usr/bin/python
from phue import Bridge

b = Bridge('yourhubIPaddress') 
print b.get_light(1, 'on') 
print b.get_light(1, 'bri') 
print b.get_light(1, 'sat') 
print b.get_light(1, 'hue')

 

%d bloggers like this: