#define BLYNK_TEMPLATE_ID "TMPL2glgtocFK"
#define BLYNK_TEMPLATE_NAME "reservation"
#define BLYNK_AUTH_TOKEN "eEdWOob9Kh7KrYNJC8OGUwo_0DJn4nwz"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
const int trigPin1 = 27;
const int echoPin1 = 26;
const int trigPin2 = 2;
const int echoPin2 = 15;
const int trigPin3 = 18;
const int echoPin3 = 5;
const int ledPin1 = 13;
const int ledPin2 = 12;
const int ledPin3 = 14;
const int switchPinEntrance = 32;
const int switchPinExit = 33;
Servo servo;
long duration;
int distance;
bool isSwitchOnEntrance = false;
bool isSwitchOnExit = false;
// LCD initialization (replace the address with the actual I2C address of your LCD)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Blynk authentication token
char auth[] = "Your_Blynk_Auth_Token";
void setup() {
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(switchPinEntrance, INPUT_PULLUP);
pinMode(switchPinExit, INPUT_PULLUP);
servo.attach(25); // Attach servo to pin D25
servo.write(0); // Initialize the servo position
// LCD setup
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Smart Parking");
Serial.begin(9600); // Initialize Serial Monitor
Blynk.begin(BLYNK_AUTH_TOKEN, WIFI_SSID , WIFI_PASSWORD );
}
void loop() {
Blynk.run();
isSwitchOnEntrance = digitalRead(switchPinEntrance) == LOW;
isSwitchOnExit = digitalRead(switchPinExit) == LOW;
if (isSwitchOnEntrance && !isSwitchOnExit) {
// Entrance gate is open, measure distance for each space
Serial.println("Entrance Gate Opened. Measuring Distances...");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Entrance Opened");
delay(2000);
for (int space = 1; space <= 3; ++space) {
measureDistance(space);
delay(1000); // Delay for stability, adjust as needed
}
}
if (isSwitchOnExit) {
// Exit gate is open, allow cars to leave
Serial.println("Exit Gate Opened. Allowing Cars to Leave...");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Exit Opened");
servo.write(90);
delay(5000); // Delay for cars to leave
servo.write(0);
isSwitchOnExit = false; // Reset exit switch status
Serial.println("Exit Gate Closed.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Exit Closed");
}
}
void measureDistance(int space) {
int trigPin, echoPin, ledPin;
String spaceText;
// Assign pins and text based on space number
switch (space) {
case 1:
trigPin = trigPin1;
echoPin = echoPin1;
ledPin = ledPin1;
spaceText = "Space 1: ";
break;
case 2:
trigPin = trigPin2;
echoPin = echoPin2;
ledPin = ledPin2;
spaceText = "Space 2: ";
break;
case 3:
trigPin = trigPin3;
echoPin = echoPin3;
ledPin = ledPin3;
spaceText = "Space 3: ";
break;
default:
return;
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
lcd.clear();
lcd.setCursor(0, 0);
if (distance < 200) {
digitalWrite(ledPin, LOW);
lcd.print(spaceText + "Occupied");
Serial.println(spaceText + "Occupied");
} else {
digitalWrite(ledPin, HIGH);
lcd.print(spaceText + "Vacant");
Serial.println(spaceText + "Vacant");
if (isSwitchOnEntrance) {
// Reservation logic - Blynk button widget on V0
Blynk.virtualWrite(V0, space);
}
}
delay(1000);
}