/* ESP32 HTTP IoT Server Example for Wokwi.com
https://wokwi.com/projects/320964045035274834
To test, you need the Wokwi IoT Gateway, as explained here:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
Then start the simulation, and open http://localhost:9080
in another browser tab.
Note that the IoT Gateway requires a Wokwi Club subscription.
To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/
#include <Wire.h>
#include <BH1750.h>
#define PIR_PIN 13 // Pin connected to PIR sensor output
#define RELAY_PIN 15 // Pin connected to Relay module
BH1750 lightMeter;
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure the relay is off initially
Wire.begin();
lightMeter.begin();
}
void loop() {
// Read PIR sensor
int motionDetected = digitalRead(PIR_PIN);
// Read ambient light level
float lux = lightMeter.readLightLevel();
// Control logic
if (motionDetected == HIGH && lux < 300) { // Example threshold value for lux
digitalWrite(RELAY_PIN, HIGH); // Turn on the lights
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off the lights
}
// Debug output
Serial.print("Motion: ");
Serial.print(motionDetected);
Serial.print(" - Light level: ");
Serial.print(lux);
Serial.println(" lx");
delay(1000); // Adjust as necessary for response time
}