#include <WiFi.h>
#include "PubSubClient.h"
const char * MQTTServer = "broker.hivemq.com";
const char * MQTT_Topic = "GUITINHIEU";
const char * MQTT_ID = "8bd0d779-adf2-4495-99b0-c11dcf2a4ae9";
int Port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
const int green = 4;
const int yellow = 2;
const int red = 15;
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("VLUTE-IoT")) {
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];
}
int a = stMessage.substring(9,13).toInt();
Serial.print(a);
int b = stMessage.substring(24,28).toInt();
Serial.print(b);
int c = stMessage.substring(39,stMessage.length()-1).toInt();
Serial.print(c);
Serial.println();
digitalWrite(green, HIGH);
delay(b);
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(c);
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
delay(a);
digitalWrite(red, LOW);
}
void setup(){
Serial.begin(115200);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);
}
void loop(){
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>
</head>
<body>
<label>Thời gian đèn xanh bật (đơn vị là giây)</label><br>
<input type ="text" class="xanh" placeholder="Xanh">
<br>
<br>
<label>Thời gian đèn vàng bật (đơn vị là giây)</label><br>
<input type ="text" class="vang" placeholder="Vàng">
<br>
<br>
<label>Thời gian đèn đỏ bật (đơn vị là giây)</label><br>
<input type ="text" class="do" placeholder="Đỏ">
<br>
<br>
<button id="btnGui"> Gửi </button>
</body>
</html>
<script>
$('#btnGui').click(function(){
var maudo = $('.do').val()*1000;
var mauvang = $('.vang').val()*1000;
var mauxanh = $('.xanh').val()*1000;
var dulieu = {
maudo,
mauxanh,
mauvang,
};
var chuoi_json = JSON.stringify(dulieu);
//alert(chuoi_json)
message = new Paho.MQTT.Message(chuoi_json);
message.destinationName = "GUITINHIEU";
client.send(message);
});
client = new Paho.MQTT.Client("broker.hivemq.com", Number(8000), "8bd0d779-adf2-4495-99b0-c11dcf2a4ae9");
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>
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2