#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Preferences.h>
#include <Ticker.h>
#include <ESP32Servo.h>
#include <DHT11.h>
Ticker timer; // Create a Ticker object
// Initialize the LCD
LiquidCrystal lcd(12, 13, 14, 27, 26, 25); // Pins for RS, E, D4, D5, D6, D7
// Initialize servo motor
const int servoPin = 20;
Servo myServo;
// Initialize Gas sensor
const int gasSensorPin = 34;
const int threshold = 300; // is the start gas sensor value to alarm
// Initialize Temperature sensor
const int DHTPin = 4;
DHT11 dht11;
// Initialize Buzzer
const int buzzerPin = 6;
// Initialize IR sensor
const int IRPin = 8
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {19, 18, 5, 17}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {16, 4, 0, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String pass = "156879";
Preferences preferences; // Create a Preferences object
void deleteOTPPassword() {
preferences.begin("passwords", false);
preferences.remove("password"); // Remove the password from preferences
preferences.end();
Serial.println("Password deleted!");
}
void get_password(){
String enteredPass = "";
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0);
lcd.print("Enter password:");
lcd.setCursor(0, 1);
int passIndex = 0; // Variable to keep track of the current index of the entered password
while(true){
// Read from keypad
char key = keypad.getKey();
if (key && passIndex < pass.length()) {
Serial.println(key);
if (key == 'C' && passIndex > 0) {
// Clear the last entered key
lcd.setCursor(passIndex - 1, 1); // Set cursor to the previous position
lcd.print(' '); // Clear the character
lcd.setCursor(passIndex - 1, 1);
enteredPass.remove(enteredPass.length() - 1); // Remove the last character from the entered password
passIndex--; // Decrement the index of the entered password
} else if (key != 'C') {
// Append the key to the entered password
enteredPass += key;
lcd.print('*'); // Print * to represent each entered character
passIndex++; // Increment the index of the entered password
}
}
// Check if the entered password matches the predefined one
if (enteredPass == pass) {
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print("Correct pass"); // Display message for correct password
delay(2000); // Wait for 2 seconds
lcd.clear(); // Clear the LCD
return;
}
else if(enteredPass.length() == pass.length() && enteredPass!=pass){
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print("Invalid pass"); // Display message for correct password
delay(2000); // Wait for 2 seconds
lcd.clear(); // Clear the LCD
return;
}
delay(100); // Slight delay to debounce the keypad
}
}
// Function to handle the password received from the broker
void OTP_pass(String password) {
preferences.begin("passwords", false);
preferences.putString("password", password); // Save the password in preferences
preferences.end();
Serial.println("Password stored successfully!");
// Set up the timer to delete the password after 15 minutes (900,000 milliseconds)
timer.attach(900000, deleteOTPPassword); // Attach the deletePassword function to run after 15 minutes
}
// Function to set the servo angle
void setServoAngle(int angle) {
// Ensure the angle is within the valid range
if (angle < 0) angle = 0;
if (angle > 180) angle = 180;
// Write the angle to the servo
myServo.write(angle);
}
// Function to check if gas is detected
bool isGasDetected() {
// Read the analog value from the MQ-5 sensor
int sensorValue = analogRead(mq5Pin);
// Print the sensor value for debugging
Serial.print("Sensor value: ");
Serial.println(sensorValue);
// Compare the sensor value with the threshold
if (sensorValue > threshold) {
buzerrON();
return true;
} else {
return false;
}
}
// Function to get temperature and humidity
void getTemperatureAndHumidity(float &temperature, float &humidity) {
// Read temperature and humidity
int chk = dht11.read(DHTPin, &temperature, &humidity, NULL);
// Check if the Reading failed
if (chk != DHTLIB_OK) {
temperature = -1; // Indicate failure with a specific value
humidity = -1; // Indicate failure with a specific value
}
}
void buzerrON(){
digitalWrite(buzzerPin, HIGH);
}
void buzerrOFF(){
digitalWrite(buzzerPin, LOW);
}
bool isDoorOpen(){
if(digitalRead(IRPin) == 1){
// Door Closed
return false;
}else{
return true;
}
}
void setup() {
Serial.begin(115200);.
// Initialize the Servo motor
myServo.attach(servoPin);
pinMode(buzzerPin, OUTPUT);
pinMode(IRPin, INPUT);
pinMode(gasSensorPin, INPUT);
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.clear(); // Clear the LCD
preferences.begin("passwords", false); // Begin preferences with the namespace "passwords"
// Retrieve and display the stored password (if any)
String storedPass = preferences.getString("password", "No password stored");
Serial.println("Stored Password: " + storedPass);
}
void loop() {
char key = keypad.getKey();
if (key) {
get_password();
Serial.println("End get pass");
pinMode(21, OUTPUT);
digitalWrite(21, HIGH);
delay(1000);
digitalWrite(21, LOW);
}
}