使用Paho Mqtt客户端发布和订阅

时间:2015-08-02 18:56:02

标签: python raspberry-pi mqtt paho

在我的覆盆子pi上安装paho mqtt客户端并将我的android应用程序和arduino连接到我的经纪人之后。我希望在收到来自应用程序的消息后,或者arduino客户端将此消息重新发布给此客户,例如,如果我收到" ON"在我收到" OFF"之后,下一次发布将是" ON" " OFF" " ON" " OFF" ...或者我需要只是" ON"或" OFF"

import paho.mqtt.client as mqtt
message = 'ON'
def on_connect(mosq, obj, rc):
    print("rc: " + str(rc))

def on_message(mosq, obj, msg):
    global message
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
    message = msg.payload

def on_publish(mosq, obj, mid):
    print("mid: " + str(mid))

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(mosq, obj, level, string):
    print(string)

mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Connect
mqttc.connect("localhost", 1883,60)

# Start subscribe, with QoS level 0
mqttc.subscribe("f", 0)

# Publish a message
#mqttc.publish("hello/world", "my message")

# Continue the network loop, exit when an error occurs
rc = 0
while rc == 0:
   rc = mqttc.loop()
   mqttc.publish("f",message)
print("rc: " + str(rc))

3 个答案:

答案 0 :(得分:2)

您可以学习此代码并自行使用,

import RPi.GPIO as GPIO
import time
import paho.mqtt.client as mqtt

GPIO.setmode(GPIO.BOARD)

GPIO.setup(13,GPIO.IN)

mqttc=mqtt.Client()
mqttc.connect("iot.eclipse.org",1883,60)
mqttc.loop_start()

def reading1():
    a=GPIO.input(13)
    print(a)
    return a

while 1:
    t=reading1()
    (result,mid)=mqttc.publish("paho/temp_ab",t,2)
    time.sleep(1)

mqttc.loop_stop()
mqttc.disconnect()

答案 1 :(得分:1)

行,

以下代码将订阅主题 f 并重新发布主题 f2

import paho.mqtt.client as mqtt
message = 'ON'
def on_connect(mosq, obj, rc):
    mqttc.subscribe("f", 0)
    print("rc: " + str(rc))

def on_message(mosq, obj, msg):
    global message
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
    message = msg.payload
    mqttc.publish("f2",msg.payload);

def on_publish(mosq, obj, mid):
    print("mid: " + str(mid))

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(mosq, obj, level, string):
    print(string)

mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Connect
mqttc.connect("localhost", 1883,60)


# Continue the network loop
mqttc.loop_forever()

答案 2 :(得分:0)

我认为这可能会对你有所帮助。

import threading
import paho.mqtt.client as mqtt

def publish_1(client,topic):
    message="on"
    print("publish data")
    client.publish(topic,message)
    publish_1(client,topic)


broker="test.mosquitto.org"
topic_pub='/temperature123'
topic_sub='$SYS/#'

def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    client.subscribe(topic_sub)


def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect(broker, 1883, 60)
thread1=threading.Thread(target=publish_1,args=(client,topic_pub))
thread1.start()

client.loop_forever()

该程序使用线程并传递客户端对象。 但这有问题,这是递归发布的。