/*
code web: btth5.html
Bài 1: Xây dựng ứng dụng khoá cửa thông minh và xác thực mật khẩu thông qua ứng dụng WEB theo mô tả sau:
a. Đấu nối Keypad và Buzzer với bo mạch ESP32
b. Kết nối ESP32 với mạng WIFI và MQTT
c. Lập trình khi người dùng nhấn đoạn ký tự sau đó nhấn “#” thì thực hiện gửi dữ liệu đến trạng web và so sánh nội dung mật khẩu hiện có trên khung mật khẩu, nếu đúng thì Buzzer phát 1 hồi âm báo, nếu sai Buzzer phát 3 hồi âm báo ?
*/
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include "toneAC.h"
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
// Số lần nhấn phím để tạo một chuỗi âm thanh
const int MAX_TONE_LENGTH = 10;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {19, 18, 5, 17}; // GPIO19, GPIO18, GPIO5, GPIO17 connect to the row pins
byte pin_column[COLUMN_NUM] = {16, 4, 0, 2}; // GPIO16, GPIO4, GPIO0, GPIO2 connect to the column pins
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
int cursorColumn = 0;
String inputString = "";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqttServer = "broker.hivemq.com";
const int mqttPort = 1883;
const char* mqttTopic = "QuanLyMatKhau";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("password_client")) {
Serial.println("Connected to MQTT");
} else {
Serial.print("Failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') {
// Xóa các ký tự đã nhập trước đó
if (inputString.length() > 0) {
inputString.remove(inputString.length() - 1);
cursorColumn--;
}
} else if (key == '#') {
// Kiểm tra mật khẩu đã nhập
if (inputString == "4699") {
// Mật khẩu đúng
toneAC(4, 2000, 1000); // Tạo âm thanh trên chân GPIO 4
delay(1000);
toneAC(4, 0, 0); // Tắt âm thanh
publishMessage("Correct password");
} else {
// Mật khẩu sai
toneAC(4, 1000, 1000); // Tạo âm thanh khác trên chân GPIO 4
delay(1000);
toneAC(4, 0, 0); // Tắt âm thanh
publishMessage("Wrong password");
}
inputString = "";
cursorColumn = 0;
} else {
// In ký tự và lưu vào chuỗi
inputString += key;
cursorColumn++;
if (cursorColumn >= MAX_TONE_LENGTH) {
inputString = "";
cursorColumn = 0;
}
}
}
if (!client.connected()) {
reconnect();
}
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming MQTT messages, if needed
}
void reconnect() {
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("password_client")) {
Serial.println("Connected to MQTT");
client.subscribe(mqttTopic);
} else {
Serial.print("Failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void publishMessage(const char* message) {
if (client.connected()) {
client.publish(mqttTopic, message);
}
}