I managed to publish on several topics and read one of them. What I need to do is to listen and read to all published topics and get messages. This is the code I use:
Publish messages to 3 topics :
#!/usr/bin/env python3 import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("localhost",1883,60) client.publish("topic/1", "400 | 350 | 320 | 410"); client.publish("topic/2", "200 | 350 | 420 | 110"); client.publish("topic/3", "200 | 350 | 420 | 110"); client.disconnect();
Subscribe and read messages for 1 topic
#!/usr/bin/env python3 import paho.mqtt.client as mqttClient import time def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to broker") global Connected #Use global variable Connected = True #Signal connection else: print("Connection failed") def on_message(client, userdata, message): print "Message received : " + message.payload Connected = False broker_address= "localhost" port = 1883 client = mqttClient.Client("Python") client.on_connect= on_connect client.on_message= on_message client.connect(broker_address, port=port) client.loop_start() while Connected != True: time.sleep(0.1) client.subscribe("topic/2") try: while True: time.sleep(1) except KeyboardInterrupt: print "exiting" client.disconnect() client.loop_stop()