#define BLYNK_TEMPLATE_ID "TMPL3ln0YFxZT"
#define BLYNK_TEMPLATE_NAME "CROWD MANAGEMENT"
#define BLYNK_AUTH_TOKEN "KkmooTOwOpqcaL40luyxrTsQnjHutHiB"
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST"; // Your Wi-Fi SSID
char pass[] = ""; // Your Wi-Fi password
// Blynk authentication token
char auth[] = "KkmooTOwOpqcaL40luyxrTsQnjHutHiB"; // Your Blynk auth token
// Sensor pins
#define PIR_PIN 4 // PIR sensor pin (GPIO 4)
#define ULTRASONIC_TRIG_PIN 2 // Ultrasonic sensor TRIG pin (GPIO 2)
#define ULTRASONIC_ECHO_PIN 15 // Ultrasonic sensor ECHO pin (GPIO 15)
#define SMOKE_SENSOR_PIN 33 // Smoke or Gas sensor pin (GPIO 33)
#define PANIC_BUTTON_PIN 5 // Physical panic button pin (GPIO 5)
// Variables for sensor readings
bool motionDetected = false; // PIR motion status
long distance = 0; // Ultrasonic measured distance
int crowdDensityLevel = 0; // Crowd density level (0 = Low, 1 = Medium, 2 = High)
bool smokeDetected = false; // Smoke or gas detection status
bool panicButtonPressed = false; // Panic button status
void setup() {
// Start Serial monitor
Serial.begin(9600);
delay(1000);
// Debugging info
Serial.println("Starting setup...");
Serial.print("Connecting to Wi-Fi: ");
Serial.println(ssid);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initialize sensor pins
pinMode(PIR_PIN, INPUT);
pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode(ULTRASONIC_ECHO_PIN, INPUT);
pinMode(SMOKE_SENSOR_PIN, INPUT);
pinMode(PANIC_BUTTON_PIN, INPUT_PULLUP);
Serial.println("Blynk connection complete");
Serial.println("Setup complete.");
}
void loop() {
Blynk.run();
// Read PIR sensor
motionDetected = digitalRead(PIR_PIN);
// Read Ultrasonic sensor
distance = getUltrasonicDistance();
// Read Smoke or Gas sensor
smokeDetected = analogRead(SMOKE_SENSOR_PIN) > 1000; // Adjust threshold as needed
// Read Physical Panic Button
panicButtonPressed = digitalRead(PANIC_BUTTON_PIN) == LOW;
// Determine crowd density level
if (motionDetected && distance < 50) {
crowdDensityLevel = 2; // High density
Blynk.logEvent("high_density"); // Predefined event
} else if (motionDetected && distance >= 50 && distance <= 100) {
crowdDensityLevel = 1; // Medium density
Blynk.logEvent("moderate_density"); // Predefined event
} else {
crowdDensityLevel = 0; // Low density
}
// Emergency alerts
if (smokeDetected) {
Blynk.logEvent("smoke_alert","Smoke alert !!!"); // Predefined event
Serial.println("Smoke alert !!!");
}
if (panicButtonPressed) {
Blynk.logEvent("panic_alert","Panic alert from physical button!!!"); // Predefined event
Serial.println("Panic alert from physical button !!!");
}
// Send data to Blynk app
Blynk.virtualWrite(V2, crowdDensityLevel); // Send crowd density level
Blynk.virtualWrite(V4, distance); // Send distance value
Blynk.virtualWrite(V5, motionDetected); // Send motion detection status
Blynk.virtualWrite(V7, smokeDetected); // Send smoke detection status
Blynk.virtualWrite(V6, panicButtonPressed); // Send physical panic button status
// Print data to Serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Motion Detected: ");
Serial.println(motionDetected ? "Yes" : "No");
Serial.print("Crowd Density Level: ");
if (crowdDensityLevel == 2) Serial.println("High");
else if (crowdDensityLevel == 1) Serial.println("Medium");
else Serial.println("Low");
Serial.print("Smoke Detected: ");
Serial.println(smokeDetected ? "Yes" : "No");
Serial.print("Panic Button Pressed: ");
Serial.println(panicButtonPressed ? "Yes" : "No");
delay(2000); // Delay for better readings
}
// Function to calculate distance using Ultrasonic sensor
long getUltrasonicDistance() {
long duration, distance;
// Trigger the ultrasonic sensor
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
// Measure the time taken by the echo
duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
return distance;
}
// Function to handle virtual panic button
BLYNK_WRITE(V6) {
bool virtualPanicButton = param.asInt(); // Read virtual button state
if (virtualPanicButton) {
Blynk.logEvent("panic_alert","Panic alert from virtual button!!!"); // Predefined event
Serial.println("Panic alert from virtual button !!!");
}
}