From 9aec5eaaa47608d9e21099a5a4741043748815d6 Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Mon, 21 Oct 2024 09:55:06 +0200 Subject: [PATCH] Refactor MQTT connection and message sending in Flask app --- src/Python/flask/web/app.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 21073ea..33c2679 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -1,17 +1,21 @@ from flask import Flask, request, render_template -import paho.mqtt.publish as publish +import paho.mqtt.client as mqtt app = Flask(__name__) +mqtt_client = mqtt.Client() + +# Verbinding maken met de Raspberry Pi (broker) +mqtt_client.connect("ishakpi.ddns.net", 1883, 60) + @app.route('/') def index(): return render_template('index.html') @app.route('/move', methods=['POST']) def move(): - direction = request.form['direction'] - publish.single("move", direction, hostname="145.109.226.204") - return "Message sent" + mqtt_client.publish("website/knop", "Knop ingedrukt!") + return "Bericht verzonden!" if __name__ == '__main__': app.run(debug=True) \ No newline at end of file