#include <WiFi.h>
#include <PubSubClient.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Màn hình LCD 20x4
#define led 27
const char *MQTTServer = "broker.emqx.io";
const char *MQTT_Topic_LCD = "21022002/lcd"; // Topic để nhận MSSV và họ tên
const char *MQTT_Topic_MSSV = "21022002/AuThiAnhThu"; // Topic để nhận/so sánh MSSV
const char *MQTT_Response_Topic = "21022002/response"; // Topic phản hồi trạng thái
const char *MQTT_ID = "esp32-client";
int Port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
const int row = 4;
const int col = 4;
char keys[row][col] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[row] = {14, 12, 19, 18};
byte colPins[col] = {5, 4, 2, 15};
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, row, col);
String receivedMSSV = "";
String currentMSSV = ""; // MSSV từ web
String currentName = ""; // Họ và tên từ web
String inputMSSV = ""; // MSSV nhập từ Keypad
bool isPasswordMatched = false;
// Kết nối WiFi
void WIFIConnect() {
Serial.println("Connecting to WiFi...");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected, IP address: " + WiFi.localIP().toString());
}
// Xử lý dữ liệu nhận qua MQTT
void callback(char *topic, byte *message, unsigned int length) {
String msg;
for (int i = 0; i < length; i++) {
msg += (char)message[i];
}
if (String(topic) == MQTT_Topic_LCD) {
int separator = msg.indexOf('|');
if (separator != -1) {
currentMSSV = msg.substring(0, separator);
currentName = msg.substring(separator + 1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MSSV: " + currentMSSV);
lcd.setCursor(0, 1);
lcd.print("Name: " + currentName);
}
} else if (String(topic) == MQTT_Topic_MSSV) {
receivedMSSV = msg;
if (inputMSSV == receivedMSSV) {
digitalWrite(led, HIGH);
delay(3000); // Sáng 3 giây
digitalWrite(led, LOW);
client.publish(MQTT_Response_Topic, "Correct"); // Gửi xác nhận đúng
} else {
for (int i = 0; i < 5; i++) { // Nhấp nháy 5 lần
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
client.publish(MQTT_Response_Topic, "Incorrect"); // Gửi xác nhận sai
}
}
}
void setup() {
Serial.begin(9600);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
lcd.init();
lcd.setBacklight(HIGH);
pinMode(led, OUTPUT);
// Đăng ký các topic MQTT
client.subscribe(MQTT_Topic_LCD);
client.subscribe(MQTT_Topic_MSSV);
}
void loop() {
if (!client.connected()) {
while (!client.connect(MQTT_ID)) {
delay(5000);
}
}
client.loop();
char key = customKeypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
inputMSSV += key;
lcd.setCursor(0, 2);
lcd.print("Input: " + inputMSSV);
} else if (key == '#') {
lcd.setCursor(0, 3);
lcd.print("Verifying...");
client.publish(MQTT_Topic_MSSV, inputMSSV.c_str()); // Gửi MSSV nhập về web
inputMSSV = ""; // Reset sau khi gửi
} else if (key == '*') {
inputMSSV = ""; // Xóa MSSV nhập
lcd.setCursor(0, 2);
lcd.print("Input cleared ");
}
}
}