2017-09-13 5 views
0

여러 주제를 게시하고 그 중 하나를 읽었습니다. 내가해야 할 일은 모든 발표 된 주제를 듣고 읽고 메시지를받는 것입니다. 이것은 내가 코드를 사용한다 :python paho를 사용하여 mqtt mosquitto에 대한 여러 주제를 구독하고 읽으십시오.

  1. 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(); 
    
  2. 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() 
    
+0

로 한 번에 여러 주제를 구독 할 수 있습니다 게시 코드에 문제도있다, 당신은 클라이언트를 호출해야합니다. 루프 함수를 사용하여 각 게시간에 네트워크 스택으로 플러시되도록하거나 단일/다중 게시 기능 (https://pypi.python.org/pypi/paho-mqtt/1.1#id17) – hardillb

답변

0

당신은 client.subscribe()를 호출 할 수 있습니다 기능을 여러 번 사용하여 여러 주제에 가입하십시오.

또한 첫 번째 루프의 필요성을 제거하려면 on_connect 콜백에 대한 가입 호출을 이동해야합니다.

#!/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") 
     client.subscribe("topic/1") 
     client.subscribe("topic/2") 
     client.subscribe("topic/3") 
     client.subscribe("topic/4") 

    else: 
     print("Connection failed") 

def on_message(client, userdata, message): 
    print("Message received : " + str(message.payload) + " on " + message.topic) 


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() 


try: 
    while True: 
     time.sleep(1) 

except KeyboardInterrupt: 
    print("exiting") 
    client.disconnect() 
    client.loop_stop() 

편집 :

당신은 또한 다음과 같은 구문

client.subscribe([("topic/1", 0), ("topic/2", 0), ("topic/3", 0),("topic/4", 0)]) 
+0

을 사용하여 주셔서 감사합니다. . 내가 주제/1에 대한 답장 만 받기 때문에 예상대로 작동하지 않는 것 같습니다. 나는 이유를 알아 내려고 노력할 것이다. 그러나 다시 감사한다 –

+0

나는 여러 가지 주제를 구독하는 다른 방법을 추가하기 위해 그것을 편집했다. – hardillb

+0

아무도 downvote에 대한 이유를주고 싶어? – hardillb