Refactor MQTT connection and message sending in Flask app

This commit is contained in:
ishak jmilou.ishak
2024-10-21 09:55:06 +02:00
parent 17333a6939
commit 9aec5eaaa4

View File

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