added sensordata to website

This commit is contained in:
2024-11-06 13:59:40 +01:00
parent f95b78d236
commit 6dba1d0262
3 changed files with 28 additions and 3 deletions

View File

@@ -34,7 +34,7 @@ def move():
@app.route('/data', methods=['GET'])
def data():
return jsonify({"kobuki_message": kobuki_message})
return kobuki_message

View File

@@ -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);
});

View File

@@ -28,6 +28,8 @@
</div>
<div class="container">
<h1>Sensor Data</h1>
<div id="sensor-data">
</div>
</div>
<script src="../static/script.js"></script>
</body>