#include <WiFi.h>
#define BLYNK_TEMPLATE_ID "TMPL6lksiKUlu"
#define BLYNK_TEMPLATE_NAME "Parking Slot IOT"
#define BLYNK_PRINT Serial
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <BlynkSimpleEsp32.h>
// Blynk Authentication Token
char auth[] = "9yxsyoSeCa7AcBFohUl5wCf0cZDZPHZm"; // Replace with your Blynk Auth Token
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// Define pins for the Ultrasonic sensors
#define echoPin1 35 // Echo pin for sensor 1
#define trigPin1 32 // Trigger pin for sensor 1
#define echoPin2 4 // Echo pin for sensor 2
#define trigPin2 2 // Trigger pin for sensor 2
// Define Servo pins
Servo myServo1; // Servo 1 to control first portal (masuk)
Servo myServo2; // Servo 2 to control second portal (keluar)
// Variables to count the vehicles
int vehiclesCount = 0; // To store the current vehicle count
bool vehicleInDetected = false; // Flag to track if a vehicle entered
bool vehicleOutDetected = false; // Flag to track if a vehicle exited
// Define virtual pins for Blynk control
#define V1 1 // Virtual Pin 1 for portal entry (masuk)
#define V2 2 // Virtual Pin 2 for portal exit (keluar)
void setup() {
// Initialize serial communication
Serial.begin(115200); // Begin serial communication at 115200 baud rate
// Initialize LCD
lcd.init();
lcd.backlight(); // Turn on LCD backlight
// Initialize the ultrasonic sensor pins
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
// Initialize the servos
myServo1.attach(13); // Servo 1 is connected to pin 13 (pintu masuk)
myServo2.attach(15); // Servo 2 is connected to pin 15 (pintu keluar)
// Initialize Blynk
Blynk.begin(auth, "Wokwi-GUEST", ""); // Replace with your Wi-Fi credentials
// Display a welcome message
lcd.setCursor(0, 0);
lcd.print(" KELOMPOK 1");
delay(3000);
// Print to Serial Monitor
Serial.println("System Initialized");
}
BLYNK_WRITE(V1) {
if (param.asInt() == 1) { // Button is pressed
myServo1.write(0); // Open portal for entry
Blynk.virtualWrite(V2, "Portal Terbuka");
} else {
myServo1.write(90); // Close portal for entry
Blynk.virtualWrite(V2, "Portal Tertutup");
}
}
BLYNK_WRITE(V0) {
if (param.asInt() == 1) { // Button is pressed
myServo2.write(0); // Open portal for exit
Blynk.virtualWrite(V3, "Portal Terbuka");
} else {
myServo2.write(90); // Close portal for exit
Blynk.virtualWrite(V3, "Portal Tertutup");
}
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("[Kapasitas Area]");
lcd.setCursor(0, 1);
lcd.print("Total: ");
lcd.print(vehiclesCount); // Display updated vehicle count
lcd.print("/10");
Blynk.virtualWrite(V4, vehiclesCount);
// Get distances from the ultrasonic sensors
long duration1, distance1;
long duration2, distance2;
// Sensor 1 (Trigger and measure distance for first sensor)
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 / 2) / 29.1; // Formula to calculate distance in cm
// Sensor 2 (Trigger and measure distance for second sensor)
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1; // Formula to calculate distance in cm
// Display vehicle count in Blynk app
// Detect vehicle entering through Sensor 1 (Only allow entry if vehicle count < 10)
if (distance1 < 5 && !vehicleInDetected) {
if (vehiclesCount < 10) { // Check if the vehicle count is less than 10
vehiclesCount++; // Increase the vehicle count when entering
vehicleInDetected = true; // Mark that vehicle entered
lcd.setCursor(0, 0);
lcd.print("Kendaraan Masuk ");
Blynk.virtualWrite(V2, "Portal Terbuka");
lcd.setCursor(0, 1);
lcd.print("Total: ");
lcd.print(vehiclesCount); // Display updated vehicle count
lcd.print("/10");
Blynk.virtualWrite(V4, vehiclesCount);
Serial.println("Vehicle In Detected");
myServo1.write(0); // Open portal 1 (vehicle enters)
delay(2000); // Delay to simulate the vehicle passing through
lcd.clear();
myServo1.write(90); // Close the portal after vehicle passes
Blynk.virtualWrite(V2, "Portal Tertutup");
} else {
// If vehicle count is 10 or more, block the entry
lcd.setCursor(0, 0);
lcd.print("Parkiran Penuh!");
lcd.setCursor(0, 1);
lcd.print("Total: ");
lcd.print(vehiclesCount);
lcd.print("/10");
Blynk.virtualWrite(V2, "Parkiran Penuh");
delay(1000);
lcd.clear();
}
}
// Reset vehicle detection for sensor 1
if (distance1 >= 5) {
vehicleInDetected = false;
}
// Detect vehicle exiting through Sensor 2
if (distance2 < 5 && !vehicleOutDetected) {
vehiclesCount--; // Decrease the vehicle count when exiting
vehicleOutDetected = true; // Mark that vehicle exited
lcd.setCursor(0, 0);
lcd.print("Kendaraan Keluar");
Blynk.virtualWrite(V3, "Portal Terbuka");
lcd.setCursor(0, 1);
lcd.print("Total: ");
lcd.print(vehiclesCount); // Display updated vehicle count
lcd.print("/10 ");
Blynk.virtualWrite(V4, vehiclesCount);
Serial.println("Vehicle Out Detected");
myServo2.write(0); // Open portal 2 (vehicle exits)
delay(2000); // Delay to simulate the vehicle passing through
lcd.clear();
myServo2.write(90); // Close the portal after vehicle passes
Blynk.virtualWrite(V3, "Portal Tertutup");
}
// Reset vehicle detection for sensor 2
if (distance2 >= 5) {
vehicleOutDetected = false;
}
// Wait before updating the LCD again
delay(100);
Blynk.run(); // Keep Blynk connected
}