Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Esempio con dizionario/cache e due topic MQTT
from paho.mqtt.client import Client
import time
topic_test1 = "test/1"
topic_test2 = "test/2"
last_msg = {}
client = Client(client_id = "sub-test")
def on_connect(client, userdata, flags, rc):
print("Connesso con successo")
def on_message(client, userdata, message):
received_msg = message.payload.decode()
if message.topic == topic_test1 or message.topic == topic_test2:
last_msg[message.topic] = received_msg
print(received_msg)
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost")
client.subscribe(topic_test1)
client.subscribe(topic_test2)
client.loop_start()
while True:
print(last_msg)
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment