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.
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