1

在我的树莓派上安装 paho mqtt 客户端并将我的 android 应用程序和 arduino 与我的代理连接后。我希望在收到来自应用程序或 arduino 客户端的消息后将此消息重新发布到此客户端,例如,如果我在收到“OFF”后收到“ON”,下一次发布将是“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))
4

3 回答 3

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()
于 2016-01-25T13:32:11.933 回答
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()
于 2015-08-03T14:12:02.173 回答
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()

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

于 2016-01-20T18:26:59.437 回答