Python(Flask)和MQTT监听 [英] Python (Flask) and MQTT listening

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

问题描述

我目前正试图让我的Python(Flask)web服务器显示我的MQTT脚本正在做什么。 MQTT脚本,本质上,它订阅了一个主题,我真的很想对它得到的信息进行分类,并实时显示/更新它。

 设置1 = 9 
设置2 = 2 $ b类似于一个简单的列表,显示各种定期更新的设置。 $ b Setting3 = 5

首先,我有一个连接页面,在Python(Flask)应该连接到的IP:

  @ app.route(/)
def my_form():
return render_template(connect.html)

@ app.route(/,methods = [POST])
def my_form_post():
text = request.form [text]
processed_text = text.upper()

connect.html的样子:

 <!DOCTYPE html> 
< html lang =en>
< body>
< h2>填写以下IP:< / h2>
< input type =textname =text>
< input type =submitname =my-formvalue =Connect>
< / form>
< / body>
< / html>

连接到MQTT部分是棘手的部分,我需要一些帮助。我想只是添加下面的连接,但这只是不正确的,它会回来一个内部服务器错误。



任何帮助,将不胜感激!

谢谢!

解决方案

MQTT消息处理。这个服务可以处理收到的消息,并将它们存储起来(数据库,redis,简单的程序存储器)进行访问。



当烧瓶应用程序中的页面被击中时,到服务(或其存储)并且处理/显示自上次请求以来的信息。

这可以在反向以及从你的应用程序到后台服务发送MQTT消息的发布信息中完成。


I'm currently trying to get my Python (Flask) webserver to display what my MQTT script is doing. The MQTT script, In essence, it's subscribed to a topic and I would really like to categorize the info it gets and display/update it in real time. Something like a simple list displaying various the settings that gets updated regularly.

Setting1 = 9
Setting2 = 2
Setting3 = 5

To begin with, I have a connect to page, so that you can fill-in an IP to which the Python (Flask) should connect to:

@app.route("/")
def my_form():
        return render_template("connect.html")

@app.route("/", methods=["POST"])
def my_form_post():
        text = request.form["text"]
        processed_text = text.upper()

To which the connect.html looks like:

<!DOCTYPE html>
<html lang="en">
<body>
    <h2>Fill in the IP in below:</h2>
    <form action="." method="POST">
        <input type="text" name="text">
        <input type="submit" name="my-form" value="Connect">
    </form>
</body>
</html>

The connecting to the MQTT part is where the tricky part comes in and I will require some assistance. I was thinking of just adding the connect underneath but this just doesn't work right, it will just come back with an Internal Server Error.

Any help would really be appreciated!

Thanks!

解决方案

I would make a separate service for the MQTT message handling. This service could process the messages received and store them (database, redis, simple inprogram memory) for access.

When a page in your flask app gets hit, you would connect to the service (or its storage) and process/display the information since the last request.

This can be done in the reverse as well where your post information from your flask app to the backend service to send MQTT messages.

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

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