#include <WiFi.h>
#include "PubSubClient.h"
#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 16, 4, 2, 15 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 19, 18, 5, 17 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char * MQTTServer = "broker.emqx.io";
const char * MQTT_Topic = "KEYPASSSSs";
// 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);
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;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
stMessage += (char)message[i];
}
if (stMessage == "on") {
tone(21,250,250);
delay(1000);
}
else if (stMessage == "off") {
tone(21,250,250);
delay(1000);
tone(21,250,250);
delay(1000);
tone(21,250,250);
delay(1000);
}
}
String pass="";
void guiPWW(String a){
client.publish("KEYPASSSS",String(a).c_str());
}
void setup() {
Serial.begin(115200);
pinMode(21, OUTPUT);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop();
char key = keypad.getKey();
if (key != NO_KEY) {
if (key != '#') {
pass+=key;
}else
{
guiPWW(pass);
Serial.println(pass);
pass="";
}
}
}
<!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">
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.6.1.min.js" type="text/javascript"></script>
</head>
<body>
<h1>ỨNG DỤNG QUẢN LÝ MẬT KHẨU <br> KHÓA CỬA THÔNG MINH</h1>
<h3>Nhập mật khẩu cần thay đổi</h3>
<input type="password" id="mk">
<label id="passs"></label>
<br><button class="changepw" style="text-align: center;background-color: chocolate;">Thay đổi mật khẩu</button>
</html>
<script>
$('.changepw').click(function(){
doiMK();
});
client = new Paho.MQTT.Client("broker.emqx.io", Number(8083), "73560a74-2da4-4dae-babe-c3214fbdae33");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
console.log("onConnect");
client.subscribe("KEYPASSSS");
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
var pass= document.getElementById("mk").value;
if(message.payloadString==pass){
message = new Paho.MQTT.Message("on");
message.destinationName = "KEYPASSSSs";
client.send(message);
}else{
message = new Paho.MQTT.Message("off");
message.destinationName = "KEYPASSSSs";
client.send(message);
}
}
function doiMK(){
var noidung = document.getElementById("mk").value;
document.getElementById("passs").innerHTML = noidung;
}
</script>