Created
February 1, 2022 17:22
-
-
Save Abathargh/8c3a91173d9e16b1bc1f65bd158898ba to your computer and use it in GitHub Desktop.
Esempio con dizionario/cache e due topic MQTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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