Friday, December 30, 2022

Playing around with Philips Hue lights from the command line...

We own a bunch of Philips Hue light bulbs now, and we just bought some as a gift for one of our kids. You can use the Philips Hue app on your computer and/or mobile device, but it's also a lot of fun to play with the lights (and other Philips Hue devices) from a terminal or an app that you can write for yourself using the Philips Hue API. 

Register to get access to the Developer's API documentation

You can find the Philips Hue Developer documentation here: https://developers.meethue.com/

The Developer page has a login link. Click the Login link, and then select the Register option at the bottom of the Login form. Or, use this link to get to the Register page.


Find your Hue bridge IP address

Follow the instructions listed here to find the IP address of your Hue bridge.

The easiest method for finding your Hue bridge IP address is to make sure you're connected to the same network that the Hue bridge is connected to and then visit this address: https://discovery.meethue.com/

The response will look something like this:

[
  {
    "id": "112233aabb44cc55",
    "internalipaddress": "10.0.0.1",
    "port": 443
  }
]


Use curl to send HTTP requests

You can use curl to send requests to the Hue bridge.

If you're new to using curl, then watch this short video about curl for sending HTTP requests to a server. In our case, the server will be the Philips Hue Hub using its IP address. We'll pretend that the IP address of the Hue bridge is 10.0.0.1 for the examples.


Go to the Hue API docs

After registering/logging in on the Philips Hue Developer page select the Develop menu and then select the Hue API menu item. 


Create a username

The Hue API requires a username. The username is created and saved by your Philips Hue bridge, and the hub adds the username to a list of usernames that are allowed to send API calls to the Hue bridge.

Follow the instructions here to create a username.


Use curl to get light info from the bridge

  1. Open a terminal
  2. Set a variable in your terminal that will hold the Hue Bridge IP address you retrieved above. For example, if your Hue bridge IP address is 10.0.0.1, then you would enter the following:

    HUE_BRIDGE=10.0.0.1

  3. Set a variable in your terminal that will hold Hue username you retrieved above. For example, if your assigned username is nE1wnTaC00ki3, then you would enter:

    HUE_USERNAME=nE1wnTaC00ki3

  4. Send a curl command to your Hue bridge to get info about all of the lights you currently have associated with your bridge:

    curl -XGET http://$HUE_BRIDGE/api/$HUE_USERNAME/lights

You should get a response that is a JSON string containing information about all of your Hue lights.