mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
include paho mqtt
This commit is contained in:
@@ -2,19 +2,30 @@ from flask import Flask
|
|||||||
from flask_mqtt import Mqtt
|
from flask_mqtt import Mqtt
|
||||||
|
|
||||||
app = Flask(__name__)
|
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):
|
# MQTT configuratie
|
||||||
command = msg.payload.decode()
|
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}")
|
print(f"Received command: {command}")
|
||||||
# Voeg hier je code toe om het commando uit te voeren
|
# Voeg hier je code toe om het commando uit te voeren
|
||||||
|
|
||||||
client = mqtt.Client()
|
# Start de Flask applicatie
|
||||||
client.on_connect = on_connect
|
if __name__ == '__main__':
|
||||||
client.on_message = on_message
|
app.run(host='0.0.0.0', port=5000)
|
||||||
|
|
||||||
client.connect("145.109.226.204", 83, 60)
|
|
||||||
client.loop_forever()
|
|
||||||
|
Reference in New Issue
Block a user