I am going to present three more plugins for the previous "cron_native" program.
1. Turn off Hue light bulb as soon as possible.
2. Turn off Hue light bulb an hour after it is turned on.
3. Turn off Hue light bulb 10 minutes after a TV is turned on.
Controlling Hue light bulb is already explained before; [link to more info]. And I posted how to detect whether TV is turned on/off already; [link to more info]. This article is to show the actual working example of the plugins for "cron_native".
I have six files for this:
The file, "plugin.hello.cpp", is not a part of this article but for the previous one.
The first program to show is "plugin.hue_bedroom.cpp", which turns off the light as soon as possible.
This program assumes the light index is "1" and the user account for the Hue Bridge is "newdeveloper". The IP address of my Hue Bridge happens to be "192.168.0.50" for now but it is dynamically allocated from the router so it may change anytime. The actual IP address is searched from ARP cache table. I once noticed that there was a case where Hue Bridge doesn't appear on
the ARP cache table. I am not sure how the network protocol works but it
seems that when the device is not active for long period of time, other
devices may not award of the existence of the device in the network. That's why it starts with the fix IP address and update it with ARP cache table.
Since the goal of the program is to turn off the light as soon as possible, it doesn't need to check to see if the light is turned on or not. It will work either way.
I wanted to have the program running every 10 seconds; not every seconds. Each HTTP request is not much cost in terms of CPU and network bandwidth. I may add up and become big when I have many other plugins running together.
The second program, "plugin.hue_outdoor.cpp", is to turn off the hue light only if it is turned on for an hour. As the file name implies, this is to turn off the out door light automatically in case my wife forgot to turn it off.
The program runs every minute and see if the light#4 is turned on or not. If it is turned on, the another timer starts to see how long the light was turned on. When the light is turned on more than an hour, it leaves a syslog message and turns the light off.
The last program, "plugin.hue_tv_room.cpp", is to turn off the light when TV is turned on. But it turns off only once, meaning TV is turned off, the light will be turned off but when I turn on the light manually while the TV is turned on, the light wouldn't be turned off again. I could also have it turn on the light back when TV is turned off, but I didn't find it useful because when I turn off the TV, most likely I was leaving the room.
My Samsung TV doesn't actually respond with HTTP protocol. It seems to have its own kind of protocol. So technically I am supposed to open a Socket connection to the port and see it the port is accessible or not. But that would require more low level Socket programming code, which isn't too hard with Boost library; [link to more detail]. I found that I can work around it with cURL as it is shown in the source code.
Makefile looks like this:
As I mentioned before, those plugin programs should be Position Independent Code, or PIC. The compiler must have the option of "-fPIC".
The file, "stdafx.h", looks like this:
The file, "my_curl.hpp", was always posted on the previous article but I am posting it again with minor changes:
That is it.