0

just want to test a simple browser application to send mqtt messages to my broker by clicking on a browser button. I found https://gist.github.com/narutaro/6461c0524f7d7ff01e21c2ecb0be84ca I am really new to those protocols like HTTP, MQTT, Javascript, HTML and so on.

Using Ubuntu...

But I dont know how to run it. I subscribe the topic "/test" and put in the brokers ip <IP:PORT> (the x' es are of course changed in my real code 192.xxx.xxx.xxx:1883) It has no password.

Normally I should receive something. But i dont receive anything. What could be the reason?

When subscribing with mosquitto_sub -h -t /test I can see the normal traffic


\<!DOCTYPE html\>
\<html lang="en"\>

\<head\>
\<meta charset="utf-8"\>
\<title\>MQTT DASH\</title\>.
\<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"\>\</script\>
\</head\>

\<body\>

\<script\>
**let client = mqtt.connect('192.xxx.xxx.xxx:1883');**
let topic = "/test"

client.subscribe(topic);

pubLoop = setInterval(() =\> {
const time = Date.now().toString();
const temp = Math.floor( Math.random() \* 11 );
let metric = '{"time":' + time + ', "name": "sensor1", "temp":' + temp + '}'
client.publish(topic, metric);
}, 1000);

client.on('message', function (topic, message) { // message is Buffer
console.log(message.toString());
});

setTimeout(function() { clearInterval( pubLoop ); }, 10000); // stop after 10sec

\</script\>

\</body\>
\</html\>

Thank you for advices :)

0 Answers0