I presented a Shell script that can control Philips Hue light bulbs with cURL before. This post is to do the same with C++ code. This post assumes that you already created a user account in the Hue bridge; you can find more detail on Hue web page.
This program will use cURL library for C++ and c++11 compiler, which is gcc-4.7 in this example.
I made four files:
One of them, "hue.cpp", has the main logic:
It has the MAC address of the Hue Bridge device and it look for the IP of the MAC address. It is because Hue bridge device relies on DHCP and the IP might be changed; unless you dedicated a static IP address for the MAC on the router. But it also has a default IP address in case the MAC address is not found from ARP cache table; or whatever reason. This MAC address resolving part is totally optional if you are fine with just a fixed IP address.
The program check the command arguments. If it doesn't have any command arguments, it prints out how to use it. It expects to have a light bulb number. It will return the status of the light bulb. When additional two more arguments are given, they will be used to change the status of the light bulb; first one will be used as a key and the second will be used as a value. Unlike the script version I presented, this doesn't handle error cases like when you haven't created user name; BTW, this example assumes that the user name on the Hue Bridge is "newdeveloper".
The Makefile will look like this:
A precompile header file, "stdafx.h", is like this:
The last file, "my_curl.hpp", looks like this:
This big class is little different from the previously presented version. It is because now it has to handle HTTP PUT method; that's what Hue Bridge requires.
Once you compile it and run it, you will see something like this result.
Enjoy the C++ version of Hue controller.