Skip to content

Instantly share code, notes, and snippets.

@Abathargh
Created February 1, 2022 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abathargh/8c3a91173d9e16b1bc1f65bd158898ba to your computer and use it in GitHub Desktop.
Save Abathargh/8c3a91173d9e16b1bc1f65bd158898ba to your computer and use it in GitHub Desktop.
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