#define BLYNK_TEMPLATE_ID "TMPL2bclPtm8i"
#define BLYNK_TEMPLATE_NAME "final project"
#define BLYNK_AUTH_TOKEN "2aQUgpLY_Io7nSsAwvg6SMvTLuWBNfs9"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <DHTesp.h>
#include <BlynkSimpleEsp32.h>
#define DHTPIN 16
DHTesp dht;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
Servo motor1;
Servo motor2;
int pos = 0;
const int trigpin = 18;
const int echopin = 19;
const int ldrpin = 34; // LDR on pin 36
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust the I2C address if needed
int led1 = 5; // LED 1 connected to pin 5
int led2 = 27; // LED 2 connected to pin 27
double distancecm;
double duration;
double distanceinch;
BlynkTimer timer;
void setup() {
Serial.begin(115200);
dht.setup(DHTPIN, DHTesp::DHT22);
Blynk.begin(auth, ssid, pass);
//timer.setInterval(2000L, sendDHTData);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(led1, OUTPUT); // Define LED 1 pin as output
pinMode(led2, OUTPUT); // Define LED 2 pin as output
pinMode(ldrpin, INPUT);
analogReadResolution(10);
motor1.attach(12); // Attach servo to pin 12
motor2.attach(14); // Attach servo to pin 14
motor1.write(90); // Start motor1 at opposite direction (180 degrees)
motor2.write(90); // Start motor2 at opposite direction (0 degrees)
lcd.init(); // Initialize LCD with default settings (20x4)
lcd.clear(); // Clear the LCD at startup
lcd.setBacklight(1); // Turn on backlight
lcd.setCursor(0, 0);
lcd.print("Complete");
lcd.setCursor(0, 1);
lcd.print("Automation ");
delay(1000); // Wait for the startup message to show
lcd.clear();
// Start Serial communication
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}
BLYNK_WRITE(V3) {
int buttonState = param.asInt();
if (buttonState == 1) {
motoropen();
Blynk.virtualWrite(V0, 1);
delay(2000);
Blynk.virtualWrite(V0, 0);
motorclose();
Blynk.virtualWrite(V3, 0);
} else {
motorclose();
}
}
//void sendDHTData() {
//float t = dht.getTemperature();
// float h = dht.getHumidity();
// if (isnan(h) || isnan(t)) {
// Serial.println("Failed to read from DHT sensor!");
// return;
// }
// Blynk.virtualWrite(V2, t);
//Blynk.virtualWrite(V4, h);
/*Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");*/
//}
void loop() {
// Measure distance using ultrasonic sensor
Blynk.run();
timer.run();
int ldrValue = analogRead(ldrpin);
int brightness = map(ldrValue, 0, 1023, 0, 1023);
// timer.setInterval(500L, sendDHTData);
float t = dht.getTemperature();
float h = dht.getHumidity();
Blynk.virtualWrite(V2, t);
Blynk.virtualWrite(V4, h);
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distancecm = (duration * 0.034) / 2;
// Check for serial communication
if (Serial.available() > 0) {
String message = Serial.readStringUntil('\n'); // Read the message until newline
message.trim(); // Remove any extra spaces or newlines
// Process received messages
if (message == "SUCCESS") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted");
lcd.setCursor(0, 1);
lcd.print("Gates - OPENING");
motoropen(); // Open the gate
Blynk.virtualWrite(V0, 1);
delay(2000);
Blynk.virtualWrite(V5, "Correct Password");
Blynk.virtualWrite(V0, 0);
motorclose();
}
else if (message == "FAILURE") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied");
lcd.setCursor(0, 1);
lcd.print("Gates - CLOSED");
motorclose(); // Close the gate
delay(400);
Blynk.virtualWrite(V0, 0);
Blynk.virtualWrite(V5, "Take care, someone is trying to enter your home!");
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Command");
delay(400);
}
}
// If distance is greater than 10 cm, turn off the entire system
if (distancecm > 10) {
Blynk.virtualWrite(V1, "System Off");
analogWrite(led1, 0); // Ensure no brightness for LED 1
analogWrite(led2, 0); // Ensure no brightness for LED 2
//motor1.write(90); // Stop motor1
//motor2.write(90); // Stop motor2
lcd.clear(); // Clear LCD
lcd.setCursor(0, 0);
lcd.print("System Off");
return; // Exit the loop to stop everything
}else{
Blynk.virtualWrite(V1, "System on");
lcd.setCursor(0, 0);
lcd.print("LED: ON "); // Display LED status
analogWrite(led1, brightness); // // Ensure no brightness for LED 2
analogWrite(led2, brightness); // // Ensure no brightness for LED 2
delay(100);
//Serial.println(ldrValue);
//Serial.println(brightness1);
lcd.setCursor(0, 1);
lcd.print("Temperature: ");
lcd.print(t);
}
}
// Function to open the motor (gate)
void motorclose() {
for (; pos <= 90; pos += 10) {
motor1.write(pos);
motor2.write(pos);
delay(80);
}
}
// Function to close the motor (gate)
void motoropen() {
for (; pos >= 0; pos -= 10) {
motor1.write(pos);
motor2.write(pos+180);
delay(80);
}
}