b)
#include <WiFi.h>
#include "PubSubClient.h"
#include <Keypad.h>
const char * MQTTServer = "broker.emqx.io";
const char * MQTT_Topic = "20004031";
// Tạo ID ngẫu nhiên tại: https://www.guidgen.com/
const char * MQTT_ID = "";
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 }; // 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);
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();
if (stMessage == "on") {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
}
else if (stMessage == "off") {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
}
}
String pass = "";
void setup() {
Serial.begin(115200);
pinMode(buzzer, OUTPUT);
WIFIConnect();
client.setServer(MQTTServer, 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 gui(String a){
client.publish("20004031",String(a).c_str());
}
Cau c)
code wokwi
#include <WiFi.h>
#include "PubSubClient.h"
#include <Keypad.h>
const char * MQTTServer = "broker.emqx.io";
const char * MQTT_Topic = "thuyduong";
// Tạo ID ngẫu nhiên tại: https://www.guidgen.com/
const char * MQTT_ID = "";
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 }; // 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);
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();
if (stMessage == "on") {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
}
else if (stMessage == "off") {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer,LOW);
}
}
String pass = "";
void setup() {
Serial.begin(115200);
pinMode(buzzer, OUTPUT);
WIFIConnect();
client.setServer(MQTTServer, 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 gui(String a){
client.publish("20004031",String(a).c_str());
}
// code VS
// <!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>Ung dung quan ly mat khau khoa cua thong minh</h1>
// <h3>Nhap mat khau can thay doi</h3>
// <h3>pas hien tai</h3>
// <p id ="pass"></p>
// <input type = "password" id = "mk">
// <button class="thaydoi" style="text-align: center;">Thay doi mat khau</button>
// </body>
// </html>
// <script>
// $('.thaydoi').click(function(){
// doimk();
// });
// client = new Paho.MQTT.Client("broker.emqx.io", Number(8083), "aca4dd11-00f6-4da5-9f41-5cb5af3e0c2e");
// client.onConnectionLost = onConnectionLost;
// client.onMessageArrived = onMessageArrived;
// client.connect({onSuccess:onConnect});
// function onConnect() {
// console.log("onConnect");
// client.subscribe("20004031");
// }
// function onConnectionLost(responseObject) {
// if (responseObject.errorCode !== 0) {
// console.log("onConnectionLost:"+responseObject.errorMessage);
// }
// }
// function onMessageArrived(message) {
// console.log("onMessageArrived:"+message.payloadString);
// pas = document.getElementById("mk").value;
// obj = message.payloadString;
// if( pas == obj){
// message = new Paho.MQTT.Message("on");
// message.destinationName = "thuyduong";
// client.send(message);
// }else{
// message = new Paho.MQTT.Message("off");
// message.destinationName = "thuyduong";
// client.send(message);
// }
// }
// function doimk(){
// var noidung = document.getElementById("mk").value;
// document.getElementById("pass").innerHTML = noidung;
// }
// </script>
// we