Controling Philips Hue
Posted: Thu Aug 06, 2015 1:33 am
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");
}
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");
}