#include "ArduinoJson.h"
#include <WiFi.h>
#include <Keypad.h>
#include "PubSubClient.h"
const char * MQTTServer = "broker.hivemq.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 = "08538910-a63d-45af-ab5b-3ca7131928c4";
int Port = 1883;
#define ROWS 4
#define COLS 4
char keyMap[ROWS][COLS] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
uint8_t rowPins[ROWS] = {14, 27, 26, 25}; // GIOP14, GIOP27, GIOP26, GIOP25
uint8_t colPins[COLS] = {33, 32, 18, 19}; // GIOP33, GIOP32, GIOP18, GIOP19
Keypad keypad = Keypad(makeKeymap(keyMap), 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);
String stMessage;
char receivedMessage[length + 1];
for (int i = 0; i < length; i++) {
receivedMessage[i] = (char)message[i];
}
receivedMessage[length] = '\0';
}
void setup() {
Serial.begin(115200);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
}
void loop() {
delay(100);
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>
</head>
<body>
<h1>Ung dung quan ly man hinh thong bao thong minh</h1>
<h4>Nhap noi dung can hien thi (tieng viet khong dau)</h4>
<input type="text" name="" id="text" class="">
<input type="button" value="" class="button" onclick="bam()">
</body>
</html>
<script>
client = new Paho.MQTT.Client("broker.hivemq.com", Number(8000), "0147852369856321");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({
onSuccess: onConnect
});
function onConnect() {
console.log("onConnect");
client.subscribe("IOT/PhatTrien/HienThi");
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
function onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
}
function bam() {
var text1 = document.getElementById("text").value;
message = new Paho.MQTT.Message(String(text1));
message.destinationName = "IOT/PhatTrien/HienThi";
client.send(message);
}
</script>
*/