#include <WiFi.h>
#include "PubSubClient.h"
#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
const char * MQTTServer = "broker.hivemq.com";
const char * MQTT_Topic = "VLUTE/KEYPAD";
const char * MQTT_ID = "4218bf36-d589-421e-bce2-276a875be063";
int Port = 1883;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
String abc= "";
uint8_t colPins[COLS] = { 22, 4, 2, 15 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 19, 18, 5, 23 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
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];
}
Serial.println();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
delay(10);
}
void loop() {
delay(10);
char key = keypad.getKey();
if ((key != '#') && (key != NO_KEY)) {
Serial.println(key);
abc +=key;
}
if ((key=='#')&&(key !=NO_KEY)){
Serial.println(abc);
String input = String (abc);
client.publish("VLUTE/KEYPAD",String(input).c_str());
abc = "";
delay(10);
}
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop();
}
// <!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>
// <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
// <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
// <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
// <title>BAI TAP 6</title>
// <style>
// h3 {
// margin-left: 200px;
// color: #000;
// }
// .css {
// border: black 3px solid;
// }
// .full {
// width: 60%;
// margin-left: 50px;
// }
// </style>
// </head>
// <body>
// <div class="full">
// <b><i>
// <h3>ỨNG DỤNG QUẢN MẬT KHẨU </h3>
// <h3>KHÓA CỬA THÔNG MINH</h3>
// <h4>Mật khẩu hiện tại</h4>
// <p id = "pass"></p>
// </i></b>
// <b><i>
// </i></b>
// <textarea type="text" class="css" id="noidung" cols="102" rows="4"></textarea>
// <br><br>
// <button onlick="doiMatkhau()" id="gui" type="button" class="btn btn-danger">Thay đổi mật khẩu</button>
// </div>
// </body>
// <script>
// function onConnect() {
// client.subscribe("KeyPad");
// }
// var client = new Paho.MQTT.Client("broker.emqx.io", Number(8083), "client" + Math.random((10000000, 10000000)*10000000));
// client.onConnectionLost = onConnectionLost;
// client.connect({ onSuccess: onConnect });
// function onConnectionLost(responseObject) {
// if (responseObject.errorCode !== 0) {
// console.log("onConnectionLost:"+responseObject.errorMessage);
// }
// }
// function onMessageArrived(message) {
// var pass = document.getElementById("noidung").value;
// if(message.payloadString==pass){
// console.log("Mở khóa thành công");
// document.getElementById("thongbao").innerHTML="Mở khóa thành công";
// message = new Paho.MQTT.Message("success");
// message.destinationName = "Keypad";
// client.send(message);
// }else{
// document.getElementById("thongbao").innerHTML="Cảnh báo đăng nhập thất bại";
// message = new Paho.MQTT.Message("error");
// message.destinationName ="Keypad";
// client.send(message);
// }
// }
// function doiMatkhau(){
// var noidung = document.getElementById("noidung").value;
// document.getElementById("pass").innerHTML=noidung;
// }
// </script>
// </html>