24 lines
630 B
JavaScript
24 lines
630 B
JavaScript
const mqtt = require('mqtt');
|
|
|
|
const PUB_TOPIC = "test-sub";
|
|
const MESSAGE = "Message from server";
|
|
|
|
const options = {
|
|
clientId: 'test_device2',
|
|
username: 'test_user',
|
|
password: 'test_pass',
|
|
};
|
|
|
|
const client = mqtt.connect('mqtt://localhost:1883', options);
|
|
|
|
client.on('connect', function () {
|
|
console.log('Connected to MQTT broker');
|
|
setInterval(function () {
|
|
client.publish(PUB_TOPIC, MESSAGE);
|
|
console.log(`Published: ${MESSAGE} to ${PUB_TOPIC}`);
|
|
}, 10000); // 30秒ごとにパブリッシュ
|
|
});
|
|
|
|
client.on('error', function (err) {
|
|
console.error('MQTT client error:', err);
|
|
}); |