여러 주제를 게시하고 그 중 하나를 읽었습니다. 내가해야 할 일은 모든 발표 된 주제를 듣고 읽고 메시지를받는 것입니다. 이것은 내가 코드를 사용한다 :python paho를 사용하여 mqtt mosquitto에 대한 여러 주제를 구독하고 읽으십시오.
3 개 주제에 메시지를 게시 :
#!/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();
1 개 주제에 대한 메시지를 구독하고 읽을
#!/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()
로 한 번에 여러 주제를 구독 할 수 있습니다 게시 코드에 문제도있다, 당신은 클라이언트를 호출해야합니다. 루프 함수를 사용하여 각 게시간에 네트워크 스택으로 플러시되도록하거나 단일/다중 게시 기능 (https://pypi.python.org/pypi/paho-mqtt/1.1#id17) – hardillb