From f2287a9dba3748ab0e4259c422adbe8cdc2836e2 Mon Sep 17 00:00:00 2001 From: derlole <122916573+derlole@users.noreply.github.com> Date: Wed, 16 Apr 2025 13:21:10 +0000 Subject: [PATCH] test mqtt --- server.py | 74 ++++++++++++++++++++++---------- templates/dev/index.html | 84 +++++++++++++++++++++++++++++++++++++ templates/index.html | 91 ++++++++-------------------------------- 3 files changed, 155 insertions(+), 94 deletions(-) create mode 100644 templates/dev/index.html diff --git a/server.py b/server.py index 6f7628f..2a95b58 100644 --- a/server.py +++ b/server.py @@ -1,33 +1,65 @@ -from flask import Flask -from flask_socketio import SocketIO, emit -import threading -import time +# from flask import Flask +# from flask_socketio import SocketIO, emit +# import threading +# import time from routes.unsecure_routes import unsecure +# app = Flask(__name__, static_url_path='/unsecure/static') +# # socketio = SocketIO(app, cors_allowed_origins="*") + +# # data = { +# # "test": 1, +# # "status": "online", +# # "counter": 0 +# # } + +# # def monitor_changes(): +# # while True: +# # time.sleep(5) # alle 5 Sekunden neue Daten senden +# # data["test"] += 1 +# # data["counter"] += 5 +# # data["status"] = "busy" if data["counter"] % 2 == 0 else "idle" + +# # print("Neue Daten gesendet:", data) +# # socketio.emit('update_data', data) + + +# # Blueprint registrieren +# app.register_blueprint(unsecure) + +# if __name__ == '__main__': +# # threading.Thread(target=monitor_changes).start() +# app.run(host='0.0.0.0', port=3060) + + +from flask import Flask, render_template +from flask_socketio import SocketIO +import paho.mqtt.client as mqtt + app = Flask(__name__, static_url_path='/unsecure/static') -# socketio = SocketIO(app, cors_allowed_origins="*") +socketio = SocketIO(app, cors_allowed_origins="*") -# data = { -# "test": 1, -# "status": "online", -# "counter": 0 -# } +# MQTT Setup +mqtt_client = mqtt.Client() -# def monitor_changes(): -# while True: -# time.sleep(5) # alle 5 Sekunden neue Daten senden -# data["test"] += 1 -# data["counter"] += 5 -# data["status"] = "busy" if data["counter"] % 2 == 0 else "idle" +def on_mqtt_message(client, userdata, msg): + payload = msg.payload.decode() + print(f"[MQTT] {msg.topic}: {payload}") + socketio.emit("esp_update", {"topic": msg.topic, "payload": payload}) -# print("Neue Daten gesendet:", data) -# socketio.emit('update_data', data) +mqtt_client.on_message = on_mqtt_message +mqtt_client.connect("localhost", 1883) +mqtt_client.subscribe("lires/esp1/status") +mqtt_client.loop_start() +# Socket.IO → MQTT +@socketio.on("send_to_esp") +def handle_send(data): + mqtt_client.publish("lires/esp1/control", data) -# Blueprint registrieren app.register_blueprint(unsecure) if __name__ == '__main__': - # threading.Thread(target=monitor_changes).start() - app.run(host='0.0.0.0', port=3060) + socketio.run(app, host="0.0.0.0", port=3060) + diff --git a/templates/dev/index.html b/templates/dev/index.html new file mode 100644 index 0000000..99cd22d --- /dev/null +++ b/templates/dev/index.html @@ -0,0 +1,84 @@ + + + + + + + gimmiCoffee + + + +
+
gimmiCoffee
+
+ Max Mustermann + Logout +
+
+ +
+ +
+
ESP-Conn Infos
+
MAKE ME A COFFEE...
+
+
+
Maschiene
+
AUS
+
+
+
Maschiene
+
Nicht Bereit
+
+
+
irgendwas kommt hier nocht
+
XXX
+
+
+
Fehler
+
Wasser leer?
+
+
+
Wasser
+
Aufgefüllt
+
+
+
Bohnen
+
Nachgefüllt
+
+
+
+ +
+

Statistiken

+
+
Coffee made
+
XXX
+
+
+
Bohnen Füllstand
+
XX%
+
+
+
Wasser Füllstand
+
XX%
+
+
+
Bohnen nachgefüllt
+
XX
+
+
+
Wasser nachgefüllt
+
XX
+
+
+ +
+ + + + + + + + diff --git a/templates/index.html b/templates/index.html index 99cd22d..3fec6e4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,84 +1,29 @@ - - - gimmiCoffee - + ESP Kontrolle -
-
gimmiCoffee
-
- Max Mustermann - Logout -
-
+

ESP Live-Daten

+
Warte auf Daten...
+
+ + -
+ + - - - - - -