mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-04 12:24:57 +00:00
include paho mqtt
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user