#include <WiFi.h>
#include <WebServer.h>
// WiFi credentials
const char* ssid = "your_ssid";
const char* password = "your_password";
// PIN code for unlocking
const String correctPin = "1234";
// Temperature sensor pin
const int tempPin = 34;
// PIR motion sensor pin
const int pirPin = 12;
// Proximity sensor pins
const int trigPin = 23;
const int echoPin = 22;
// Relay pin for door lock
const int relayPin = 19;
// LED pins
const int ledLightPin = 13; // For light intensity
const int ledDoorPin = 21; // For door status
// Create a web server object
WebServer server(80);
void setup() {
Serial.begin(115200);
// Set pin modes
pinMode(tempPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(ledLightPin, OUTPUT);
pinMode(ledDoorPin, OUTPUT);
digitalWrite(relayPin, LOW); // Lock door initially
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Define web server routes
server.on("/", handleRoot);
server.on("/unlock", HTTP_GET, handleUnlock);
server.begin();
}
void loop() {
server.handleClient();
handleTemperatureControl();
handleMotionDetection();
}
void handleTemperatureControl() {
int tempValue = analogRead(tempPin); // Read temperature
int brightness = map(tempValue, 0, 4095, 0, 255); // Map to brightness
digitalWrite(ledLightPin, brightness); // Set LED brightness based on temperature
Serial.print("Temperature (analog): ");
Serial.print(tempValue);
Serial.print(" Light Intensity: ");
Serial.println(brightness);
}
void handleMotionDetection() {
int pirState = digitalRead(pirPin); // Read motion sensor state
if (pirState == HIGH) { // If motion is detected
digitalWrite(ledDoorPin, HIGH); // Turn on door status LED
} else {
digitalWrite(ledDoorPin, LOW); // Turn off LED if no motion is detected
}
// Check proximity
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2; // Calculate distance in cm
// If user is within 30 cm, LED on
if (distance < 30) {
digitalWrite(ledDoorPin, HIGH); // Turn on LED if close to the door
} else {
digitalWrite(ledDoorPin, LOW); // Turn off LED if far
}
}
void handleRoot() {
String html = "<h1>Smart Door</h1>";
html += "<form action=\"/unlock\" method=\"GET\">";
html += "Enter PIN: <input type=\"text\" name=\"pin\">";
html += "<input type=\"submit\" value=\"Unlock\">";
html += "</form>";
server.send(200, "text/html", html);
}
void handleUnlock() {
String pin = server.arg("pin");
if (pin == correctPin) {
digitalWrite(relayPin, HIGH); // Unlock door
delay(5000); // Keep unlocked for 5 seconds
digitalWrite(relayPin, LOW); // Lock door again
server.send(200, "text/html", "Door Unlocked!");
} else {
server.send(200, "text/html", "Incorrect PIN!");
}
}