#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <ThingSpeak.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
long myChannelNumber = 2729019;
const char *myWriteAPIKey = "CL9YNLVFSCQBW2L0";
int statusCode;
const int pirPin = 2; // PIR motion sensor pin
const int greenLedPin = 12; // Green LED pin
const int redLedPin = 13; // Red LED pin
const int buzzerPin = 4; // Buzzer pin
const int lcdColumns = 21; // LCD column size
const int lcdRows = 2; // LCD row size
int R = 0, B = 0, M = 0; // Variables for ThingSpeak fields
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); // Initialize LCD
Servo servo; // Initialize Servo object
void setup() {
pinMode(pirPin, INPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
servo.attach(18); // Attach servo to pin 18
Serial.begin(115200); // Start serial communication
WiFi.mode(WIFI_STA); // Set WiFi to station mode
ThingSpeak.begin(client); // Initialize ThingSpeak
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on LCD backlight
lcd.print("Anti-Theft System");
delay(2000); // Initial display time
servo.write(90); // Set servo to 0 degrees (open door)
randomSeed(analogRead(0)); // Initialize random number generator for sensor (optional)
}
void loop() {
// Check WiFi connection
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
delay(5000); // Delay to give time for WiFi to connect (if not already connected)
// Check PIR sensor state
int pirState = digitalRead(pirPin);
Serial.println(pirState); // Debugging: Print PIR state (0 = no motion, 1 = motion detected)
if (pirState == HIGH) { // Motion detected
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detected!");
digitalWrite(redLedPin, HIGH); // Turn on red LED (indicating alarm)
tone(buzzerPin, 500); // Activate buzzer at 2000Hz
Serial. println(" Door 0 degre");
servo.write(0); // Move servo to 90 degrees (close door)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Closed");
R = 1; // Update status for ThingSpeak (red LED on)
B = 1; // Update status for ThingSpeak (buzzer on)
M = 1; // Update status for ThingSpeak (motion detected)
delay(2000); // Short delay before next loop iteration
} else { // No motion detected
noTone(buzzerPin); // Turn off buzzer
digitalWrite(redLedPin, LOW); // Turn off red LED
Serial. println(" Door Open");
servo.write(90) ;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Open");
e
R = 0; // Update status for ThingSpeak (red LED off)
B = 0; // Update status for ThingSpeak (buzzer off)
M = 0; // Update status for ThingSpeak (no motion)
delay(2000); // Short delay before next loop iteration
}
// Update ThingSpeak fields with sensor data
ThingSpeak.setField(1, B); // Field 1: Buzzer status (0 or 1)
ThingSpeak.setField(2, M); // Field 2: Motion detection status (0 or 1)
ThingSpeak.setField(3, R); // Field 3: Red LED status (0 or 1)
// Write data to ThingSpeak
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) { // Successful write
Serial.println("Channel update successful.");
} else {
Serial.println("Problem writing data. HTTP error code: " + String(statusCode));
}
delay(5000); // Wait for 5 seconds before the next loop
}