Skip to content

Instantly share code, notes, and snippets.

@xxlukas42 xxlukas42/philips_hue.ino
Last active Aug 16, 2019

What would you like to do?
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
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:
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connected to the WiFi network");
// Light level sensor
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);
// 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
unsigned long currentMillis = millis();
PIRState = digitalRead(PIR);
if(getLightlevel() > light_level){
digitalWrite(BUILTIN_LED, HIGH);
if(state == true) switchLight(1, false);
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);
int getLightlevel(){
uint16_t lux = light.readLightLevel();
// Uncomment these lines to detect and set proper light level
//Serial.print("Light: ");
//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";
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();
} else {
Serial.print("Error on sending PUT Request: ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.