De echte 'clean' versie!!!!
This commit is contained in:
@@ -1,24 +1,13 @@
|
||||
import sys
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from os.path import dirname, abspath, join
|
||||
from paho.mqtt import subscribe
|
||||
|
||||
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
|
||||
|
||||
mqtt_topic = "goodgarden/soil_electric_conductivity"
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(mqtt_topic)
|
||||
print(f"Subscribed to {mqtt_topic}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
message = msg.payload.decode()
|
||||
print(f"Message received on topic {msg.topic}: {message}")
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unique_client_id = f"subscriber_{uuid.uuid4()}"
|
||||
client = create_client(unique_client_id, on_connect, on_message)
|
||||
start_loop(client)
|
||||
topic = "goodgarden/soil_electric_conductivity"
|
||||
subscribe.callback(on_message, topic)
|
||||
Reference in New Issue
Block a user