Remote Sensor via JavaScript MQTT WebSocket

by Henrique Gaspar and Annie Bekker, 18 Jan 2025

Subscribed to:

Host/Broker:
Topic:

Connection Log:



Messages:


Lessons for short-term memory disabled parents:

1: Running from terminal can be useful with "watch", via the code:
watch -n 1 'd=$(date) && mosquitto_pub -h broker.emqx.io -p 1883 -t train_sensor -m "$d"

2: We can use the JavaScript console to send a message to the broker/ host via:
i) Creating a new object "message" with the command:
message = new Paho.MQTT.Message("your message here")
ii) Including the topic in the property .destinationName
message.destinationName = "your_topic_here"
iii) Send the message with:
mqtt.send(message)

3: For a loop that send data every 1000 miliseconds, just put in the terminal the following:
setInterval(function () { d= Date(); message = new Paho.MQTT.Message(d); message.destinationName = "train_sensor" ;mqtt.send(message)}, 1000);

Based on the tutorials from Stephen Cope.