Skip to content

Instantly share code, notes, and snippets.

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

Embed
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
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();
}
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.