python mqtt-paho发布后断开连接

时间:2019-03-09 21:08:39

标签: python mqtt paho

我正在尝试将一条消息发布到 c1 0.0 c2 0.0 c3 -100.0 c4 -100.0 c1 0.0 c2 0.0 c3 -100.0 c4 -100.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c1 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c2 0.0 c3 -100.0 c3 -73.76543209876543 c3 -50.617283950617285 c3 -56.79012345679013 c3 -62.96296296296296 c3 -69.1358024691358 c3 -75.30864197530863 c3 -81.4814814814815 c3 -87.65432098765432 c3 -93.82716049382715 c3 -98.45679012345678 c4 -100.0 c4 -72.22222222222223 c4 -44.44444444444445 c4 -44.44444444444445 c4 -44.44444444444445 c4 -44.444444444444436 c4 -44.44444444444445 c4 -44.44444444444448 c4 -44.44444444444445 c4 -44.44444444444442 c4 -72.22222222222221 Singular matrix C in LSQ subproblem (Exit mode 6) Current function value: 4225000.0 Iterations: 1 Function evaluations: 12 Gradient evaluations: 1 fun: 4225000.0 jac: array([1800., 1800., 1800., 1800., 1800., 1800., 1800., 1800., 1800., 1800.]) message: 'Singular matrix C in LSQ subproblem' nfev: 12 nit: 1 njev: 1 status: 6 success: False x: array([650., 650., 650., 650., 650., 650., 650., 650., 650., 650.]) ,并与以下代码断开连接。但是它有时起作用,有时不能按预期起作用。我想听一个话题,如果switch1 os打开然后关闭,则根据接收到的数据打开它,然后断开连接。

MQTT

根据 hardillb 的回答,我尝试了:

#!/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()

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

1 个答案:

答案 0 :(得分:1)

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

import paho.mqtt.publish as publish

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

有一种equivalent方法也可以订阅主题并接收一条消息。