#include <WiFi.h>
#include <PubSubClient.h>
#include <Keypad.h>
const char * MQTTServer = "broker.emqx.com";
const char * MQTT_Topic = "IOT/PhatTrien/doipass";
// Tạo ID ngẫu nhiên tại: https://www.guidgen.com/
const char * MQTT_ID = "";
int Port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
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] = {5, 17, 16, 14};
byte colPins[COLS] = {0, 2, 15, 1};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int buzzerPin = 18;
String inputPassword = "";
const String correctPassword = "1234";
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(MQTT_Topic);
Serial.print(" connected");
client.subscribe(MQTT_Topic);
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;
}
void setup() {
Serial.begin(115200);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop();
char key = keypad.getKey();
if (key) {
if (key == '#') {
client.publish(MQTT_Topic, inputPassword.c_str());
if (inputPassword == correctPassword) {
tone(buzzerPin, 1000, 200);
delay(200);
noTone(buzzerPin);
} else {
for (int i = 0; i < 3; i++) {
tone(buzzerPin, 1000, 200);
delay(200);
noTone(buzzerPin);
delay(200);
}
}
inputPassword = "";
} else {
inputPassword += key;
}
}
}
/*<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ỨNG DỤNG QUẢN LÝ MẬT KHẨU KHÓA CỬA THÔNG MINH</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<style>
input[type=password] {
padding: 10px;
margin: 10px 0;
}
button {
padding: 10px 20px;
background-color: orange;
color: white;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>ỨNG DỤNG QUẢN LÝ MẬT KHẨU KHÓA CỬA THÔNG MINH</h1>
<p>Nhập mật khẩu cần thay đổi</p>
<input type="password" name="password" id="pw">
<br>
<br>
<button class="submit">
Thay đổi mật khẩu
</button>
<script>
const topic = "key";
const client = new Paho.MQTT.Client("broker.hivemq.com", Number(8000), "0000000000000");
$(".submit").click(() => {
const password = $("#pw").val();
if (password !== "") {
message = new Paho.MQTT.Message(password);
message.destinationName = topic;
client.send(message);
} else {
alert("Please enter a password!");
}
})
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({
onSuccess: onConnect
});
function onConnect() {
console.log("onConnect");
client.subscribe(topic);
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
const data = JSON.parse(message.payloadString);
}
</script>
</body>
</html>*/