#include <Keypad.h>
#include <PubSubClient.h>
#include <WiFi.h>
const char* mqtt_server = "broker.hivemq.com";
const char* MQTT_ID = "fc73bd3b-4db2-4e1a-8fb7-9619fa7cddab";
int status = WL_IDLE_STATUS;
int Port = 1883;
int buzzer = 21;
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 };
uint8_t rowPins[ROWS] = { 19, 18, 5, 17 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
WiFiClient wifiClient;
PubSubClient client(wifiClient);
String pass = "";
void setup() {
Serial.begin(115200);
pinMode(buzzer, OUTPUT);
WIFIConnect();
client.setServer(mqtt_server, Port);
client.setCallback(callback);
}
void loop() {
delay(10);
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop();
char key = keypad.getKey();
if (key != NO_KEY) {
if (key != '#') {
pass = pass + key;
} else {
gui(pass);
Serial.print(pass);
pass = "";
}
}
}
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.println("MQTT connected");
client.subscribe("IOT/PhatTrien/doipass");
} 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.println("Message arrived on topic: " + String(topic));
String stMessage;
for (int i = 0; i < length; i++) {
stMessage += (char)message[i];
}
Serial.println("Message: " + stMessage);
if (stMessage == "on") {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
} else if (stMessage == "off") {
for (int i = 0; i < 3; i++) {
digitalWrite(buzzer, HIGH);
delay(100000);
digitalWrite(buzzer, LOW);
}
}
}
void gui(String a) {
client.publish("IOT/PhatTrien/doipass", a.c_str());
}
/*<!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: rgb(255, 0, 132);
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), "0000");
$(".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>*/