Created
October 17, 2022 04:17
-
-
Save truongtpa/138b6ce30dd1f8c5b98f5e28a1cd637a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> | |
| <button class="bat">Bật LED</button> | |
| <button class="tat">Tắt LED</button> | |
| </body> | |
| </html> | |
| <script> | |
| $('.bat').click(function(){ | |
| message = new Paho.MQTT.Message("on"); | |
| message.destinationName = "VLUTE/LED"; | |
| client.send(message); | |
| }); | |
| $('.tat').click(function(){ | |
| message = new Paho.MQTT.Message("off"); | |
| message.destinationName = "VLUTE/LED"; | |
| client.send(message); | |
| }); | |
| client = new Paho.MQTT.Client("broker.emqx.io", Number(8083), "clientId"); | |
| client.onConnectionLost = onConnectionLost; | |
| client.onMessageArrived = onMessageArrived; | |
| client.connect({onSuccess:onConnect}); | |
| function onConnect() { | |
| console.log("onConnect"); | |
| // client.subscribe("World"); | |
| } | |
| function onConnectionLost(responseObject) { | |
| if (responseObject.errorCode !== 0) { | |
| console.log("onConnectionLost:"+responseObject.errorMessage); | |
| } | |
| } | |
| function onMessageArrived(message) { | |
| console.log("onMessageArrived:"+message.payloadString); | |
| } | |
| </script> |
Author
Author
var anh_bat = "https://i.imgur.com/pPqLQx4.png";
var anh_tat = "https://i.imgur.com/yRMydZv.png";
$('.anh').click(function(){
var anhht = $('.anh').attr('src');
if(anhht === anh_bat){
$('.anh').attr('src', anh_tat);
message = new Paho.MQTT.Message("off");
message.destinationName = "VLUTE/LED";
client.send(message);
}
else{
$('.anh').attr('src', anh_bat);
message = new Paho.MQTT.Message("on");
message.destinationName = "VLUTE/LED";
client.send(message);
}
});
Author
Gửi dữ liệu đi
message = new Paho.MQTT.Message("off");
message.destinationName = "VLUTE/LED";
client.send(message);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tạo một file
index.htmlvà dán đoạn chương trình trên vào. Sau đó mởindex.htmlbằng trình duyệt WEB