De echte 'clean' versie!!!!

This commit is contained in:
Atilla
2024-03-12 20:57:53 +01:00
parent 925f73469b
commit 380764d1a6
8 changed files with 64 additions and 124 deletions

View File

@@ -1,28 +1,13 @@
import sys
import uuid
import json
from os.path import dirname, abspath, join
from paho.mqtt import subscribe
# Voeg het pad naar de 'root' directory toe aan sys.path
root_dir = dirname(dirname(abspath(__file__)))
sys.path.append(root_dir)
def on_message(client, userdata, message):
payload_str = message.payload.decode("utf-8")
data = json.loads(payload_str)
from mqtt.mqtt_client import create_client, start_loop
# Lijst waarop je je wil subscriben
mqtt_topic = "goodgarden/devices"
def on_connect(client, userdata, flags, rc):
client.subscribe(mqtt_topic)
print(f"Subscribed to {mqtt_topic}")
def on_message(client, userdata, msg):
# Decodeer de payload van bytes naar string
message = msg.payload.decode()
print(f"Message received on topic {msg.topic}: {message}")
# Hier kun je code toevoegen om iets te doen met het ontvangen bericht
print(f"Message received on topic {message.topic}: {data}")
if __name__ == "__main__":
unique_client_id = f"subscriber_{uuid.uuid4()}" # Zorg voor een unieke client ID, zodat meerdere subscribers kunnen runnen
client = create_client(unique_client_id, on_connect, on_message)
start_loop(client)
topic = "goodgarden/devices"
subscribe.callback(on_message, topic)