This commit is contained in:
derlole
2025-04-16 16:13:10 +00:00
parent 413890699a
commit 45dd394b60

View File

@@ -46,7 +46,6 @@ socketio = SocketIO(app, cors_allowed_origins="*")
# MQTT Setup # MQTT Setup
mqtt_client = mqtt.Client(protocol=mqtt.MQTTv5) mqtt_client = mqtt.Client(protocol=mqtt.MQTTv5)
# Funktion für den Empfang von MQTT-Nachrichten
def on_mqtt_message(client, userdata, msg): def on_mqtt_message(client, userdata, msg):
payload = msg.payload.decode() payload = msg.payload.decode()
print(f"[MQTT] {msg.topic}: {payload}") print(f"[MQTT] {msg.topic}: {payload}")
@@ -55,18 +54,14 @@ def on_mqtt_message(client, userdata, msg):
mqtt_client.on_message = on_mqtt_message mqtt_client.on_message = on_mqtt_message
mqtt_client.connect("localhost", 1883) # MQTT-Broker-Adresse mqtt_client.connect("localhost", 1883) # MQTT-Broker-Adresse
mqtt_client.subscribe("lires/esp1/status") # MQTT-Topic, das abonniert werden soll mqtt_client.subscribe("lires/esp1/status")
mqtt_client.loop_start() # Starten des MQTT-Client-Loops in einem separaten Thread mqtt_client.loop_start()
# Socket.IO → MQTT: Empfangene Daten von Socket.IO an MQTT senden
@socketio.on("send_to_esp") @socketio.on("send_to_esp")
def handle_send(data): def handle_send(data):
mqtt_client.publish("lires/esp1/control", data) # MQTT-Message senden mqtt_client.publish("lires/esp1/control", data)
# Blueprint registrieren
app.register_blueprint(unsecure) app.register_blueprint(unsecure)
# Starten der Flask-SocketIO-Anwendung
if __name__ == '__main__': if __name__ == '__main__':
# Verwende socketio.run() statt app.run() für asynchrone WebSocket-Kommunikation
socketio.run(app, host="0.0.0.0", port=3060) socketio.run(app, host="0.0.0.0", port=3060)