#include <Keypad.h>
#include <WiFi.h>
#include "PubSubClient.h"
// const char * MQTTServer = "broker.hivemq.com";
const char * MQTTServer = "broker.emqx.io";
const char* topicPasswordEntered = "IoT/password_entered";
const char* topicPasswordResponse = "IoT/password_response";
const int buzzerPin = 4;
// Tạo ID ngẫu nhiên tại: https://www.guidgen.com/
const char * MQTT_ID = "8748c87e-736d-4327-9e75-a52a0e497dd8";
int Port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
String password = "1234";
String enteredPassword = "";
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
const uint8_t ledPins[] = {15, 2, 4, 5, 18, 19};
const int numLed = sizeof(ledPins) / sizeof(ledPins[0]);
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] = {13, 12, 14, 27};
byte pin_column[COLUMN_NUM] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
int kpc = 144;
int transpose = 0;
int midC = 60;
void WIFIConnect() {
Serial.println("Connecting to SSID: Wokwi-GUEST");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected");
Serial.print(", IP address: ");
Serial.println(WiFi.localIP());
}
void MQTT_Reconnect() {
while (!client.connected()) {
if (client.connect(MQTT_ID)) {
Serial.print("MQTT Topic: ");
Serial.print(topicPasswordEntered);
Serial.println(" connected");
client.subscribe(topicPasswordEntered);
Serial.print("MQTT Topic: ");
Serial.print(topicPasswordResponse);
client.subscribe(topicPasswordResponse);
Serial.println("");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.println(topic);
Serial.print("Message: ");
String stMessage;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
stMessage += (char)message[i];
}
Serial.println();
Serial.println("Callback triggered.");
Serial.print("Received topic: ");
Serial.println(topic);
Serial.print("Received message: ");
Serial.println(stMessage);
if (String(topic) == topicPasswordResponse) {
Serial.println("Received password response.");
if (stMessage == "correct") {
Serial.println("Password is correct. Beeping once.");
beep(1); // Đúng mật khẩu, phát âm báo 1 lần
} else if (stMessage == "incorrect") {
Serial.println("Password is incorrect. Beeping three times.");
beep(3); // Sai mật khẩu, phát âm báo 3 lần
}
}
}
void beep(unsigned int count) {
for (unsigned int i = 0; i < count; i++) {
digitalWrite(buzzerPin, HIGH);
Serial.println("Beep");
delay(100);
digitalWrite(buzzerPin, LOW);
if (i < count - 1) delay(100);
}
}
void setup() {
Serial.begin(115200);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
delay(10);
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop(); // Đảm bảo xử lý các callback
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '#') {
// Gửi mật khẩu đã nhập đến MQTT broker khi người dùng nhấn '#'
client.publish(topicPasswordEntered, enteredPassword.c_str());
enteredPassword = ""; // Reset mật khẩu đã nhập
} else if (key == '*') {
enteredPassword = ""; // Reset mật khẩu đã nhập khi người dùng nhấn '*'
} else {
enteredPassword += key; // Thêm ký tự đã nhập vào biến enteredPassword
}
}
}