From 9cdf9aa44dfe0c7b9b16e7a437f7ec52b684b691 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Tue, 5 Nov 2024 17:06:07 +0100 Subject: [PATCH 1/6] intergrated everything and added simple endpoint for mqtt data --- src/C++/Driver/src/main.cpp | 2 ++ src/Python/flask/web/app.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/C++/Driver/src/main.cpp b/src/C++/Driver/src/main.cpp index e5c7f22..aa0c5aa 100644 --- a/src/C++/Driver/src/main.cpp +++ b/src/C++/Driver/src/main.cpp @@ -27,9 +27,11 @@ int main() setup(); std::thread safety([&]() { robot.robotSafety(&message); }); std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); }); + while(true){ parseMQTT(readMQTT()); } + sendMqtt.join(); safety.join(); return 0; } diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index e49edcf..8f5ae04 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -23,6 +23,13 @@ def move(): mqtt_client.publish("home/commands", direction) # Het topic kan aangepast worden return jsonify({"status": "success", "direction": direction}) -# Run the Flask application in debug mode + + +@app.route('/data', methods=['GET']) +def data(): + data = mqtt_client.subscribe("kobuki/data") + if data: + return jsonify({data}) + if __name__ == '__main__': app.run(debug=True) \ No newline at end of file From 5f4a7606ce3c15b7c9fe2cda685cd9dc16c68cfb Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Tue, 5 Nov 2024 17:07:40 +0100 Subject: [PATCH 2/6] attempt to fix endpoint --- src/Python/flask/web/app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 8f5ae04..4cedb6c 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -27,8 +27,9 @@ def move(): @app.route('/data', methods=['GET']) def data(): + data = 0 data = mqtt_client.subscribe("kobuki/data") - if data: + if data != 0: return jsonify({data}) if __name__ == '__main__': From 6326227be62939c2714acc6a231c263471904bf2 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Tue, 5 Nov 2024 19:11:29 +0100 Subject: [PATCH 3/6] made working endpoint for kobuki data --- src/Python/flask/web/app.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 4cedb6c..10b896b 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -3,11 +3,18 @@ import paho.mqtt.client as mqtt app = Flask(__name__) +def on_message(client, userdata, message): + global kobuki_message #set scope for this variable + kobuki_message = str(message.payload.decode("utf-8")) + # Create an MQTT client instance mqtt_client = mqtt.Client() mqtt_client.username_pw_set("ishak", "kobuki") -mqtt_client.connect("localhost", 1883, 60) +mqtt_client.connect("145.92.224.21", 1883, 60) mqtt_client.loop_start() +mqtt_client.subscribe("kobuki/data") +mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant find which function it needs to use + @app.route('/', methods=["GET","POST"]) def index(): @@ -27,10 +34,10 @@ def move(): @app.route('/data', methods=['GET']) def data(): - data = 0 - data = mqtt_client.subscribe("kobuki/data") - if data != 0: - return jsonify({data}) + return jsonify({"kobuki_message": kobuki_message}) + + + if __name__ == '__main__': app.run(debug=True) \ No newline at end of file From f95b78d236e18c729c1f9992ddad97b38a9116c2 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Wed, 6 Nov 2024 13:08:48 +0100 Subject: [PATCH 4/6] comment --- src/Python/flask/web/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 10b896b..e0be6b2 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -2,7 +2,7 @@ from flask import Flask, request, render_template, jsonify import paho.mqtt.client as mqtt app = Flask(__name__) - +# This function gets triggered when it receives a mqtt message def on_message(client, userdata, message): global kobuki_message #set scope for this variable kobuki_message = str(message.payload.decode("utf-8")) From 6dba1d026217d656a5033fb2a6d15f76c0f1b498 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Wed, 6 Nov 2024 13:59:40 +0100 Subject: [PATCH 5/6] added sensordata to website --- src/Python/flask/web/app.py | 2 +- src/Python/flask/web/static/script.js | 27 +++++++++++++++++++++-- src/Python/flask/web/templates/index.html | 2 ++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index e0be6b2..d92d5b0 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -34,7 +34,7 @@ def move(): @app.route('/data', methods=['GET']) def data(): - return jsonify({"kobuki_message": kobuki_message}) + return kobuki_message diff --git a/src/Python/flask/web/static/script.js b/src/Python/flask/web/static/script.js index c6e8451..a5f0161 100644 --- a/src/Python/flask/web/static/script.js +++ b/src/Python/flask/web/static/script.js @@ -6,7 +6,6 @@ document.querySelectorAll(".btn").forEach(button => { // Haal de waarde van de knop op const direction = event.target.value; - // Verstuur de richting naar de server met fetch fetch("/move", { method: "POST", headers: { @@ -22,4 +21,28 @@ document.querySelectorAll(".btn").forEach(button => { console.error("Error:", error); }); }); -}); + + // Fetch data from the server + async function fetchData() { + const response = await fetch("/data"); + const data = await response.json(); + return data; + } + + // Parse the data and show it on the website + async function parseData() { + const data = await fetchData(); + console.log("Fetched data:", data); // Log the fetched data + const sensorDataContainer = document.getElementById("sensor-data"); + sensorDataContainer.innerHTML = ""; // Clear previous data + + for (const [key, value] of Object.entries(data)) { + const dataElement = document.createElement("p"); + dataElement.textContent = `${key}: ${value}`; + sensorDataContainer.appendChild(dataElement); + } + } + + // Fetch and display sensor data every 5 seconds + setInterval(parseData, 5000); +}); \ No newline at end of file diff --git a/src/Python/flask/web/templates/index.html b/src/Python/flask/web/templates/index.html index eccf147..eb529e8 100644 --- a/src/Python/flask/web/templates/index.html +++ b/src/Python/flask/web/templates/index.html @@ -28,6 +28,8 @@

Sensor Data

+
+
From 89b608b759038fb0cbed62647c21975f64279370 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Wed, 6 Nov 2024 14:01:39 +0100 Subject: [PATCH 6/6] comments and cleanup --- src/Python/flask/web/static/script.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Python/flask/web/static/script.js b/src/Python/flask/web/static/script.js index a5f0161..109916d 100644 --- a/src/Python/flask/web/static/script.js +++ b/src/Python/flask/web/static/script.js @@ -32,10 +32,9 @@ document.querySelectorAll(".btn").forEach(button => { // Parse the data and show it on the website async function parseData() { const data = await fetchData(); - console.log("Fetched data:", data); // Log the fetched data const sensorDataContainer = document.getElementById("sensor-data"); sensorDataContainer.innerHTML = ""; // Clear previous data - + //for each object in json array create a new paragraph element and append it to the sensorDataContainer for (const [key, value] of Object.entries(data)) { const dataElement = document.createElement("p"); dataElement.textContent = `${key}: ${value}`;