#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <HTTPClient.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
const int servoPin = 4;
const int unlockPos = 90;
const int lockPos = 0;
const int buzzerPin = 15;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String botToken = "7602956280:AAHa0z5NLqApmw3xOH3LlQtggHQPdDrR7ng";
String chatId = "7737620102";
String inputCode = "";
const String correctPassword = "1414";
int wrongAttempts = 0;
void sendTelegramMessage(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://api.telegram.org/bot" + botToken + "/sendMessage?chat_id=" + chatId + "&text=" + message;
http.begin(url);
int httpResponseCode = http.GET();
http.end();
Serial.print("Message Telegram envoyé, code réponse : ");
Serial.println(httpResponseCode);
} else {
Serial.println("WiFi non connecté !");
}
}
void unlockDoor() {
Serial.println("Déverrouillage porte");
myservo.write(unlockPos);
delay(5000);
lockDoor();
}
void lockDoor() {
myservo.write(lockPos);
lcd.clear();
lcd.print("Porte fermee");
delay(1000);
lcd.clear();
lcd.print("Entrer code:");
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Door Ready");
myservo.attach(servoPin);
myservo.write(lockPos);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
WiFi.begin(ssid, password);
Serial.print("Connexion WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connecté !");
delay(2000);
lcd.clear();
lcd.print("Entrer code:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Touche : ");
Serial.println(key);
if (key == '#') {
if (inputCode == correctPassword) {
lcd.clear();
lcd.print("Code correct");
wrongAttempts = 0;
unlockDoor();
} else {
wrongAttempts++;
lcd.clear();
lcd.print("Code incorrect");
if (wrongAttempts >= 2) {
lcd.setCursor(0, 1);
lcd.print("Alerte sonore!");
tone(buzzerPin, 1000);
sendTelegramMessage("🚨 2 codes erronés successifs !");
delay(1500);
noTone(buzzerPin);
wrongAttempts = 0;
} else {
sendTelegramMessage("🚨 Code incorrect saisi !");
delay(1000);
}
lcd.clear();
lcd.print("Entrer code:");
}
inputCode = "";
} else if (key == '*') {
inputCode = "";
lcd.clear();
lcd.print("Code efface");
delay(500);
lcd.clear();
lcd.print("Entrer code:");
} else {
if (inputCode.length() < 10) {
inputCode += key;
lcd.setCursor(inputCode.length() - 1, 1);
lcd.print("*");
}
}
}
}