Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions robot_control/mqtt_client.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
import { env } from "./.env.js";
import mqtt from "./libs/mqtt.js";

console.log("Mqtt library imported")
export function connect() {
let client = mqtt.connect(`ws://${env.PI_HOSTNAME}:9001`, {
username: env.MQTT_USERNAME,
password: env.MQTT_PASSWORD,
});
console.log("Connecting to MQTT broker...")
try {
let client = mqtt.connect(`ws://${env.PI_HOSTNAME}:9001`, {
username: env.MQTT_USERNAME,
password: env.MQTT_PASSWORD,
});
console.log("MQTT client created.");

client.on("connect", () => {
console.log("Connected to MQTT broker");
});
client.on("connect", () => {
console.log("Connected to MQTT broker");
});

client.on("error", (err) => {
console.log("Error connecting to MQTT broker");
console.log(err);
});
return client;
client.on("error", (err) => {
console.error("Error connecting to MQTT broker", err);
});

let original_publish = client.publish;
client.publish = (topic, message, options, callback) => {
console.log(`Publishing message to topic ${topic}:`, message);
original_publish.call(client, topic, message, options, (err, packet) => {
if (err) {
console.error("Error publishing message", err);
if (callback) callback(err, packet);
return;
}
console.log(`Message published to topic ${topic}:`, message);
if (callback) callback(err, packet);
});
};

let original_subscribe = client.subscribe;
client.subscribe = (topic, options, callback) => {
console.log(`Subscribing to topic ${topic}`);
original_subscribe.call(client, topic, options, () => {});
};

return client;
} catch (error) {
console.error("Error connecting to MQTT broker", error);
}
}

export function publish_when_clicked(client, element_id,topic, data) {
export function publish_when_clicked(client, element_id, topic, data) {
console.log("Publishing when clicked");
document.getElementById(element_id).addEventListener("click", () => {
client.publish(topic, data);
});
Expand Down