-->
Page 1 of 1

Controling Philips Hue

PostPosted: Thu Aug 06, 2015 1:33 am
by Douglas C. Santos
Simple code to turn on/off Philips Hue. I am planning to build a cheap light switch.

Code:
--------------------------------------
/*
* This sketch sends data via HTTP PUT requests to a Hue Bridge.
*
*
*/

#include <ESP8266WiFi.h>

// Wifi Settings

const char* ssid = "Your_Wifi";
const char* password = "Wifi_password";

// Hue Settings

const char* bridge_ip = "192.168.0.88"; // Hue Bridge IP
const int port = 80;
String user="newdeveloper"; // Hue Bridge user -To create a user check this page (http://support.intuilab.com/kb/connecte ... hue-lights)
String light="3";

// Commands

String hue_on="{\"on\":true}";
String hue_off="{\"on\":false}";


void setup() {
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

hue_control();

}

void loop() {


}

void hue_control() {
Serial.print("Connecting to ");
Serial.println(bridge_ip);

// Use WiFiClient class to create TCP connections
WiFiClient client;

if (!client.connect(bridge_ip, port)) {
Serial.println("Connection failed");
return;
}

// This will send the request to the server
client.println("PUT /api/" + user + "/lights/" + light + "/state");
client.println("Host: " + String(bridge_ip) + ":" + String(port));
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println("Content-type: text/xml; charset=\"utf-8\"");
client.print("Content-Length: ");
client.println(hue_on.length()); // PUT COMMAND HERE
client.println();
client.println(hue_on); // PUT COMMAND HERE

delay(10);

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Closing connection");
}

Re: Controling Philips Hue

PostPosted: Sat Aug 08, 2015 6:34 am
by Tolipwen
Douglas C. Santos wrote:Simple code to turn on/off Philips Hue. I am planning to build a cheap light switch.

Code:
--------------------------------------
/*
* This sketch sends data via HTTP PUT requests to a Hue Bridge.
*
*
*/

#include <ESP8266WiFi.h>

// Wifi Settings

const char* ssid = "Your_Wifi";
const char* password = "Wifi_password";

// Hue Settings

const char* bridge_ip = "192.168.0.88"; // Hue Bridge IP
const int port = 80;
String user="newdeveloper"; // Hue Bridge user -To create a user check this page (http://support.intuilab.com/kb/connecte ... hue-lights)
String light="3";

// Commands

String hue_on="{\"on\":true}";
String hue_off="{\"on\":false}";


void setup() {
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

hue_control();

}

void loop() {


}

void hue_control() {
Serial.print("Connecting to ");
Serial.println(bridge_ip);

// Use WiFiClient class to create TCP connections
WiFiClient client;

if (!client.connect(bridge_ip, port)) {
Serial.println("Connection failed");
return;
}

// This will send the request to the server
client.println("PUT /api/" + user + "/lights/" + light + "/state");
client.println("Host: " + String(bridge_ip) + ":" + String(port));
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println("Content-type: text/xml; charset=\"utf-8\"");
client.print("Content-Length: ");
client.println(hue_on.length()); // PUT COMMAND HERE
client.println();
client.println(hue_on); // PUT COMMAND HERE

delay(10);

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Closing connection");
}


Hi Douglas,

I'm about to build a switch for my Hue-system as well. I will be using a PIR (HC-SR501) as a trigger to automatically turn the light on once movement is detected and turn them off after a period of time where no movement is detected by the PIR. Sadly my ordered hardware still hasn't arrived yet.
How are you planning on triggering the switch?

Re: Controling Philips Hue

PostPosted: Sat Aug 08, 2015 3:30 pm
by Mikejstb
That's nice - good work!
I took the easy way out - I guess consistent is a better word. I try to keep my ESP sensors simple and just report to a mqtt broker.I use Node Red to listen to the sensors and control the Hue lights,etc based on time of day, PIR sensors, etc.
After the initial setup then adding new things like Hue is easy...

I like the Hue system a lot, the GE bulbs are a lot less expensive than the Philipps and seem to work fine.
Plus the Hue works well with my Amazon Echo. I have the Echo developers kit and have meant to try to integrate it into my NodeRed scheme but so far haven't done that.
Being able to ask "Alexa" to display the weather on my mqtt matrix display would really be cool!

BTW - I've found the Hue light system to not be at all "wife-proof". I try to set things up so say the living room light will slowly come up to a dim level if there is movement detected toward the living room at night, etc. and 9 times out of 10 the light doesn't come on because my better half has turned the switch off. Oh well...

Re: Controling Philips Hue

PostPosted: Sun Aug 09, 2015 5:32 am
by Tolipwen
Mikejstb wrote:That's nice - good work!
I took the easy way out - I guess consistent is a better word. I try to keep my ESP sensors simple and just report to a mqtt broker.I use Node Red to listen to the sensors and control the Hue lights,etc based on time of day, PIR sensors, etc.
After the initial setup then adding new things like Hue is easy...

I like the Hue system a lot, the GE bulbs are a lot less expensive than the Philipps and seem to work fine.
Plus the Hue works well with my Amazon Echo. I have the Echo developers kit and have meant to try to integrate it into my NodeRed scheme but so far haven't done that.
Being able to ask "Alexa" to display the weather on my mqtt matrix display would really be cool!

BTW - I've found the Hue light system to not be at all "wife-proof". I try to set things up so say the living room light will slowly come up to a dim level if there is movement detected toward the living room at night, etc. and 9 times out of 10 the light doesn't come on because my better half has turned the switch off. Oh well...


Just had a quick look at Node Red. Seems interesting as the light to switch on/off wouldn't have to be hardcoded in the ESP.
Would you mind sharing your firmware/code used on the ESP? And are you ESPs battery-powered?