#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>
// --- LCD ---
LiquidCrystal_I2C lcd(0x27, 16, 2);
// --- Servo ---
Servo myservo;
const int servoPin = 4;
const int unlockPos = 90;
const int lockPos = 0;
// --- Buzzer ---
const int buzzerPin = 15;
// --- Keypad ---
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);
// --- WiFi ---
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// --- Twilio WhatsApp ---
const char* twilioServer = "https://api.twilio.com/2010-04-01/Accounts/AC7461aa338aa1ff8d1307b52afcb5174d/Messages.json";
String accountSid = "AC7461aa338aa1ff8d1307b52afcb5174d";
String authToken = "5RX3CJ6RR7SRVYL19YU9JA6C";
String fromNumber = "whatsapp:+14155238886";
String toNumber = "whatsapp:+212604275689";
// --- RFID ---
#define SS_PIN 5
#define RST_PIN 22
MFRC522 rfid(SS_PIN, RST_PIN);
String authorizedUID = "A1B2C3D4"; // Remplacez par votre UID réel
// --- Variables ---
String inputCode = "";
const String correctPassword = "1414";
int wrongAttempts = 0;
void sendWhatsAppMessage(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(twilioServer);
http.setAuthorization(accountSid.c_str(), authToken.c_str());
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String body = "From=" + fromNumber +
"&To=" + toNumber +
"&Body=" + message;
int httpResponseCode = http.POST(body);
Serial.print("Réponse Twilio : ");
Serial.println(httpResponseCode);
http.end();
} else {
Serial.println("WiFi non connecté !");
}
}
void unlockDoor() {
Serial.println("Déverrouillage...");
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.print("Initialisation...");
myservo.attach(servoPin);
myservo.write(lockPos);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
SPI.begin();
rfid.PCD_Init();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connecte");
lcd.clear();
lcd.print("Entrer code:");
}
void loop() {
// --- Lecture RFID ---
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
uid += String(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
uid += String(rfid.uid.uidByte[i], HEX);
}
uid.toUpperCase();
Serial.print("Carte lue: "); Serial.println(uid);
rfid.PICC_HaltA();
if (uid == authorizedUID) {
lcd.clear();
lcd.print("Carte valide");
unlockDoor();
} else {
lcd.clear();
lcd.print("Carte inconnue");
sendWhatsAppMessage("🚨 Accès refusé: Carte RFID non reconnue");
tone(buzzerPin, 1000, 1500);
delay(1500);
lcd.clear();
lcd.print("Entrer code:");
}
}
// --- Lecture clavier ---
char key = keypad.getKey();
if (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) {
sendWhatsAppMessage("🚨 2 tentatives de code échouées !");
tone(buzzerPin, 1000, 2000);
wrongAttempts = 0;
} else {
tone(buzzerPin, 1000, 1000);
sendWhatsAppMessage("🚨 Code incorrect saisi !");
}
delay(1000);
lcd.clear();
lcd.print("Entrer code:");
}
inputCode = "";
} else if (key == '*') {
inputCode = "";
lcd.clear();
lcd.print("Efface");
delay(500);
lcd.clear();
lcd.print("Entrer code:");
} else {
if (inputCode.length() < 10) {
inputCode += key;
lcd.setCursor(inputCode.length() - 1, 1);
lcd.print("*");
}
}
}
}