Maker Pro
Arduino

Smart Home Automated Lights With ESP8266 and Philips Hue (part 1)

April 14, 2019 by Petr Lukáš
Share
banner

How to connect your ESP or Arduino with Philips Hue bridge using RESTfull API and HTTP requests.

In this tutorial, I would like to focus on the basic connection between an ESP8266 and the Philips Hue and show you how to control your Philips Hue system with your Arduino or ESP8266/ESP32. 

We go to "register new device" on the Hue bridge and set up the controller to communicate with build-in RESTful API in bridge (commands are sent in HTTP requests and JSON).

Cheaper Replacement for the Philips Hue Motion Sensor

The sensor activates the light only when certain levels of light is detected (by default less than 100 lx). If the ambient light sensor level exceeds the set value, a switch-off request is sent and motion detection is ignored.

If the PIR sensor stops to detect motion, the timer is started to switch off the light after a set time interval. A controller LED indicates detected motion.

title.png
/*************************************************************
Philips Hue @ Wemos D1 mini (ESP8266)
Basic demo of PIR and ambient light sensor to demonstrate
communication between ESP and Hue gateway
by Petr Lukas
Functionality:
Sensor activates the light only in case certain level of light is detected
(by default less than 100 lx). If the ambient light sensor light level exceeds the set value,
switch-off request is sent and motion detection is ignored.
If the PIR sensor stops to detect motion, the timer is
started to switch off the light after set time interval.
Controller LED indicates detected motion.
*************************************************************/
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
// Ambient light sensor
#include <Wire.h>
#include <BH1750.h>
// IP of Hue gateway
String ip = "YOUR_IP";
// Hue gateway user name
String user_name = "YOUR_USERNAME";
// Light identificator
int light_id = 3;
// Default delay (10 seconds) to switch light back to OFF status
int switch_delay = 10;
// Default ambient light level (100 lx) to prevent light switch when certain light level is detected
int light_level = 100;
// Wifi network SSID
const char* ssid = "YOUR_SSID";
// Wifi network password
const char* password = "YOUR_PASSWORD";
const int PIR = D3;
int PIRState = 0;
unsigned long previousMillis = 0;
bool state = false;
BH1750 light(0x23);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to the WiFi network");
// Light level sensor
Wire.begin();
if (light.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
} else {
Serial.println(F("Error initialising BH1750"));
}
// PIR sensor
pinMode(PIR, INPUT);
pinMode(BUILTIN_LED, OUTPUT);
// set initial state, LED off
digitalWrite(BUILTIN_LED, HIGH);
}
void loop() {
// Set 125 ms to measure precise light level or switch to CONTINUOUS_LOW_RES_MODE and set 20 ms delay
delay(125);
unsigned long currentMillis = millis();
PIRState = digitalRead(PIR);
if(getLightlevel() > light_level){
digitalWrite(BUILTIN_LED, HIGH);
if(state == true) switchLight(1, false);
return;
}
if (PIRState == HIGH) {
digitalWrite(BUILTIN_LED, LOW); // LED on
previousMillis = currentMillis;
if(state == true) return;
switchLight(1, true);
} else {
digitalWrite(BUILTIN_LED, HIGH); // LED off
if(state == false) return;
if (currentMillis - previousMillis >= (switch_delay*1000)){
previousMillis = currentMillis;
switchLight(1, false);
return;
}
}
}
int getLightlevel(){
uint16_t lux = light.readLightLevel();
// Uncomment these lines to detect and set proper light level
//Serial.print("Light: ");
//Serial.print(lux);
//Serial.println(" lx");
return lux;
}
void switchLight(byte room, bool current_state){
state = current_state;
HTTPClient http;
String req_string;
req_string = "http://";
req_string += ip;
req_string += "/api/";
req_string += user_name;
req_string += "/lights/";
req_string += light_id;
req_string += "/state";
Serial.println(req_string);
http.begin(req_string);
http.addHeader("Content-Type", "text/plain");
String put_string;
put_string = "{\"on\":";
put_string += (current_state)? "true" : "false";
put_string += "}";
int httpResponseCode = http.PUT(put_string);
if(httpResponseCode > 0){
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending PUT Request: ");
Serial.println(httpResponseCode);
}
http.end();
}
view raw philips_hue.ino hosted with ❤ by GitHub

Author

Avatar
Petr Lukáš

Application Software Developer, Performance and capacity reporting for IBM zSeries team, experimenting with home automation, sensors, Arduino and ESP

Related Content

Comments


You May Also Like