HomeGenie Forum

Automation Program Plugins and Wizard Scripting => APP Contributions => Topic started by: mvdarend on October 25, 2014, 01:12:08 PM

Title: Philips Hue and Ambilight
Post by: mvdarend on October 25, 2014, 01:12:08 PM
Are there any users with Philips Hue Lighting and an Ambilight television that would be interested in a HomeGenie App for syncing the lights with the TV?

I'm thinking of trying to make one, but I'll probably never use it myself (apart from testing)
Title: Re: Philips Hue and Ambilight
Post by: mvdarend on October 26, 2014, 09:26:35 AM
I decided to try it anyway, but I'm running into the following problem. The JointSpace API gives the following json reply:

Code: [Select]
{
"layer1": {
"left": {
"0": {
"r": 254,
"g": 96,
"b": 0
},
"1": {
"r": 255,
"g": 192,
"b": 114
},
"2": {
"r": 254,
"g": 205,
"b": 113
},
"3": {
"r": 132,
"g": 255,
"b": 64
}
},
"top": {

},
"right": {
"0": {
"r": 167,
"g": 255,
"b": 42
},
"1": {
"r": 137,
"g": 255,
"b": 32
},
"2": {
"r": 65,
"g": 254,
"b": 0
},
"3": {
"r": 254,
"g": 120,
"b": 0
}
},
"bottom": {

}
}
}

And I'm trying to retrieve the data as follows:

Code: [Select]
   
var ambilightData = Net.WebService(webserviceurl).GetData();
   
string leftR = ambilightData.layer1.left.0.r.ToString();

HomeGenie won't allow the integer here (won't compile), so I tried to work with indexes:

Code: [Select]
   
string leftR = ambilightData.layer1.left[0].r.ToString();

This compiles, but results in the runtime error "Accessed JObject values with invalid key value: 0. Object property name expected."

Anyone know what I'm doing wrong?



Edit: Found the problem, by querying the data as follows it works

Code: [Select]
string leftR = ambilightData["layer1"]["left"]["0"]["r"];
string leftG = ambilightData["layer1"]["left"]["0"]["g"];
string leftB = ambilightData["layer1"]["left"]["0"]["b"];
Title: Re: Philips Hue and Ambilight
Post by: mvdarend on October 28, 2014, 10:40:21 AM
OK, I've managed to get it to control my Fibaro RGBW LED strip, but I'm having trouble finding an elegant way to convert RGB to HSB for the Hue Lights in C#.

It can be done via the System.Drawing namespace, but that's not included in HomeGenie at the moment. Does anyone know another way?
Title: Re: Philips Hue and Ambilight
Post by: dani on October 28, 2014, 07:09:49 PM
For Fibaro RGBW, you have the module Fibaro RGBW that I wrote. Then you can look at the source code of the fictive Node, to know how to code RGB. We don't need HSB value.

Cheers
Dani
Title: Re: Philips Hue and Ambilight
Post by: mvdarend on October 28, 2014, 07:25:00 PM
Hi Dani, Thanks for your reply.

Unfortunately, the Philips Hue lights expect HSB values. Controlling your Fibaro RGBW module was the easy part, as I didn't have to convert anything :)
Title: Re: Philips Hue and Ambilight
Post by: dani on October 29, 2014, 08:09:50 PM
Yes, that's right.

Cheers
Dani
Title: Re: Philips Hue and Ambilight
Post by: dani on October 29, 2014, 08:10:43 PM
Yes, that's right.

Cheers
Dani
Title: Re: Philips Hue and Ambilight
Post by: mvdarend on October 30, 2014, 09:14:58 PM
Philips Hue also accepts an XY value, after a bit of searching I found a way to convert RGB to XY here:
http://stackoverflow.com/questions/22564187/rgb-to-philips-hue-hsb (http://stackoverflow.com/questions/22564187/rgb-to-philips-hue-hsb)

Using that code, and the following added to the Philips Hue Bridge App. I managed to get it working (sort of)
Code: [Select]
       
case "Control.ColorXY":
    bridgeapicall(lightnumber, "{ "on" : true" +
              ", "xy" : [" + parameter.ToString() + "], "transitiontime" : 3}");
        Program.RaiseEvent(module, "Status.ColorXY", parameter.ToString(), "Hue Light");
        break;

Unfortunately the colours don't match half the time which basically makes it useless :)

Back to the drawing board....
Title: Re: Philips Hue and Ambilight
Post by: RogerCox on January 26, 2015, 05:13:09 PM
I use Hambisync to synchronize my hue lights and my ambilight tv from my windows mediacenter. Since it's written in java it could perhaps be integrated or called from Homegenie?

I would be very interested if it could!
Title: Re: Philips Hue and Ambilight
Post by: mvdarend on January 27, 2015, 08:27:48 AM
HI Roger,

I use Hambisync too, which is how I got the idea to try it for HomeGenie.

The test code I have works fine with reading the values from the television and then controlling, say, an RGB lamp (I tested it with a RGB led strip)

But I'm having trouble converting the RGB values from the TV to HSB or XY values for the Philips Hue lights. Maybe I'm missing something really simple, but as no-one showed any interest in getting this to work I haven't really put much effort into it.

Title: Re: Philips Hue and Ambilight
Post by: Gene on January 27, 2015, 08:39:03 AM
Hi mvdarend,

try this:

http://theembeddedsystems.blogspot.it/2011/08/rgb-to-hsv-method-in-c.html (http://theembeddedsystems.blogspot.it/2011/08/rgb-to-hsv-method-in-c.html)

Cheers,
g.
Title: Re: Philips Hue and Ambilight
Post by: mvdarend on January 27, 2015, 09:23:01 AM
Thanks Gene, unfortunately I cannot find the correct namespaces for Rgb and Hsv.

I've found a lot of good examples implementing the System.Drawing.Color class, which I can get to work in a simple Visual Studio Test project, but I can't get them to work in Mono. (I'm assuming it's a Mono thing, please correct me if I'm wrong.)

Title: Re: Philips Hue and Ambilight
Post by: Gene on January 27, 2015, 10:00:47 AM
System.Drawing works with mono, but it's not allowed in HG by design.
You can still use code from the link by just adapting it.
Rgb and Hsv are most likely struct/classes, so what you will have to do is replacing them with standard variables.
Eg. Rgb.Green will just become rgbGreen in your code, and so on.

g.
Title: Re: Philips Hue and Ambilight
Post by: RogerCox on January 27, 2015, 09:21:23 PM
I for one think it would be great if this function could be fully integrated into HG and I'm very much interested!

But before this is a reality, is it possible to simply run the hambisync.jar file from HG? It runs fine on my bananapro when I start it manually, I just need a way to start and stop it from within HG.

Or is this a very newbie thing to ask?


Title: Re: Philips Hue and Ambilight
Post by: mvdarend on January 28, 2015, 08:08:14 AM
You can still use code from the link by just adapting it.
Rgb and Hsv are most likely struct/classes, so what you will have to do is replacing them with standard variables.
Eg. Rgb.Green will just become rgbGreen in your code, and so on.
You're right, I was so fixed on trying to find the correct namespaces each time, that I completely forgot to see if the code was easily modifiable.

I'll give it a shot, thanks.

Quote
But before this is a reality, is it possible to simply run the hambisync.jar file from HG? It runs fine on my bananapro when I start it manually, I just need a way to start and stop it from within HG.
I think this is possible, but I'm not sure how to go about it.
Title: Re: Philips Hue and Ambilight
Post by: saue0 on August 18, 2015, 06:54:58 PM
Here my code for RGB to HSV
case "Control.Color":
     
        double r = int.Parse(values[0]) / 255;
        double g = int.Parse(values[1]) / 255;
        double b = int.Parse(values[2]) / 255;
       
        double min = Math.Min( r, Math.Min( g, b ));
        double max = Math.Max( r, Math.Max( g, b ));
        double v = max;            // v
        double s = 0;            // s
        double h = 0;            // h
        double delta = max - min;
        if( max != 0 )
        {
            s = delta / max;      // s

            if( r == max )
                h = ( g - b ) / delta;      // between yellow & magenta
            else if( g == max )
                h = 2 + ( b - r ) / delta;   // between cyan & yellow
            else
                h = 4 + ( r - g ) / delta;   // between magenta & cyan
           
            h *= 60;            // degrees
            if( h < 0 )
                h += 360;       
        }
     
        if (values.Length > 3)
            transition = ((int)(double.Parse(values[3]) * 10));

        bridgeapicall(grp, lightnumber, "{ \"on\" : true" +
                      ", \"hue\" : " + ((int)Math.Round(h * 182.044)).ToString() +
                      ", \"sat\" : " + ((int)Math.Round(s * 255)).ToString() +
                      ", \"bri\" : " + ((int)Math.Round(v * 255)).ToString() +
                      ", \"transitiontime\" : " + transition.ToString() +  "}");
        break;
Title: Re: Philips Hue and Ambilight
Post by: mvdarend on August 22, 2015, 09:36:33 AM
Thanks for that saue0, unfortunately I've given up on this project though. My wife *hates* the Hues as extra Ambilights so I've never really put more effort into it.

I can't seem to find my test code either, although it wasn't too hard to get the RGB info from the TV. If anyone is interested you can find some examples for reading the data in my previous posts.