Hacking the Hue with Python

I like good lighting. So I just got the Phillips Hue lights for my house to “set the mood”. In reality its just so I can turn off my lights without getting out of bed… I also like to code, so I decided to write this quick blog post as an introduction to hacking and controlling the Hue lights with python.

How Hue works?

All the Hue lights use wifi to attach to the Hue bridge. This is the small white box you plug into your router. When you do this you set up a specific IP address to access all your Hue toys. Mine is 167.122.3.122 (jokes, don’t tell the internet this). Once we have this special unique address we can use a REST api to send commands over the interwebs.

How to REST api with python

To send these commands python makes it really easy with the library requests. Just pip install it and lets try it out.

All we really need to know for this project is sending a GET, POST, and PUSH request. Check it out

>>> r = request.get('http://someaddress/test')
>>> print(r.text)
  {
    ...
    "form": {
      "key1": "value1"
      "key2": "value2"
     },
     ...
  }

All this does is say give me the info at this endpoint. This is a GET request. If we want to do a PUT or POST we need to just change to request.put.

Setting up a user

Since it would really be a pain if anybody on the internet could turn on and off your lights Hue makes it so you must be an aproved user to controll the lights. The way it does this is by requesting, pressing the physical button on the Bridge, and requesting again. Doing this with python is easy.

We request a new user with a POST command. In this command we will send data saying what our ‘app’ is called and what we want our user to be. This is done in python with

payload = json.dumps({'devicetype': 'my-hue-app#hack-the-planet sean'})
r = requests.post("http:///api", data=payload)
print(r.text)

Nice! Now we got a response back asking us to press the bridge button. Once we do that we can run this code again to get a respose that gives us our username key. This looks something like ‘8S40hGQNdslKdW57xipFwFJsiPhtfscmCvInbPwz’ and we will want to save this because we will use this for every request to authenticate.

What can we do?

Now all the hard work is done and we can hack the planet. The first thing we want to do is get the number of lights, where they are, and ll other sorts of info. To to this the url we use is the same base as before plus the command. Now my script looks like:

import requests

KEY = '8S40hGQcd1lKdj57xipFwFJsiPhhkscmCvInbPwz'
BRIDGE_IP = '172.158.1.192'
BASE = 'http://{}/api/{}/'.format(BRIDGE_IP, KEY)

# get the config
r = requests.get(BASE + 'lights/config')
print(r.text)

Cool. Thats a lot of settings. All I want to do is turn on my lamp so I see it is light number one and I send another request.

# turn the lights on
payload = json.dumps({'on':True})
r = requests.put(BASE + 'lights/1/state', data=payload)

And then there was light! This example shows just how easy it is. I looked at the state of light 1 by sending ‘lights/1/state’. This is just a json file with everything I can see and do with the light. There was a field for ‘on’, so I just sent a JSON object saying {'on': True}.

Here’s another just changing the color

payload = json.dumps({"on":True, "sat":254, "bri":254,"hue":10000})
r = requests.put(BASE + 'lights/1/state', data=payload)

From here we have enough to understand how every command works. Its just adding the right string to the end of our base url. These are all the commands you can try. For more help just check out Hues website, https://www.developers.meethue.com/

  • /lights
  • /groups
  • /config
  • /schedules
  • /scenes
  • /sensors
  • /rules

Happy hacking! Now i’m going to build a script that plays music and a light show every time I enter my room.

Hi, my name is Sean

Well the title pretty much says it all…

But in case you missed it my name is Sean and I like machine learning, math, coding, and long walks on the beach. I am a graduate student in computer science and this is my attempt at a blog. Hopefully it helps and if you have any request for topics let me know!

❤️❤️❤️

-Sean