Circuit diagram
Laser Security Alarm Codeππ»ππ»
If your name is not showing on wifi then it means that wifi password and name are not set. First set the wifi password and name of esp.
Wi-Fi code is hereππ»ππ»
#include <ESP8266WiFi.h>
const char* ssid = "My_New_ESP"; // Set your new WiFi name
const char* password = "MyNewPass"; // Set your new password
void setup() {
Serial.begin(115200);
// Start WiFi in Access Point (AP) mode
WiFi.softAP(ssid, password);
Serial.println("WiFi AP Started");
Serial.print("SSID: ");
Serial.println(ssid);
}
void loop() {
// No loop code needed unless you add extra functionality
}
Laser Security Alarm system Codeππ»ππ»
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
#define LDR_PIN A0
#define BUZZER_PIN D6
#define SERVO_PIN D4
ESP8266WebServer server(80);
Servo servo;
int personCount = 0;
int targetCount = 10;
bool countingEnabled = false;
bool isBlocked = false; // Tracks if the laser is currently blocked
void playTone() {
tone(BUZZER_PIN, 1000, 300);
delay(300);
tone(BUZZER_PIN, 1500, 300);
delay(300);
tone(BUZZER_PIN, 2000, 300);
}
void closeDoor() {
servo.write(0); // Door closed position
}
void openDoor() {
servo.write(90); // Door open position
}
void handleRoot() {
String html = "<h1>Laser Security Alarm</h1>";
html += "<p>Person Count: " + String(personCount) + "</p>";
html += "<form action='/setTarget' method='get'>";
html += "Set Target Count: <input type='number' name='target' value='" + String(targetCount) + "'>";
html += "<input type='submit' value='Set'>";
html += "</form>";
html += "<form action='/start' method='get'>";
html += "<input type='submit' value='Start'>";
html += "</form>";
server.send(200, "text/html", html);
}
void handleSetTarget() {
if (server.hasArg("target")) {
targetCount = server.arg("target").toInt();
personCount = 0; // Reset count
server.send(200, "text/html", "Target updated. <a href='/'>Go Back</a>");
}
}
void handleStart() {
personCount = 0; // Reset counter
countingEnabled = true;
openDoor();
server.send(200, "text/html", "Counting restarted. <a href='/'>Go Back</a>");
}
void setup() {
pinMode(LDR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
servo.attach(SERVO_PIN);
closeDoor();
WiFi.begin("Your_SSID", "Your_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.on("/", handleRoot);
server.on("/setTarget", handleSetTarget);
server.on("/start", handleStart);
server.begin();
}
void loop() {
server.handleClient();
if (countingEnabled) {
int ldrValue = analogRead(LDR_PIN);
if (ldrValue < 500 && !isBlocked) { // Laser interrupted, person entering
personCount++;
isBlocked = true; // Mark as blocked to prevent continuous counting
delay(500); // Small debounce delay
}
else if (ldrValue >= 500 && isBlocked) { // Laser restored
isBlocked = false; // Reset block state for next person
}
if (personCount >= targetCount) {
playTone();
closeDoor();
countingEnabled = false;
}
}
}
0 Comments