include paho mqtt

This commit is contained in:
ishak jmilou.ishak
2024-10-21 10:49:57 +02:00
parent 2dadabba18
commit 82bc53e2c7

View File

@@ -2,19 +2,30 @@ from flask import Flask
from flask_mqtt import Mqtt
app = Flask(__name__)
mqtt = Mqtt(app)
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("move")
def on_message(client, userdata, msg):
command = msg.payload.decode()
# MQTT configuratie
app.config['MQTT_BROKER_URL'] = '145.109.226.204'
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = 'ishak'
app.config['MQTT_PASSWORD'] = 'kobuki'
app.config['MQTT_KEEPALIVE'] = 60
app.config['MQTT_TLS_ENABLED'] = False # TLS uitschakelen indien niet nodig
mqtt = Mqtt(app)
# Callback voor wanneer de verbinding tot stand komt
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
mqtt.subscribe("move")
# Callback voor wanneer een bericht binnenkomt
@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
command = message.payload.decode()
print(f"Received command: {command}")
# Voeg hier je code toe om het commando uit te voeren
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("145.109.226.204", 83, 60)
client.loop_forever()
# Start de Flask applicatie
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)