出版后,Python Mqtt-Paho断开连接



我试图将单个消息发布到MQTT,并与以下代码断开连接。但是它有时可以工作,有时无法正常工作。我想收听一个主题,以说明Switch1 OS打开,然后关闭,然后根据接收到的数据和断开连接打开。

#!/usr/bin/env python2.7
import json
import time
import os
import paho.mqtt.client as mqtt
mqtt_host = os.getenv('HOST', 'xxxx')
mqtt_port = os.getenv('PORT', 1883)
mqtt_username = os.getenv('USERNAME', 'xxxx')
mqtt_password = os.getenv('PASSWORD', 'xxxx')
mqtt_subacribe_topic = os.getenv('SUBSCRIBE_TOPIC', 'xxxx')
mqtt_publish_topic = os.getenv('PUBLISH_TOPIC', 'xxxx')
sleep_time = os.getenv('SLEEP_TIME', 15)
CLIENT_ID = "lambda"
SWITCH1_ON = { "SWITCH1": "on" }
SWITCH1_OFF = { "SWITCH1": "off" }
def on_publish(client, userdata, mid):
    print ("Message Published...")
    client.disconnect()
def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: " + str(message.topic) + " " + str(mid) + " " + str(granted_qos))
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to broker")
        client.subscribe(mqtt_subacribe_topic)
    else:
        print("Connection failed")
def on_message(client, userdata, msg):
    payload = json.loads(msg.payload)
    if payload.get('switch1') == 1:
        client.publish(mqtt_publish_topic,json.dumps(SWITCH1_ON))
    elif payload.get('switch1') == 0:
        client.publish(mqtt_publish_topic,json.dumps(SWITCH1_OFF))
def main():
    client = mqtt.Client(CLIENT_ID)
    client.username_pw_set(mqtt_username, password=mqtt_password)
    # Register publish callback function
    client.on_publish = on_publish
    client.on_connect = on_connect
    client.on_message = on_message
    # Connect with MQTT Broker
    client.connect(mqtt_host, port=mqtt_port)
    # Loop forever
    client.loop_start()
    time.sleep(sleep_time)
    client.loop_stop()
    client.disconnect()
if __name__ == "__main__":
    main()

基于 Hardillb 的答案,我尝试了:

#!/usr/bin/env python2.7
import json
import time
import os
import paho.mqtt.subscribe as subscribe
import paho.mqtt.publish as publish
mqtt_host = os.getenv('HOST', 'xxx.cloudmqtt.com')
mqtt_port = os.getenv('PORT', 1883)
mqtt_username = os.getenv('USERNAME', 'xxx')
mqtt_password = os.getenv('PASSWORD', 'xxx')
mqtt_subacribe_topic = os.getenv('SUBSCRIBE_TOPIC', 'xxx')
mqtt_publish_topic = os.getenv('PUBLISH_TOPIC', 'xxx')
sleep_time = os.getenv('SLEEP_TIME', 14)
CLIENT_ID = "lambda"
SWITCH1_ON = { "SWITCH1": "on" }
SWITCH1_OFF = { "SWITCH1": "off" }
auth = {'username':mqtt_username, 'password':mqtt_password}
def on_message(client, userdata, msg):
    payload = json.loads(msg.payload)
    print(payload)
    if payload.get('switch1') == 1:
        publish.single(mqtt_publish_topic,json.dumps(SWITCH1_ON),hostname=mqtt_host,auth=auth)
        print "Turning switch1 ON"
    elif payload.get('switch1') == 0:
        publish.single(mqtt_publish_topic,json.dumps(SWITCH1_OFF),hostname=mqtt_host,auth=auth)
        print "Turning switch1 OFF"
def main():
    subscribe.callback(on_message, mqtt_subacribe_topic,hostname=mqtt_host,auth=auth)
if __name__ == "__main__":
    main()

但是脚本一直在运行,我必须杀死它才能停止。是否可以仅订阅一个主题,一旦接收到第一消息,将其处理,将其发布到另一个主题并结束执行。

如果您只想发布一条消息,则PAHO客户端具有内置方法来执行此操作。您可以在此处找到文档

import paho.mqtt.publish as publish
publish.single("paho/test/single", "payload", hostname="iot.eclipse.org")

也有一种等效的方法,还可以订阅主题并接收单个消息。

相关内容

  • 没有找到相关文章

最新更新