I'll be using Linux in this project because I have a Mac. Walabot does not make an SDK for Mac, so I decided to run an Unbuntu VM. This code will work with Windows, but the installation instructions will be slightly different.
Step 1: Install the Walabot SDKDownload and install the Walabot API for Linux. This contains the Walabot library and some example code to help us get started.
Step 2: Install libcurlUse the following Terminal (Ctrl + Alt + T) command to install the libcurl library. We will use libcurl to make our HTTP requests to the Philips Hue bridge.
$ sudo apt-get install libcurl4-gnutls-dev
Step 3: Download CodeDownload the code from the code section below.
If you're wondering where it came from, I simply modified Walabot's breathing example code found here: /usr/share/doc/walabot/examples/cpp/SensorBreathingSampleCode.cpp
.
Make sure your computer is on the same network as your Hue bridge, and visit the url: https://www.meethue.com/api/nupnp. This will return JSON-formatted data with an internalipaddress
property. This is the IP address of the Hue bridge. Save this IP address.
Now, using a web browser, visit http://<bridge
ip
address>/debug/clip.html
. Fill in the text fields with the following information, and click the POST button. You will receive an error about the link button not being pressed. That's your cue to walk over to your Hue bridge and press the button. Then, without changing anything, press POST again. If you pressed the link button in time, you should get a success message in the Command Response field, along with a username. Save this username.
Note: The string "walabot" after "devicetype" is arbitrary and is only used internally by the bridge to associate a randomly-generated username with a human-readable description.
We will need to customize the URL to which our computer will send the requests.
The Hue API is easy to understand. The entire system is based on a URL structure. Every object (such as a light or a room) has a specific URL. We can get information on those objects by making a GET request to that URL, or we can update information on that object, such as changing a light's "on" state to true, by making a PUT request.
The base URL looks like this. It contains the IP address and username we obtained in the previous step.
We will append all our requests to the base URL to form one like this: http://10.0.0.1/api/OOeV6cZg0zhTu4wLriaTSYMU1VfnttWYzwlZtiPx/groups/1/action
. With the proper request body, a PUT request made to this URL will be able to control all the lights in a room. The body we will be using look something like this, except transitiontime
will be dynamically calculated:
{
"on": true,
"Bri": 254,
"transitiontime": 7
}
Step 5: Configure Code for Your LightsUsing a text editor such as Atom, open the code we downloaded earlier. On line 44
, replace the IP address and username with your IP address and username.
Go back to http://<bridge
ip
address>/debug/clip.html
. Set the URL box to /api/<your username>/groups and click the GET button. It will return a list of all the groups (rooms) in your house. Read the description of each one until you find the group you would like to control. Save the index of that group ("1", "2", "3', ...).
Go back to line 44
in the code, and change the number after groups/
to the index of the group you just found.
Plug in your Walabot! We are now ready to compile our code and test it out. Open up Terminal and navigate to the directory where the code is located. Don't forget to save your code before this step.
$ cd ~/Desktop
Run the following command to compile the .cpp file into a .o file.
$ g++ -o HueBreathing.out HueBreathing.cpp -O2 -D__LINUX__ -lWalabotAPI -lcurl
Now, run the program with the following command.
$ ./HueBreathing.out
Position the Walabot so that it is facing your chest, and begin to breathe. Your lights should fade on when you inhale and fade off when you exhale.
Comments