Python (Flask) and MQTT listening

萝らか妹 提交于 2019-12-10 11:54:38

问题


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!


回答1:


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.




回答2:


try this:http://flask-mqtt.readthedocs.io/en/latest/#,a Flask extension that meant to facilitate the integration of a MQTT client into your web application



来源:https://stackoverflow.com/questions/38436412/python-flask-and-mqtt-listening

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!