Last active
January 25, 2018 18:30
-
-
Save banthaherder/f58b589d21db1b0d7a12dfccff72ec0e to your computer and use it in GitHub Desktop.
A snippet of code from phase 1 of my project lights_camera_hue to control your Philips Hue Lights with an Arduino via HTTP requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ArduinoJson.h> | |
// A file I declared my passwords and other sensitive info in | |
// Delete it if you don't have one | |
#include "env.h" | |
// WiFi info | |
const char* SSID = YOURWIFINAME; | |
const char* PASSWORD = YOURWIFIPASSWORD; | |
// Locally assigned IP of Hue Hub | |
String NETWORK_ADDRESS = String("http://") + YOURHUEHUBIP; | |
// The slide switch is wired up to pin 13 | |
int slideSwitch = 13; | |
// The initial states should be false to avoid flickering | |
// Won't turn off a light that is already on | |
bool slideState = false; | |
bool lightState = false; | |
// Makes initial connection to local network | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(""); | |
// Connect to the WiFi using the constants SSID and PASSWORD | |
WiFi.begin(SSID, PASSWORD); | |
// Show 'Connecting..' until the status becomes "connected" | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting.."); | |
} | |
// Once the status becomes "connected" print "Connected" | |
Serial.println("Connected"); | |
} | |
void loop() { | |
// Calls a function that checks to see if the switch has changed position | |
bool changed = didSlideChangeState(); | |
// If there has been a hardware change then proceed to bugging the Hub for the light status | |
if (changed) { | |
// Bugs the Hub to see what the current status of the light is | |
lightState = getLightState(); | |
// If the light is off AND the switch is on: Send request to turn light on | |
if(!lightState && slideState == 1) { | |
setLight("true"); | |
// IF the light is on AND the switch is off: Send request to turn light off | |
} else if(lightState && slideState == 0) { | |
setLight("false"); | |
} | |
} | |
// Chill for a reasonable second | |
delay(1000); | |
} | |
// Returns a bool representing a change in the slide switch state | |
bool didSlideChangeState() { | |
// Take a reading from the slide switch | |
bool currentSlideState = digitalRead(slideSwitch); | |
// If the stored slight state and current slide state are different then the switch has changed state. | |
if(slideState != currentSlideState) { | |
// Reset the slide state to the most recently value | |
slideState = currentSlideState; | |
return true; | |
} | |
return false; | |
} | |
// Takes a string argument and uses it make a PUT request to the hub turn the light on/off | |
void setLight(String newLightState) { | |
// Checks to ensure we're connected | |
if (WiFi.status() == WL_CONNECTED) { | |
// Defines a the http client | |
HTTPClient http; | |
// Initializes a connection with the 2nd light on my Hue Hub (living room) | |
http.begin(String("http://") + YOURHUEHUBIP + String("/api/") + YOURDEVICEUSERNAME + String("/lights/2/state")); | |
int httpCode = http.GET(); | |
// If we get any HTTP code back from the hub proceed | |
if (httpCode > 0) { | |
// Makes a PUT request to change the Hub's light state to the value of the newLightState parameter | |
String stateChange = String("{\"on\":") + newLightState + String("}"); | |
http.PUT(stateChange); | |
} | |
// Nix the connection | |
http.end(); | |
} | |
} | |
// Makes a GET request to the Hub to determine whether the light is on or off | |
bool getLightState() { | |
// Checks to ensure we're connected | |
if (WiFi.status() == WL_CONNECTED) { | |
// Defines a the http client | |
HTTPClient http; | |
// Initializes a connection with the 2nd light on my Hue Hub (living room) | |
http.begin(String("http://") + YOURHUEHUBIP + String("/api/") + YOURDEVICEUSERNAME + String("/lights/2")); | |
int httpCode = http.GET(); | |
// Nix the http connection | |
http.end(); | |
// If we get any HTTP code back from the hub proceed | |
if (httpCode > 0) { | |
String httpResponse = http.getString(); | |
// Defines the exact size of the expected return JSON. Saves a lot of memory! | |
const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(2) + 2*JSON_OBJECT_SIZE(11); | |
DynamicJsonBuffer jsonBuffer(capacity); | |
// Parses the httpResponse into the perfectly sized buffer | |
JsonObject& response = jsonBuffer.parseObject(httpResponse); | |
// If the response was a success then check the light state on property and return a bool based on its value | |
return (response.success() && response["state"]["on"] == "true") ? true : false; | |
} | |
} | |
// Only executes if we didn't get an HTTP code back or if we aren't connected to the WiFi | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment