#include <Adafruit_Fingerprint.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define buzzerPin 7
#define servoPin 6
#define RX_PIN 2
#define TX_PIN 3
// Initialize the ILI9341 display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Initialize the fingerprint sensor
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial1);
// Initialize the servo
Servo myservo;
// WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Function prototypes
void connectToWiFi();
void setup() {
// Initialize Serial
Serial.begin(115200);
Serial1.begin(57600); // For fingerprint sensor
// Initialize the display
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
// Initialize the buzzer
pinMode(buzzerPin, OUTPUT);
// Initialize the servo
myservo.attach(servoPin);
myservo.write(0); // Close the lock initially
// Initialize the fingerprint sensor
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
// Connect to WiFi
connectToWiFi();
// Print message on the display
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Smart Door Lock");
}
void loop() {
int fingerprintID = getFingerprintID();
if (fingerprintID >= 0) {
// Successful fingerprint match
digitalWrite(buzzerPin, HIGH); // Buzzer on
myservo.write(90); // Unlock the door
delay(3000); // Keep the door unlocked for 3 seconds
myservo.write(0); // Lock the door again
digitalWrite(buzzerPin, LOW); // Buzzer off
} else {
// Fingerprint not recognized
digitalWrite(buzzerPin, HIGH); // Buzzer on
delay(1000); // Buzzer sound for 1 second
digitalWrite(buzzerPin, LOW); // Buzzer off
}
delay(1000); // Short delay before next fingerprint scan
}
int getFingerprintID() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// Found a match
Serial.print("Found ID #");
Serial.print(finger.fingerID);
Serial.print(" with confidence of ");
Serial.println(finger.confidence);
return finger.fingerID;
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}