diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 7d07c0a..085eba0 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -1,4 +1,4 @@ -from flask import Flask, request, render_template +from flask import Flask, request, render_template, jsonify import paho.mqtt.client as mqtt app = Flask(__name__) @@ -10,15 +10,18 @@ mqtt_client.username_pw_set("ishak", "kobuki") mqtt_client.connect("localhost", 1883, 60) mqtt_client.loop_start() -@app.route('/', methods=['GET', 'POST']) +@app.route('/') def index(): - message = '' - if request.method == 'POST': - direction = request.form['direction'] - result = mqtt_client.publish("home/commands", direction) - if result.rc == mqtt.MQTT_ERR_SUCCESS: - message = "Bericht succesvol gepubliceerd" - return render_template('index.html', message=message) + return render_template('index.html') + +@app.route('/move', methods=['POST']) +def move(): + direction = request.form['direction'] + result = mqtt_client.publish("home/commands", direction) + if result.rc == mqtt.MQTT_ERR_SUCCESS: + return jsonify({"message": "Bericht succesvol gepubliceerd"}), 200 + else: + return jsonify({"message": "Fout bij het publiceren van bericht"}), 500 if __name__ == '__main__': - app.run(debug=True) + app.run(debug=True) \ No newline at end of file diff --git a/src/Python/flask/web/static/script.js b/src/Python/flask/web/static/script.js new file mode 100644 index 0000000..1e1f33c --- /dev/null +++ b/src/Python/flask/web/static/script.js @@ -0,0 +1,19 @@ +document.querySelectorAll('button').forEach(button => { + button.addEventListener('click', function() { + const direction = this.value; + fetch('/move', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams({ direction: direction }), + }) + .then(response => response.json()) + .then(data => { + document.getElementById('response').innerText = data.message; + }) + .catch(error => { + document.getElementById('response').innerText = 'Er is een fout opgetreden: ' + error; + }); + }); +}); \ 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 28a99f2..cb9f211 100644 --- a/src/Python/flask/web/templates/index.html +++ b/src/Python/flask/web/templates/index.html @@ -1,18 +1,30 @@ {%extends 'base.html'%} {%block head%} {%endblock%} {%block content%} -
-
- Kobuki Robot -
-
-
- - - - -
-
-
-
-

Sensor Data

-
+ + + + + + Kobuki + + +
+
+ Kobuki Robot +
+
+
+ + + + +
+
+
+
+

Sensor Data

+
+ + + + {%endblock%}