-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonMQTT.py
More file actions
45 lines (34 loc) · 1.2 KB
/
Copy pathPythonMQTT.py
File metadata and controls
45 lines (34 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from nicegui import ui
import paho.mqtt.client as mqtt
import json
# Layout
with ui.card().classes('w-96 mx-auto mt-20 p-6 shadow-2xl'):
ui.label('🌡️ Room Monitor').classes('text-2xl font-bold text-center mb-4')
temp_label = ui.label('Temp: -- °C').classes('text-xl text-center')
hum_label = ui.label('Humidity: -- %').classes('text-xl text-center')
status_label = ui.label('🔴 Disconnected').classes('text-center mt-2')
# MQTT Callback
def on_connect(client, userdata, flags, rc):
if rc == 0:
status_label.set_text('🟢 Connected')
else:
status_label.set_text('🔴 Error')
def on_message(client, userdata, msg):
data = json.loads(msg.payload)
temp = data["temp"]
hum = data["hum"]
temp_label.set_text(f'🌡️ Temp: {temp:.1f} °C')
hum_label.set_text(f'💧 Humidity: {hum:.1f} %')
# Farbe je nach Temperatur
if temp > 30:
temp_label.style('color: red')
else:
temp_label.style('color: black')
# MQTT Setup
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.emqx.io", 1883)
client.subscribe("room/data")
client.loop_start()
ui.run(title="Roomwatch Dashboard")