├── README.adoc ├── examples ├── LightToggle │ └── LightToggle.ino └── RandomColor │ └── RandomColor.ino ├── library.properties └── src ├── ESPHue.cpp └── ESPHue.h /README.adoc: -------------------------------------------------------------------------------- 1 | = Philips Hue Library for ESP8266 = 2 | 3 | This library is designed to access and control Philips Hue Lights Directly with an ESP8266 using the Arduino IDE. 4 | 5 | To use: 6 | 7 | 1. Download ZIP 8 | 9 | 2. In Arduino IDE from the menu Sketch->Include Library->Add .ZIP Library... 10 | 11 | 3. Browse to the .zip file and install 12 | 13 | 4. Library will show up under Include Library 14 | 15 | 5. Examples are in File->Examples->ESP_Hue 16 | 17 | Available Class Methods: 18 | 19 | void setAPIKey(const char* APIKey); 20 | 21 | void setHubIP(const char* host); 22 | 23 | void setHubPort(uint8_t port); 24 | 25 | String getLightInfo(int lightNum); // Gets the light raw http request data 26 | 27 | int getLightState(int lightNum); // Gets Light State On, or Off 28 | 29 | void setLight(int lightNum, int state, int sat, int bri, int hue); // Set Light State, Saturation, Brightness, and Hue 30 | 31 | void setLight(int lightNum, int state, int sat, int bri, int hue, unsigned int trans); // Set Light State, Saturation, Brightness, and Hue, with transition speed 32 | 33 | void setLightTemperature(int lightNum, int state, int bri, unsigned int ct); // Set Light State, Brightness, and Light Temperature 34 | 35 | void setLightTemperature(int lightNum, int state, int bri, unsigned int ct, unsigned int trans); // Set Light State, Brightness, and Light Temperature, with transition speed 36 | 37 | void setLightPower(int lightNum, int state); // Set Light Power On, or Off 38 | 39 | String getGroupInfo(int groupNum); // Gets the group raw http request data 40 | 41 | int getGroupState(int groupNum); // Gets Group State On, or Off 42 | 43 | void setGroup(int groupNum, int state, int sat, int bri, int hue); // Set Group State, Saturation, Brightness, and Hue 44 | 45 | void setGroup(int groupNum, int state, int sat, int bri, int hue, unsigned int trans); // Set Group State, Saturation, Brightness, and Hue, with transition speed 46 | 47 | void setGroupTemperature(int groupNum, int state, int bri, unsigned int ct); // Set Group State, Brightness, and Light Temperature 48 | 49 | void setGroupTemperature(int groupNum, int state, int bri, unsigned int ct, unsigned int trans); // Set Group State, Brightness, and Light Temperature, with transition speed 50 | 51 | void setGroupPower(int groupNum, int state); // Set Group Power On, or Off 52 | 53 | See Examples for Usage 54 | 55 | 56 | ------------------------------------------------------------------------------------ 57 | == License == 58 | 59 | Copyright (c) Richard Wardlow. All right reserved. 60 | 61 | This library is free software; you can redistribute it and/or 62 | modify it under the terms of the GNU Lesser General Public 63 | License as published by the Free Software Foundation; either 64 | version 2.1 of the License, or (at your option) any later version. 65 | 66 | This library is distributed in the hope that it will be useful, 67 | but WITHOUT ANY WARRANTY; without even the implied warranty of 68 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 69 | Lesser General Public License for more details. 70 | 71 | You should have received a copy of the GNU Lesser General Public 72 | License along with this library; if not, write to the Free Software 73 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 74 | -------------------------------------------------------------------------------- /examples/LightToggle/LightToggle.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const char* ssid = "Your_SSID_Here"; 6 | const char* password = "Your_PASS_Here"; 7 | 8 | WiFiClient client; 9 | int Button1 = 12; 10 | 11 | ESPHue myHue = ESPHue(client, "Your_API_Key_Here", "Your_Hue_Bridge_IP_Here", 80); 12 | 13 | void setup() { 14 | pinMode(Button1, INPUT_PULLUP); 15 | Serial.begin(115200); 16 | Serial.println(); 17 | Serial.print("Connecting to "); 18 | Serial.println(ssid); 19 | 20 | WiFi.begin(ssid, password); 21 | 22 | while (WiFi.status() != WL_CONNECTED) { 23 | delay(500); 24 | Serial.print("."); 25 | } 26 | 27 | Serial.println(""); 28 | Serial.println("WiFi connected"); 29 | Serial.println("IP address: "); 30 | Serial.println(WiFi.localIP()); 31 | } 32 | 33 | void loop() { 34 | 35 | if(digitalRead(Button1) == 0) // If Button1 is pressed 36 | { 37 | Serial.println("Button1 Pressed"); 38 | if (myHue.getLightState(3) == 1) // If Light 3 is on 39 | { 40 | Serial.println("Hue Light 3 is Currently On, Turning Light Off"); 41 | myHue.setLightPower(3, myHue.OFF); 42 | } 43 | else // Light 3 is off 44 | { 45 | Serial.println("Hue Light 3 is Currently Off, Turning Light On"); 46 | myHue.setLightPower(3, myHue.ON); 47 | } 48 | } 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /examples/RandomColor/RandomColor.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const char* ssid = "Your_SSID_Here"; 6 | const char* password = "Your_PASS_Here"; 7 | 8 | WiFiClient client; 9 | int Button1 = 12; 10 | 11 | ESPHue myHue = ESPHue(client, "Your_API_Key_Here", "Your_Hue_Bridge_IP_Here", 80); 12 | 13 | void setup() { 14 | pinMode(Button1, INPUT_PULLUP); 15 | randomSeed(analogRead(A0)); 16 | Serial.begin(115200); 17 | Serial.println(); 18 | Serial.print("Connecting to "); 19 | Serial.println(ssid); 20 | 21 | WiFi.begin(ssid, password); 22 | 23 | while (WiFi.status() != WL_CONNECTED) { 24 | delay(500); 25 | Serial.print("."); 26 | } 27 | 28 | Serial.println(""); 29 | Serial.println("WiFi connected"); 30 | Serial.println("IP address: "); 31 | Serial.println(WiFi.localIP()); 32 | } 33 | 34 | void loop() { 35 | 36 | if(digitalRead(Button1) == 0) // If Button1 is pressed 37 | { 38 | Serial.println("Button1 Pressed"); 39 | int rndNum = random(267, 65534); 40 | myHue.setGroup(0, myHue.ON, 255, 255, rndNum); // Set group 0 to random color (group 0 is all lights by default) 41 | delay(100); 42 | } 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ESP_Hue 2 | version=1.0 3 | author=Richard Wardlow 4 | maintainer=Richard Wardlow 5 | sentence=Enables easy control of Philips Hue Lights directly from an ESP8266 6 | paragraph= 7 | category=Communication 8 | url= 9 | architectures=esp8266 10 | -------------------------------------------------------------------------------- /src/ESPHue.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | = Philips Hue Library for ESP8266 = 3 | 4 | This library is designed to access and control Philips Hue Lights Directly. 5 | 6 | Written by: Richard Wardlow @ Circuits for Fun, LLC 7 | GNU GPL, include above text in redistribution 8 | ***************************************************************************/ 9 | 10 | #include 11 | #include 12 | 13 | //////////////////////////////////////// 14 | // ESPHue Class Methods 15 | //////////////////////////////////////// 16 | ESPHue::ESPHue(WiFiClient& client, const char* APIKey, const char* host, uint8_t port) 17 | { 18 | _client = &client; 19 | _apiKey = APIKey; 20 | _host = host; 21 | _port = port; 22 | } 23 | 24 | void ESPHue::setAPIKey(const char* APIKey) 25 | { 26 | _apiKey = APIKey; 27 | } 28 | 29 | void ESPHue::setHubIP(const char* host) 30 | { 31 | _host = host; 32 | } 33 | 34 | void ESPHue::setHubPort(uint8_t port) 35 | { 36 | _port = port; 37 | } 38 | 39 | String ESPHue::getLightInfo(byte lightNum) 40 | { 41 | if (!_client->connect(_host, _port)) { 42 | Serial.println("connection failed"); 43 | return ""; 44 | } 45 | String url = "/api/" + String(_apiKey) + "/lights/" + lightNum; 46 | _client->print("GET " + url + " HTTP/1.1\r\n" + 47 | "Host: " + _host + "\r\n" + 48 | "Connection: keep-alive\r\n\r\n"); 49 | String line; 50 | delay(200); 51 | while(_client->available()){ 52 | line = _client->readString(); 53 | } 54 | return line; 55 | } 56 | 57 | int ESPHue::getLightState(byte lightNum) 58 | { 59 | int lightState = 0; 60 | String response = getLightInfo(lightNum); 61 | 62 | if (response.indexOf("\"on\":false") != -1) 63 | { 64 | lightState = 0; 65 | } 66 | if (response.indexOf("\"on\":true") != -1) 67 | { 68 | lightState = 1; 69 | } 70 | return lightState; 71 | } 72 | 73 | void ESPHue::setLight(byte lightNum, byte state, byte sat, byte bri, unsigned int hue) 74 | { 75 | if (!_client->connect(_host, _port)) { 76 | Serial.println("connection failed"); 77 | return; 78 | } 79 | String url = "/api/" + String(_apiKey) + "/lights/" + lightNum + "/state"; 80 | String cmd = "{\"on\":"; 81 | if (state == 1) 82 | cmd += "true,"; 83 | else 84 | cmd += "false,"; 85 | cmd += " \"sat\":" + String(sat) + ", \"bri\":" + String(bri) + ", \"hue\":" + String(hue) + "}"; 86 | int contLen = cmd.length(); 87 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 88 | "Host: " + _host + "\r\n" + 89 | "Connection: keep-alive\r\n" + 90 | "Accept: */*\r\n" + 91 | "Content-Type: application/json\r\n" + 92 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 93 | delay(100); 94 | } 95 | 96 | void ESPHue::setLight(byte lightNum, byte state, byte sat, byte bri, unsigned int hue, unsigned int trans) 97 | { 98 | if (!_client->connect(_host, _port)) { 99 | Serial.println("connection failed"); 100 | return; 101 | } 102 | String url = "/api/" + String(_apiKey) + "/lights/" + lightNum + "/state"; 103 | String cmd = "{\"on\":"; 104 | if (state == 1) 105 | cmd += "true,"; 106 | else 107 | cmd += "false,"; 108 | cmd += " \"sat\":" + String(sat) + ", \"bri\":" + String(bri) + ", \"hue\":" + String(hue) + ", \"transitiontime\":" + String(trans) + "}"; 109 | int contLen = cmd.length(); 110 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 111 | "Host: " + _host + "\r\n" + 112 | "Connection: keep-alive\r\n" + 113 | "Accept: */*\r\n" + 114 | "Content-Type: application/json\r\n" + 115 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 116 | delay(100); 117 | } 118 | 119 | void ESPHue::setLightTemperature(byte lightNum, byte state, byte bri, unsigned int ct) 120 | { 121 | if (!_client->connect(_host, _port)) { 122 | Serial.println("connection failed"); 123 | return; 124 | } 125 | String url = "/api/" + String(_apiKey) + "/lights/" + lightNum + "/state"; 126 | String cmd = "{\"on\":"; 127 | if (state == 1) 128 | cmd += "true,"; 129 | else 130 | cmd += "false,"; 131 | cmd += " \"bri\":" + String(bri) + ", \"ct\":" + String(ct) + "}"; 132 | int contLen = cmd.length(); 133 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 134 | "Host: " + _host + "\r\n" + 135 | "Connection: keep-alive\r\n" + 136 | "Accept: */*\r\n" + 137 | "Content-Type: application/json\r\n" + 138 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 139 | delay(100); 140 | } 141 | 142 | void ESPHue::setLightTemperature(byte lightNum, byte state, byte bri, unsigned int ct, unsigned int trans) 143 | { 144 | if (!_client->connect(_host, _port)) { 145 | Serial.println("connection failed"); 146 | return; 147 | } 148 | String url = "/api/" + String(_apiKey) + "/lights/" + lightNum + "/state"; 149 | String cmd = "{\"on\":"; 150 | if (state == 1) 151 | cmd += "true,"; 152 | else 153 | cmd += "false,"; 154 | cmd += " \"bri\":" + String(bri) + ", \"ct\":" + String(ct) + ", \"transitiontime\":" + String(trans) + "}"; 155 | int contLen = cmd.length(); 156 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 157 | "Host: " + _host + "\r\n" + 158 | "Connection: keep-alive\r\n" + 159 | "Accept: */*\r\n" + 160 | "Content-Type: application/json\r\n" + 161 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 162 | delay(100); 163 | } 164 | 165 | void ESPHue::setLightPower(byte lightNum, byte state) 166 | { 167 | if (!_client->connect(_host, _port)) { 168 | Serial.println("connection failed"); 169 | return; 170 | } 171 | String url = "/api/" + String(_apiKey) + "/lights/" + lightNum + "/state"; 172 | String cmd = "{\"on\":"; 173 | if (state == 1) 174 | cmd += "true}"; 175 | else 176 | cmd += "false}"; 177 | 178 | int contLen = cmd.length(); 179 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 180 | "Host: " + _host + "\r\n" + 181 | "Connection: keep-alive\r\n" + 182 | "Accept: */*\r\n" + 183 | "Content-Type: application/json\r\n" + 184 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 185 | delay(100); 186 | } 187 | 188 | 189 | String ESPHue::getGroupInfo(byte groupNum) 190 | { 191 | if (!_client->connect(_host, _port)) { 192 | Serial.println("connection failed"); 193 | return ""; 194 | } 195 | String url = "/api/" + String(_apiKey) + "/groups/" + groupNum; 196 | _client->print("GET " + url + " HTTP/1.1\r\n" + 197 | "Host: " + _host + "\r\n" + 198 | "Connection: keep-alive\r\n\r\n"); 199 | String line; 200 | delay(200); 201 | while(_client->available()){ 202 | line = _client->readString(); 203 | } 204 | return line; 205 | } 206 | 207 | int ESPHue::getGroupState(byte groupNum) 208 | { 209 | int groupState = 0; 210 | String response = getGroupInfo(groupNum); 211 | 212 | if (response.indexOf("\"on\":false") != -1) 213 | { 214 | groupState = 0; 215 | } 216 | if (response.indexOf("\"on\":true") != -1) 217 | { 218 | groupState = 1; 219 | } 220 | return groupState; 221 | } 222 | 223 | void ESPHue::setGroup(byte groupNum, byte state, byte sat, byte bri, unsigned int hue) 224 | { 225 | if (!_client->connect(_host, _port)) { 226 | Serial.println("connection failed"); 227 | return; 228 | } 229 | String url = "/api/" + String(_apiKey) + "/groups/" + groupNum + "/action"; 230 | String cmd = "{\"on\":"; 231 | if (state == 1) 232 | cmd += "true,"; 233 | else 234 | cmd += "false,"; 235 | cmd += " \"sat\":" + String(sat) + ", \"bri\":" + String(bri) + ", \"hue\":" + String(hue) + "}"; 236 | int contLen = cmd.length(); 237 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 238 | "Host: " + _host + "\r\n" + 239 | "Connection: keep-alive\r\n" + 240 | "Accept: */*\r\n" + 241 | "Content-Type: application/json\r\n" + 242 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 243 | delay(100); 244 | } 245 | 246 | void ESPHue::setGroup(byte groupNum, byte state, byte sat, byte bri, unsigned int hue, unsigned int trans) 247 | { 248 | if (!_client->connect(_host, _port)) { 249 | Serial.println("connection failed"); 250 | return; 251 | } 252 | String url = "/api/" + String(_apiKey) + "/groups/" + groupNum + "/action"; 253 | String cmd = "{\"on\":"; 254 | if (state == 1) 255 | cmd += "true,"; 256 | else 257 | cmd += "false,"; 258 | cmd += " \"sat\":" + String(sat) + ", \"bri\":" + String(bri) + ", \"hue\":" + String(hue) + ", \"transitiontime\":" + String(trans) + "}"; 259 | int contLen = cmd.length(); 260 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 261 | "Host: " + _host + "\r\n" + 262 | "Connection: keep-alive\r\n" + 263 | "Accept: */*\r\n" + 264 | "Content-Type: application/json\r\n" + 265 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 266 | delay(100); 267 | } 268 | 269 | void ESPHue::setGroupTemperature(byte groupNum, byte state, byte bri, unsigned int ct) 270 | { 271 | if (!_client->connect(_host, _port)) { 272 | Serial.println("connection failed"); 273 | return; 274 | } 275 | String url = "/api/" + String(_apiKey) + "/groups/" + groupNum + "/action"; 276 | String cmd = "{\"on\":"; 277 | if (state == 1) 278 | cmd += "true,"; 279 | else 280 | cmd += "false,"; 281 | cmd += " \"bri\":" + String(bri) + ", \"ct\":" + String(ct) + "}"; 282 | int contLen = cmd.length(); 283 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 284 | "Host: " + _host + "\r\n" + 285 | "Connection: keep-alive\r\n" + 286 | "Accept: */*\r\n" + 287 | "Content-Type: application/json\r\n" + 288 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 289 | delay(100); 290 | } 291 | 292 | void ESPHue::setGroupTemperature(byte groupNum, byte state, byte bri, unsigned int ct, unsigned int trans) 293 | { 294 | if (!_client->connect(_host, _port)) { 295 | Serial.println("connection failed"); 296 | return; 297 | } 298 | String url = "/api/" + String(_apiKey) + "/groups/" + groupNum + "/action"; 299 | String cmd = "{\"on\":"; 300 | if (state == 1) 301 | cmd += "true,"; 302 | else 303 | cmd += "false,"; 304 | cmd += " \"bri\":" + String(bri) + ", \"ct\":" + String(ct) + ", \"transitiontime\":" + String(trans) + "}"; 305 | int contLen = cmd.length(); 306 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 307 | "Host: " + _host + "\r\n" + 308 | "Connection: keep-alive\r\n" + 309 | "Accept: */*\r\n" + 310 | "Content-Type: application/json\r\n" + 311 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 312 | delay(100); 313 | } 314 | 315 | void ESPHue::setGroupPower(byte groupNum, byte state) 316 | { 317 | if (!_client->connect(_host, _port)) { 318 | Serial.println("connection failed"); 319 | return; 320 | } 321 | String url = "/api/" + String(_apiKey) + "/groups/" + groupNum + "/action"; 322 | String cmd = "{\"on\":"; 323 | if (state == 1) 324 | cmd += "true}"; 325 | else 326 | cmd += "false}"; 327 | 328 | int contLen = cmd.length(); 329 | _client->print("PUT " + url + " HTTP/1.1\r\n" + 330 | "Host: " + _host + "\r\n" + 331 | "Connection: keep-alive\r\n" + 332 | "Accept: */*\r\n" + 333 | "Content-Type: application/json\r\n" + 334 | "Content-Length: " + contLen + "\r\n\r\n" + cmd + "\r\n\r\n"); 335 | delay(100); 336 | } 337 | -------------------------------------------------------------------------------- /src/ESPHue.h: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | = Philips Hue Library for ESP8266 = 3 | 4 | This library is designed to access and control Philips Hue Lights Directly. 5 | 6 | Written by: Richard Wardlow @ Circuits for Fun, LLC 7 | GNU GPL, include above text in redistribution 8 | ***************************************************************************/ 9 | #include 10 | 11 | //////////////////////////////////////// 12 | // ESPHue Class 13 | //////////////////////////////////////// 14 | class ESPHue 15 | { 16 | public: 17 | ESPHue(WiFiClient& client, const char* APIKey, const char* host, uint8_t port); 18 | void setAPIKey(const char* APIKey); 19 | void setHubIP(const char* host); 20 | void setHubPort(uint8_t port); 21 | String getLightInfo(byte lightNum); 22 | int getLightState(byte lightNum); 23 | void setLight(byte lightNum, byte state, byte sat, byte bri, unsigned int hue); 24 | void setLight(byte lightNum, byte state, byte sat, byte bri, unsigned int hue, unsigned int trans); 25 | void setLightTemperature(byte lightNum, byte state, byte bri, unsigned int ct); 26 | void setLightTemperature(byte lightNum, byte state, byte bri, unsigned int ct, unsigned int trans); 27 | void setLightPower(byte lightNum, byte state); 28 | String getGroupInfo(byte groupNum); 29 | int getGroupState(byte groupNum); 30 | void setGroup(byte groupNum, byte state, byte sat, byte bri, unsigned int hue); 31 | void setGroup(byte groupNum, byte state, byte sat, byte bri, unsigned int hue, unsigned int trans); 32 | void setGroupTemperature(byte groupNum, byte state, byte bri, unsigned int ct); 33 | void setGroupTemperature(byte groupNum, byte state, byte bri, unsigned int ct, unsigned int trans); 34 | void setGroupPower(byte groupNum, byte state); 35 | int ON = 1; 36 | int OFF = 0; 37 | 38 | private: 39 | const char* _host; 40 | const char* _apiKey; 41 | uint8_t _port; 42 | WiFiClient* _client; 43 | 44 | }; 45 | 46 | 47 | --------------------------------------------------------------------------------