带有MQTT的Python Flask应用 [英] Python Flask App with MQTT

查看:897
本文介绍了带有MQTT的Python Flask应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将Flask应用程序用作订户,但在收到消息时它不会调用on_message回调.相反,我将得到如下所示的信息:

I'm trying to use my Flask app as a subscriber, but it does not call the on_message callback when receiving a message. Instead, I get about like the following:

Connected with result code 0
Closing data file...
Connected with result code 0
Closing data file...

这是我运行Flask应用程序的方式:

This is how I am running the Flask app:

main.py:

from flask import Flask, render_template, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *
import paho.mqtt.client as mqtt
import time

broker_address = <broker_ip>
port = 1883
timeout = 60
username = "first"
password = "last"
uuid = "1234"
topic = "mytopic"
qos = 0



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

    client.subscribe(topic)

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

def on_disconnect(client, userdata, rc):
    print("Closing data file...")

client = mqtt.Client(client_id=uuid)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.username_pw_set(username, password)

client.connect(broker_address, port, 60)
client.loop_start()

<other Flask code>

if __name__ == "__main__":
    app.run(debug=True)

我尝试使用另一个Python脚本生成一些假数据发布到该主题,但是只有在该脚本运行时,我才获得上面的输出.如果该脚本未运行,则main.py似乎正在等待消息.这是另一个脚本:

I've tried using a another Python script to generate some fake data to publish to the topic, but only when that script is running to I get the output above. If that script is not running, then the main.py seems to wait for messages. This is the other script:

fake_data.py:

import paho.mqtt.client as mqtt
import time

broker_address = <broker_ip>
port = 1883
timeout = 60
username = "first"
password = "last"
uuid = "1234"
topic = "mytopic"
qos = 0



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

client = mqtt.Client(client_id=uuid, clean_session=True)
client.on_connect = on_connect

client.username_pw_set(username, password)

client.connect(broker_address, port, 60)


client.loop_start()

while True:
    count = 0
    while count != 30:
        print("Publishing {0}".format(count))
        client.publish(topic, count, qos=0)
        count += 1
        time.sleep(1)

我的问题是,为什么Flask应用程序在不实际处理消息的情况下一直不断地连接和断开连接.

My question is why the Flask app keeps connecting and disconnecting endlessly without actually processing a message.

推荐答案

连接到代理的所有客户端的客户端ID必须不同.在您发布的代码中,订阅者和发布者都使用相同的客户端ID(uuid=1234).

The client id needs to be different for all clients connected to a broker. In the code you've posted both the subscriber and the publisher are using the same client id (uuid=1234).

当具有相同客户端ID的2个客户端连接到代理时,代理将启动最早的代理.如果将其设置为重新连接,它将开始第二个连接.

When 2 clients with the same client id connect to the broker, the broker will kick the oldest off. If this is then set to reconnect it will then kick the second one off.

uuid设置为不同的值.

这篇关于带有MQTT的Python Flask应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆